@@ -2,7 +2,11 @@ package sqlite3
2
2
3
3
import (
4
4
"database/sql"
5
+ "fmt"
5
6
"log"
7
+ "os"
8
+ "path/filepath"
9
+ "time"
6
10
7
11
"github.com/admpub/sessions"
8
12
sqlstore "github.com/coscms/session-sqlstore"
@@ -24,7 +28,7 @@ func New(cfg *Options) sessions.Store {
24
28
}
25
29
26
30
func Reg (store sessions.Store , args ... string ) {
27
- name := `sqlite3 `
31
+ name := `sqlite `
28
32
if len (args ) > 0 {
29
33
name = args [0 ]
30
34
}
@@ -54,17 +58,22 @@ const DDL = "CREATE TABLE IF NOT EXISTS %s (" +
54
58
" `expires` int NOT NULL DEFAULT '0');"
55
59
56
60
// NewSQLiteStore takes the following paramaters
57
- // endpoint - A sql.Open style endpoint
58
- // tableName - table where sessions are to be saved. Required fields are created automatically if the table doesnot exist.
59
61
// path - path for Set-Cookie header
60
- // maxAge
61
- // codecs
62
62
func NewSQLiteStore (cfg * Options ) (* SQLiteStore , error ) {
63
- db , err := sql .Open ("sqlite3" , cfg .Path )
63
+ var uri string
64
+ if len (cfg .Path ) == 0 {
65
+ d := fmt .Sprintf ("%d-session" , time .Now ().Unix ())
66
+ uri = filepath .Join (os .TempDir (), d , "sessions.db" )
67
+ os .MkdirAll (filepath .Dir (uri ), 0755 )
68
+ uri += `?tmp=true`
69
+ } else {
70
+ os .MkdirAll (filepath .Dir (cfg .Path ), 0755 )
71
+ uri = cfg .Path
72
+ }
73
+ db , err := sql .Open ("sqlite3" , "file:" + uri )
64
74
if err != nil {
65
75
return nil , err
66
76
}
67
-
68
77
return NewSQLiteStoreFromConnection (db , cfg )
69
78
}
70
79
0 commit comments