53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
// Package files implements sessions saved into filesystem persistently encoded using gob
|
|
package files
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
sessDir string = "go-session"
|
|
sessExt string = "gsd"
|
|
)
|
|
|
|
func init() {
|
|
|
|
}
|
|
|
|
// ProviderFiles implement filesystem session provider
|
|
type ProviderFiles struct {
|
|
sessPath string
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func (pder *ProviderFiles) ckdirpath(sid string) string {
|
|
return fmt.Sprintf("%s/%s/%s.%s", pder.sessPath, sessDir, sid, sessExt)
|
|
}
|
|
|
|
func (pder *ProviderFiles) updateAtime(sid string) {}
|
|
|
|
// SetParams for files session provider set base path in filesystem for save sessions
|
|
func (pder *ProviderFiles) SetParams(p any) (err error) {
|
|
if p != nil {
|
|
if s, ok := p.(string); ok {
|
|
pder.sessPath = s
|
|
return
|
|
}
|
|
return fmt.Errorf("parameter for files session provider is not string")
|
|
}
|
|
return fmt.Errorf("parameter for files session provider must not be nil")
|
|
}
|
|
|
|
// Init create session file if not exists and retturn *Session
|
|
func (pder *ProviderFiles) Init(sid string) (err error) {
|
|
pder.lock.Lock()
|
|
defer pder.lock.Unlock()
|
|
var fd *os.File
|
|
ckf := pder.ckdirpath(sid)
|
|
if fd, err = os.Create(ckf); err != nil {
|
|
return fmt.Errorf("create session file: %s failed with err: %w", ckf, err)
|
|
}
|
|
return fd.Close()
|
|
}
|