Skip to content

Commit 0ad7db5

Browse files
committed
Move to call this db session as per @lunny
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 54f04f4 commit 0ad7db5

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
550550

551551
## Session (`session`)
552552

553-
- `PROVIDER`: **memory**: Session engine provider \[memory, file, redis, xorm, mysql, couchbase, memcache, postgres\].
553+
- `PROVIDER`: **memory**: Session engine provider \[memory, file, redis, db, mysql, couchbase, memcache, postgres\].
554554
- `PROVIDER_CONFIG`: **data/sessions**: For file, the root path; for others, the connection string.
555555
- `COOKIE_SECURE`: **false**: Enable this to force using HTTPS for all session access.
556556
- `COOKIE_NAME`: **i\_like\_gitea**: The name of the cookie used for the session ID.

models/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"code.gitea.io/gitea/modules/timeutil"
1111
)
1212

13-
// Session represents a session compatible for go-macaron session
13+
// Session represents a session compatible for go-chi session
1414
type Session struct {
1515
Key string `xorm:"pk CHAR(16)"` // has to be Key to match with go-chi/session
1616
Data []byte `xorm:"BLOB"`
17-
Expiry timeutil.TimeStamp // has to be Expiry to match with go-macaron/session
17+
Expiry timeutil.TimeStamp // has to be Expiry to match with go-chi/session
1818
}
1919

2020
// UpdateSession updates the session with provided id

modules/session/xorm.go renamed to modules/session/db.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ import (
1414
"gitea.com/go-chi/session"
1515
)
1616

