1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 11:10:40 +00:00

support using json string to init session

This commit is contained in:
Ming Deng
2020-10-21 22:12:25 +08:00
parent 03ba495b7f
commit 05f4e0c146
12 changed files with 464 additions and 136 deletions

View File

@ -2,6 +2,7 @@ package ssdb
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
@ -18,33 +19,48 @@ var ssdbProvider = &Provider{}
// Provider holds ssdb client and configs
type Provider struct {
client *ssdb.Client
host string
port int
Host string `json:"host"`
Port int `json:"port"`
maxLifetime int64
}
func (p *Provider) connectInit() error {
var err error
if p.host == "" || p.port == 0 {
if p.Host == "" || p.Port == 0 {
return errors.New("SessionInit First")
}
p.client, err = ssdb.Connect(p.host, p.port)
p.client, err = ssdb.Connect(p.Host, p.Port)
return err
}
// SessionInit init the ssdb with the config
func (p *Provider) SessionInit(ctx context.Context, maxLifetime int64, savePath string) error {
func (p *Provider) SessionInit(ctx context.Context, maxLifetime int64, cfg string) error {
p.maxLifetime = maxLifetime
address := strings.Split(savePath, ":")
p.host = address[0]
cfg = strings.TrimSpace(cfg)
var err error
if p.port, err = strconv.Atoi(address[1]); err != nil {
// we think this is v2.0, using json to init the session
if strings.HasPrefix(cfg, "{") {
err = json.Unmarshal([]byte(cfg), p)
} else {
err = p.initOldStyle(cfg)
}
if err != nil {
return err
}
return p.connectInit()
}
// for v1.x
func (p *Provider) initOldStyle(savePath string) error {
address := strings.Split(savePath, ":")
p.Host = address[0]
var err error
p.Port, err = strconv.Atoi(address[1])
return err
}
// SessionRead return a ssdb client session Store
func (p *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
if p.client == nil {

View File

@ -0,0 +1,41 @@
// Copyright 2020
//
// 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 ssdb
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestProvider_SessionInit(t *testing.T) {
// using old style
savePath := `localhost:8080`
cp := &Provider{}
cp.SessionInit(context.Background(), 12, savePath)
assert.Equal(t, "localhost", cp.Host)
assert.Equal(t, 8080, cp.Port)
assert.Equal(t, int64(12), cp.maxLifetime)
savePath = `
{ "host": "localhost", "port": 8080}
`
cp = &Provider{}
cp.SessionInit(context.Background(), 12, savePath)
assert.Equal(t, "localhost", cp.Host)
assert.Equal(t, 8080, cp.Port)
assert.Equal(t, int64(12), cp.maxLifetime)
}