-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3.go
91 lines (81 loc) · 2.01 KB
/
sqlite3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package sqlite3
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/admpub/sessions"
sqlstore "github.com/coscms/session-sqlstore"
ss "github.com/webx-top/echo/middleware/session/engine"
"github.com/webx-top/echo/middleware/session/engine/file"
)
func New(cfg *Options) sessions.Store {
eng, err := NewSQLiteStore(cfg)
if err != nil {
log.Println("sessions: Operation SQLite failed:", err)
return file.NewFilesystemStore(&file.FileOptions{
SavePath: ``,
KeyPairs: cfg.KeyPairs,
CheckInterval: cfg.CheckInterval,
})
}
return eng
}
func Reg(store sessions.Store, args ...string) {
name := `sqlite`
if len(args) > 0 {
name = args[0]
}
ss.Reg(name, store)
}
func RegWithOptions(opts *Options, args ...string) sessions.Store {
store := New(opts)
Reg(store, args...)
return store
}
type Options struct {
Path string `json:"path"`
sqlstore.Options
}
type SQLiteStore struct {
*sqlstore.SQLStore
}
const DDL = "CREATE TABLE IF NOT EXISTS %s (" +
" `id` char(64) PRIMARY KEY," +
" `data` longblob NOT NULL," +
" `created` int NOT NULL DEFAULT '0'," +
" `modified` int NOT NULL DEFAULT '0'," +
" `expires` int NOT NULL DEFAULT '0');"
// NewSQLiteStore takes the following paramaters
// path - path for Set-Cookie header
func NewSQLiteStore(cfg *Options) (*SQLiteStore, error) {
var uri string
if len(cfg.Path) == 0 {
d := fmt.Sprintf("%d-session", time.Now().Unix())
uri = filepath.Join(os.TempDir(), d, "sessions.db")
os.MkdirAll(filepath.Dir(uri), 0755)
uri += `?tmp=true`
} else {
os.MkdirAll(filepath.Dir(cfg.Path), 0755)
uri = cfg.Path
}
db, err := sql.Open("sqlite3", "file:"+uri)
if err != nil {
return nil, err
}
return NewSQLiteStoreFromConnection(db, cfg)
}
// NewSQLiteStoreFromConnection .
func NewSQLiteStoreFromConnection(db *sql.DB, cfg *Options) (*SQLiteStore, error) {
cfg.Options.SetDDL(DDL)
base, err := sqlstore.New(db, &cfg.Options)
if err != nil {
return nil, err
}
s := &SQLiteStore{
SQLStore: base,
}
return s, nil
}