Skip to content

Commit 49b37f3

Browse files
committed
update
1 parent 83ce514 commit 49b37f3

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

sqlite3.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package sqlite3
22

33
import (
44
"database/sql"
5+
"fmt"
56
"log"
7+
"os"
8+
"path/filepath"
9+
"time"
610

711
"github.com/admpub/sessions"
812
sqlstore "github.com/coscms/session-sqlstore"
@@ -24,7 +28,7 @@ func New(cfg *Options) sessions.Store {
2428
}
2529

2630
func Reg(store sessions.Store, args ...string) {
27-
name := `sqlite3`
31+
name := `sqlite`
2832
if len(args) > 0 {
2933
name = args[0]
3034
}
@@ -54,17 +58,22 @@ const DDL = "CREATE TABLE IF NOT EXISTS %s (" +
5458
" `expires` int NOT NULL DEFAULT '0');"
5559

5660
// 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.
5961
// path - path for Set-Cookie header
60-
// maxAge
61-
// codecs
6262
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)
6474
if err != nil {
6575
return nil, err
6676
}
67-
6877
return NewSQLiteStoreFromConnection(db, cfg)
6978
}
7079

0 commit comments

Comments
 (0)