mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-25 03:06:10 +01:00
6433ba0ec3
Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
217 lines
5.1 KiB
Go
217 lines
5.1 KiB
Go
// Copyright 2013 Beego Authors
|
|
// Copyright 2014 The Macaron Authors
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
// not use this file except in compliance with the License. You may obtain
|
|
// a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
// License for the specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package session
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/nosql"
|
|
|
|
"gitea.com/go-chi/session"
|
|
"github.com/go-redis/redis/v7"
|
|
)
|
|
|
|
// RedisStore represents a redis session store implementation.
|
|
type RedisStore struct {
|
|
c redis.UniversalClient
|
|
prefix, sid string
|
|
duration time.Duration
|
|
lock sync.RWMutex
|
|
data map[interface{}]interface{}
|
|
}
|
|
|
|
// NewRedisStore creates and returns a redis session store.
|
|
func NewRedisStore(c redis.UniversalClient, prefix, sid string, dur time.Duration, kv map[interface{}]interface{}) *RedisStore {
|
|
return &RedisStore{
|
|
c: c,
|
|
prefix: prefix,
|
|
sid: sid,
|
|
duration: dur,
|
|
data: kv,
|
|
}
|
|
}
|
|
|
|
// Set sets value to given key in session.
|
|
func (s *RedisStore) Set(key, val interface{}) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.data[key] = val
|
|
return nil
|
|
}
|
|
|
|
// Get gets value by given key in session.
|
|
func (s *RedisStore) Get(key interface{}) interface{} {
|
|
s.lock.RLock()
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.data[key]
|
|
}
|
|
|
|
// Delete delete a key from session.
|
|
func (s *RedisStore) Delete(key interface{}) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
delete(s.data, key)
|
|
return nil
|
|
}
|
|
|
|
// ID returns current session ID.
|
|
func (s *RedisStore) ID() string {
|
|
return s.sid
|
|
}
|
|
|
|
// Release releases resource and save data to provider.
|
|
func (s *RedisStore) Release() error {
|
|
// Skip encoding if the data is empty
|
|
if len(s.data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
data, err := session.EncodeGob(s.data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.c.Set(s.prefix+s.sid, string(data), s.duration).Err()
|
|
}
|
|
|
|
// Flush deletes all session data.
|
|
func (s *RedisStore) Flush() error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.data = make(map[interface{}]interface{})
|
|
return nil
|
|
}
|
|
|
|
// RedisProvider represents a redis session provider implementation.
|
|
type RedisProvider struct {
|
|
c redis.UniversalClient
|
|
duration time.Duration
|
|
prefix string
|
|
}
|
|
|
|
// Init initializes redis session provider.
|
|
// configs: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180,prefix=session;
|
|
func (p *RedisProvider) Init(maxlifetime int64, configs string) (err error) {
|
|
p.duration, err = time.ParseDuration(fmt.Sprintf("%ds", maxlifetime))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
uri := nosql.ToRedisURI(configs)
|
|
|
|
for k, v := range uri.Query() {
|
|
switch k {
|
|
case "prefix":
|
|
p.prefix = v[0]
|
|
}
|
|
}
|
|
|
|
p.c = nosql.GetManager().GetRedisClient(uri.String())
|
|
return p.c.Ping().Err()
|
|
}
|
|
|
|
// Read returns raw session store by session ID.
|
|
func (p *RedisProvider) Read(sid string) (session.RawStore, error) {
|
|
psid := p.prefix + sid
|
|
if !p.Exist(sid) {
|
|
if err := p.c.Set(psid, "", p.duration).Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
var kv map[interface{}]interface{}
|
|
kvs, err := p.c.Get(psid).Result()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(kvs) == 0 {
|
|
kv = make(map[interface{}]interface{})
|
|
} else {
|
|
kv, err = session.DecodeGob([]byte(kvs))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return NewRedisStore(p.c, p.prefix, sid, p.duration, kv), nil
|
|
}
|
|
|
|
// Exist returns true if session with given ID exists.
|
|
func (p *RedisProvider) Exist(sid string) bool {
|
|
v, err := p.c.Exists(p.prefix + sid).Result()
|
|
return err == nil && v == 1
|
|
}
|
|
|
|
// Destroy deletes a session by session ID.
|
|
func (p *RedisProvider) Destroy(sid string) error {
|
|
return p.c.Del(p.prefix + sid).Err()
|
|
}
|
|
|
|
// Regenerate regenerates a session store from old session ID to new one.
|
|
func (p *RedisProvider) Regenerate(oldsid, sid string) (_ session.RawStore, err error) {
|
|
poldsid := p.prefix + oldsid
|
|
psid := p.prefix + sid
|
|
|
|
if p.Exist(sid) {
|
|
return nil, fmt.Errorf("new sid '%s' already exists", sid)
|
|
} else if !p.Exist(oldsid) {
|
|
// Make a fake old session.
|
|
if err = p.c.Set(poldsid, "", p.duration).Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err = p.c.Rename(poldsid, psid).Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var kv map[interface{}]interface{}
|
|
kvs, err := p.c.Get(psid).Result()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(kvs) == 0 {
|
|
kv = make(map[interface{}]interface{})
|
|
} else {
|
|
kv, err = session.DecodeGob([]byte(kvs))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return NewRedisStore(p.c, p.prefix, sid, p.duration, kv), nil
|
|
}
|
|
|
|
// Count counts and returns number of sessions.
|
|
func (p *RedisProvider) Count() int {
|
|
return int(p.c.DBSize().Val())
|
|
}
|
|
|
|
// GC calls GC to clean expired sessions.
|
|
func (*RedisProvider) GC() {}
|
|
|
|
func init() {
|
|
session.Register("redis", &RedisProvider{})
|
|
}
|