17-
// XormStore represents a xorm session store implementation.
18-
type XormStore struct {
17+
// DBStore represents a session store implementation based on the DB.
18+
type DBStore struct {
1919
sid string
2020
lock sync.RWMutex
2121
data map[interface{}]interface{}
2222
}
2323

24-
// NewXormStore creates and returns a Xorm session store.
25-
func NewXormStore(sid string, kv map[interface{}]interface{}) *XormStore {
26-
return &XormStore{
24+
// NewDBStore creates and returns a DB session store.
25+
func NewDBStore(sid string, kv map[interface{}]interface{}) *DBStore {
26+
return &DBStore{
2727
sid: sid,
2828
data: kv,
2929
}
3030
}
3131

3232
// Set sets value to given key in session.
33-
func (s *XormStore) Set(key, val interface{}) error {
33+
func (s *DBStore) Set(key, val interface{}) error {
3434
s.lock.Lock()
3535
defer s.lock.Unlock()
3636

@@ -39,15 +39,15 @@ func (s *XormStore) Set(key, val interface{}) error {
3939
}
4040

4141
// Get gets value by given key in session.
42-
func (s *XormStore) Get(key interface{}) interface{} {
42+
func (s *DBStore) Get(key interface{}) interface{} {
4343
s.lock.RLock()
4444
defer s.lock.RUnlock()
4545

4646
return s.data[key]
4747
}
4848

4949
// Delete delete a key from session.
50-
func (s *XormStore) Delete(key interface{}) error {
50+
func (s *DBStore) Delete(key interface{}) error {
5151
s.lock.Lock()
5252
defer s.lock.Unlock()
5353

@@ -56,12 +56,12 @@ func (s *XormStore) Delete(key interface{}) error {
5656
}
5757

5858
// ID returns current session ID.
59-
func (s *XormStore) ID() string {
59+
func (s *DBStore) ID() string {
6060
return s.sid
6161
}
6262

6363
// Release releases resource and save data to provider.
64-
func (s *XormStore) Release() error {
64+
func (s *DBStore) Release() error {
6565
// Skip encoding if the data is empty
6666
if len(s.data) == 0 {
6767
return nil
@@ -76,28 +76,28 @@ func (s *XormStore) Release() error {
7676
}
7777

7878
// Flush deletes all session data.
79-
func (s *XormStore) Flush() error {
79+
func (s *DBStore) Flush() error {
8080
s.lock.Lock()
8181
defer s.lock.Unlock()
8282

8383
s.data = make(map[interface{}]interface{})
8484
return nil
8585
}
8686

87-
// XormProvider represents a Xorm session provider implementation.
88-
type XormProvider struct {
87+
// DBProvider represents a DB session provider implementation.
88+
type DBProvider struct {
8989
maxLifetime int64
9090
}
9191

92-
// Init initializes Xorm session provider.
92+
// Init initializes DB session provider.
9393
// connStr: username:password@protocol(address)/dbname?param=value
94-
func (p *XormProvider) Init(maxLifetime int64, connStr string) error {
94+
func (p *DBProvider) Init(maxLifetime int64, connStr string) error {
9595
p.maxLifetime = maxLifetime
9696
return nil
9797
}
9898

9999
// Read returns raw session store by session ID.
100-
func (p *XormProvider) Read(sid string) (session.RawStore, error) {
100+
func (p *DBProvider) Read(sid string) (session.RawStore, error) {
101101
s, err := models.ReadSession(sid)
102102
if err != nil {
103103
return nil, err
@@ -113,25 +113,25 @@ func (p *XormProvider) Read(sid string) (session.RawStore, error) {
113113
}
114114
}
115115

116-
return NewXormStore(sid, kv), nil
116+
return NewDBStore(sid, kv), nil
117117
}
118118

119119
// Exist returns true if session with given ID exists.
120-
func (p *XormProvider) Exist(sid string) bool {
120+
func (p *DBProvider) Exist(sid string) bool {
121121
has, err := models.ExistSession(sid)
122122
if err != nil {
123-
panic("session/Xorm: error checking existence: " + err.Error())
123+
panic("session/DB: error checking existence: " + err.Error())
124124
}
125125
return has
126126
}
127127

128128
// Destroy deletes a session by session ID.
129-
func (p *XormProvider) Destroy(sid string) error {
129+
func (p *DBProvider) Destroy(sid string) error {
130130
return models.DestroySession(sid)
131131
}
132132

133133
// Regenerate regenerates a session store from old session ID to new one.
134-
func (p *XormProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) {
134+
func (p *DBProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) {
135135
s, err := models.RegenerateSession(oldsid, sid)
136136
if err != nil {
137137
return nil, err
@@ -148,25 +148,25 @@ func (p *XormProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err e
148148
}
149149
}
150150

151-
return NewXormStore(sid, kv), nil
151+
return NewDBStore(sid, kv), nil
152152
}
153153

154154
// Count counts and returns number of sessions.
155-
func (p *XormProvider) Count() int {
155+
func (p *DBProvider) Count() int {
156156
total, err := models.CountSessions()
157157
if err != nil {
158-
panic("session/Xorm: error counting records: " + err.Error())
158+
panic("session/DB: error counting records: " + err.Error())
159159
}
160160
return int(total)
161161
}
162162

163163
// GC calls GC to clean expired sessions.
164-
func (p *XormProvider) GC() {
164+
func (p *DBProvider) GC() {
165165
if err := models.CleanupSessions(p.maxLifetime); err != nil {
166-
log.Printf("session/Xorm: error garbage collecting: %v", err)
166+
log.Printf("session/DB: error garbage collecting: %v", err)
167167
}
168168
}
169169

170170
func init() {
171-
session.Register("Xorm", &XormProvider{})
171+
session.Register("db", &DBProvider{})
172172
}

modules/session/virtual.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (o *VirtualSessionProvider) Init(gclifetime int64, config string) error {
3939
o.provider = &session.FileProvider{}
4040
case "redis":
4141
o.provider = &RedisProvider{}
42-
case "xorm":
43-
o.provider = &XormProvider{}
42+
case "db":
43+
o.provider = &DBProvider{}
4444
case "mysql":
4545
o.provider = &mysql.MysqlProvider{}
4646
case "postgres":

modules/setting/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var (
4141
func newSessionService() {
4242
sec := Cfg.Section("session")
4343
SessionConfig.Provider = sec.Key("PROVIDER").In("memory",
44-
[]string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache"})
44+
[]string{"memory", "file", "redis", "mysql", "postgres", "couchbase", "memcache", "db"})
4545
SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(path.Join(AppDataPath, "sessions")), "\" ")
4646
if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
4747
SessionConfig.ProviderConfig = path.Join(AppWorkPath, SessionConfig.ProviderConfig)

0 commit comments

Comments
 (0)