[add] redis session store

This commit is contained in:
Max 2022-05-25 23:38:53 +08:00
parent ad0535e17f
commit 6e9bc9b7be
5 changed files with 45 additions and 270 deletions

View file

@ -124,252 +124,3 @@ func CloseLog() {
}
}
}
// // Config 系统配置
// type Config struct {
// XiangConfig
// Service ServiceConfig `json:"service,omitempty"`
// Session SessionConfig `json:"session,omitempty"`
// Database DatabaseConfig `json:"database,omitempty"`
// JWT JWTConfig `json:"jwt,omitempty"`
// Log LogConfig `json:"log,omitempty"`
// }
// // JWTConfig JWT配置
// type JWTConfig struct {
// Secret string `json:"secret,omitempty" env:"YAO_JWT_SECRET"` // JWT 密钥
// }
// // LogConfig 日志配置
// type LogConfig struct {
// Access string `json:"access,omitempty" env:"XIANG_LOG_ACCESS" envDefault:"os://stdout"` // 服务访问日志
// Error string `json:"error,omitempty" env:"XIANG_LOG_ERROR" envDefault:"os://stderr"` // 服务错误日志
// DB string `json:"database,omitempty" env:"XIANG_LOG_DB" envDefault:"os://stdout"` // 数据库日志
// Plugin string `json:"plugin,omitempty" env:"XIANG_LOG_PLUGIN" envDefault:"os://stdout"` // 插件日志
// }
// // XiangConfig 象传应用引擎配置
// type XiangConfig struct {
// Mode string `json:"mode,omitempty" env:"XIANG_MODE" envDefault:"release"` // 象传引擎模式 debug/release/test
// Source string `json:"source,omitempty" env:"XIANG_SOURCE" envDefault:"fs://."` // 源码路径(用于单元测试载入数据)
// Path string `json:"path,omitempty" env:"XIANG_PATH" envDefault:"bin://xiang"` // 引擎文件目录
// Root string `json:"root,omitempty" env:"XIANG_ROOT" envDefault:"fs://."` // 应用文件目录
// RootUI string `json:"root_ui,omitempty" env:"XIANG_ROOT_UI"` // 应用界面静态文件目录
// RootDB string `json:"root_db,omitempty" env:"XIANG_ROOT_DB"` // 应用SQLite数据库目录
// RootData string `json:"root_data,omitempty" env:"XIANG_ROOT_DATA"` // 应用数据文件目录
// RootAPI string `json:"root_api,omitempty" env:"XIANG_ROOT_API"` // 应用API文件目录
// RootModel string `json:"root_model,omitempty" env:"XIANG_ROOT_MODEL"` // 应用模型文件目录
// RootFLow string `json:"root_flow,omitempty" env:"XIANG_ROOT_FLOW"` // 应用业务逻辑文件目录
// RootPlugin string `json:"root_plugin,omitempty" env:"XIANG_ROOT_PLUGIN"` // 应用业务插件文件目录
// RootLib string `json:"root_lib,omitempty" env:"XIANG_ROOT_LIB"` // 应用资料库文件目录
// RootTable string `json:"root_table,omitempty" env:"XIANG_ROOT_TABLE"` // 应用数据表格文件目录
// RootChart string `json:"root_chart,omitempty" env:"XIANG_ROOT_CHART"` // 应用分析图表文件目录
// RootPage string `json:"root_page,omitempty" env:"XIANG_ROOT_PAGE"` // 应用通用页面文件目录
// RootScreen string `json:"root_screen,omitempty" env:"XIANG_ROOT_SCREEN"` // 应用大屏文件目录
// RootForm string `json:"root_form,omitempty" env:"XIANG_ROOT_FORM"` // 表单文件目录
// RootWorkFlow string `json:"root_workflow,omitempty" env:"XIANG_ROOT_WORKFLOW"` // 工作流文件目录
// }
// // ServiceConfig 服务配置
// type ServiceConfig struct {
// Debug bool `json:"debug,omitempty" env:"XIANG_SERVICE_DEBUG" envDefault:"false"` // DEBUG 开关
// HTTPS bool `json:"https,omitempty" env:"XIANG_SERVICE_HTTPS" envDefault:"false"` // HTTPS 开关
// Cert string `json:"cert,omitempty" env:"XIANG_SERVICE_CERT"` // HTTPS 证书
// Key string `json:"key,omitempty" env:"XIANG_SERVICE_KEY"` // HTTPS 证书密钥
// Allow []string `json:"allow,omitempty" env:"XIANG_SERVICE_ALLOW" envSeparator:"|"` // 跨域访问域名列表
// Host string `json:"host,omitempty" env:"XIANG_SERVICE_HOST" envDefault:"127.0.0.1"` // 服务监听IP
// Port int `json:"port,omitempty" env:"XIANG_SERVICE_PORT" envDefault:"5099"` // 服务监听端口
// }
// SessionConfig 会话服务器
type SessionConfig struct {
Debug bool `json:"debug,omitempty" env:"XIANG_SESSION_DEBUG" envDefault:"false"` // DEBUG 开关
Hosting bool `json:"hosting,omitempty" env:"XIANG_SESSION_HOSTING" envDefault:"true"` // 会话服务器
IsCLI bool `json:"iscli,omitempty" env:"XIANG_SESSION_ISCLI" envDefault:"false"` // 是否为客户端启动
Host string `json:"host,omitempty" env:"XIANG_SESSION_HOST" envDefault:"127.0.0.1"` // 会话服务器IP
Port int `json:"port,omitempty" env:"XIANG_SESSION_PORT" envDefault:"3322"` // 会话服务器端口
}
// // DatabaseConfig 数据库配置
// type DatabaseConfig struct {
// Debug bool `json:"debug,omitempty" env:"XIANG_DB_DEBUG" envDefault:"false"` // DEBUG 开关
// Driver string `json:"driver,omitempty" env:"XIANG_DB_DRIVER" envDefault:"sqlite3"` // 数据库驱动 ( sqlite3, mysql, postgres)
// Primary []string `json:"primary,omitempty" env:"XIANG_DB_PRIMARY" envSeparator:"|" envDefault:"file:./db/xiang.db"` // 主库连接DSN
// Secondary []string `json:"secondary,omitempty" env:"XIANG_DB_SECONDARY" envSeparator:"|"` // 从库连接DSN
// AESKey string `json:"aeskey,omitempty" env:"XIANG_DB_AESKEY"` // 加密存储KEY
// }
// // NewConfig 创建配置文件
// func NewConfig(envfile ...string) Config {
// filename := os.Getenv("XIANG_ENV_FILE")
// if filename == "" {
// filename = ".env"
// }
// if len(envfile) > 0 {
// file, err := filepath.Abs(envfile[0])
// if err == nil {
// // log.Printf("加载环境配置文件%s出错 %s\n", envfile[0], err.Error())
// // } else {
// filename = file
// }
// }
// godotenv.Overload(filename)
// // if err != nil {
// // log.Printf("加载环境配置文件%s出错 %s\n", filename, err.Error())
// // }
// cfg := Config{}
// if err := env.Parse(&cfg); err != nil {
// exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
// }
// cfg.SetDefaults()
// return cfg
// }
// // NewConfigFrom 创建配置文件
// func NewConfigFrom(input io.Reader) Config {
// cfg := Config{}
// err := helper.UnmarshalFile(input, &cfg)
// if err != nil {
// exception.New("解析配置文件出错 %s", 500, err.Error()).Throw()
// }
// cfg.SetDefaults()
// return cfg
// }
// // SetDefaults 设定默认值
// func (cfg *Config) SetDefaults() {
// pwd, err := os.Getwd()
// if err != nil {
// log.Fatal(err.Error())
// }
// if cfg.Root == "fs://." || cfg.Root == "." {
// cfg.Root = pwd
// }
// if cfg.RootAPI == "" {
// cfg.RootAPI = cfg.Root + "/apis"
// }
// if cfg.RootFLow == "" {
// cfg.RootFLow = cfg.Root + "/flows"
// }
// if cfg.RootModel == "" {
// cfg.RootModel = cfg.Root + "/models"
// }
// if cfg.RootPlugin == "" {
// cfg.RootPlugin = cfg.Root + "/plugins"
// }
// if cfg.RootTable == "" {
// cfg.RootTable = cfg.Root + "/tables"
// }
// if cfg.RootLib == "" {
// cfg.RootLib = cfg.Root + "/libs"
// }
// if cfg.RootChart == "" {
// cfg.RootChart = cfg.Root + "/charts"
// }
// if cfg.RootPage == "" {
// cfg.RootPage = cfg.Root + "/pages"
// }
// if cfg.RootWorkFlow == "" {
// cfg.RootWorkFlow = cfg.Root + "/workflows"
// }
// if cfg.RootData == "" {
// cfg.RootData = cfg.Root + "/data"
// }
// if cfg.RootDB == "" {
// cfg.RootDB = cfg.Root + "/db"
// }
// if cfg.RootUI == "" {
// cfg.RootUI = cfg.Root + "/ui"
// }
// if len(cfg.Database.Primary) > 0 {
// if strings.HasPrefix(cfg.Database.Primary[0], "file:.") {
// cfg.Database.Primary[0] = strings.ReplaceAll(cfg.Database.Primary[0], "file:.", pwd)
// } else if strings.HasPrefix(cfg.Database.Primary[0], "file:/") {
// cfg.Database.Primary[0] = strings.ReplaceAll(cfg.Database.Primary[0], "file:.", cfg.RootDB)
// }
// }
// // 过滤数据
// cfg.RootDB = strings.TrimPrefix(cfg.RootDB, "fs://")
// cfg.RootDB = strings.TrimPrefix(cfg.RootDB, "file://")
// cfg.RootData = strings.TrimPrefix(cfg.RootData, "fs://")
// cfg.RootData = strings.TrimPrefix(cfg.RootData, "file://")
// cfg.RootUI = strings.TrimPrefix(cfg.RootUI, "fs://")
// cfg.RootUI = strings.TrimPrefix(cfg.RootUI, "file://")
// cfg.RootLib = strings.TrimPrefix(cfg.RootLib, "fs://")
// cfg.RootLib = strings.TrimPrefix(cfg.RootLib, "file://")
// }
// // SetEnvFile 指定ENV文件
// func SetEnvFile(filename string) {
// Conf = NewConfig(filename)
// }
// // SetAppPath 设定应用目录
// func SetAppPath(root string, envfile ...string) {
// fullpath, err := filepath.Abs(root)
// if err != nil {
// log.Panic("设定应用目录%s出错 %s\n", root, err.Error())
// }
// // 创建应用目录
// pathinfo, err := os.Stat(fullpath)
// if os.IsNotExist(err) {
// err := os.MkdirAll(fullpath, os.ModePerm)
// if err != nil {
// log.Panic("创建目录失败(%s) %s", root, err)
// }
// }
// pathinfo, err = os.Stat(fullpath)
// if !pathinfo.IsDir() {
// log.Panic("检查应用目录失败(%s) ", err)
// }
// if !pathinfo.IsDir() {
// log.Panic("应用目录不是文件夹(%s) ", root)
// }
// // Set ENV
// if len(envfile) > 0 {
// Conf = NewConfig(envfile[0])
// }
// // 从加载配置文件
// Conf.Root = fullpath
// Conf.RootAPI = filepath.Join(fullpath, "/apis")
// Conf.RootFLow = filepath.Join(fullpath, "/flows")
// Conf.RootModel = filepath.Join(fullpath, "/models")
// Conf.RootPlugin = filepath.Join(fullpath, "/plugins")
// Conf.RootTable = filepath.Join(fullpath, "/tables")
// Conf.RootChart = filepath.Join(fullpath, "/charts")
// Conf.RootPage = filepath.Join(fullpath, "/pages")
// Conf.RootWorkFlow = filepath.Join(fullpath, "/workflows")
// Conf.RootData = filepath.Join(fullpath, "/data")
// Conf.RootUI = filepath.Join(fullpath, "/ui")
// Conf.RootDB = filepath.Join(fullpath, "/db")
// Conf.RootLib = filepath.Join(fullpath, "/libs")
// }
// // IsDebug 是否为调试模式
// func IsDebug() bool {
// return Conf.Mode == "debug"
// }

View file

@ -24,3 +24,14 @@ type DBConfig struct {
Secondary []string `json:"secondary,omitempty" env:"YAO_DB_SECONDARY" envSeparator:"|"` // 从库连接DSN
AESKey string `json:"aeskey,omitempty" env:"YAO_DB_AESKEY"` // 加密存储KEY
}
// SessionConfig 会话服务器
type SessionConfig struct {
Store string `json:"store,omitempty" env:"YAO_SESSION_STORE" envDefault:"server"` // The session store. server | redis | file
Host string `json:"host,omitempty" env:"YAO_SESSION_HOST" envDefault:"127.0.0.1"` // The redis host
Port string `json:"port,omitempty" env:"YAO_SESSION_PORT" envDefault:"6379"` // The redis port
Password string `json:"password,omitempty" env:"YAO_SESSION_PASSWORD"` // The redis password
Username string `json:"username,omitempty" env:"YAO_SESSION_USERNAME"` // The redis username
DB string `json:"db,omitempty" env:"YAO_SESSION_DB" envDefault:"1"` // The redis username
IsCLI bool `json:"iscli,omitempty" env:"YAO_SESSION_ISCLI" envDefault:"false"` // Command Line Start
}

7
go.sum
View file

@ -80,6 +80,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@ -102,6 +103,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
@ -147,6 +149,7 @@ github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
@ -377,15 +380,18 @@ github.com/mojocn/base64Captcha v1.3.5 h1:Qeilr7Ta6eDtG4S+tQuZ5+hO+QHbiGAJdi4Pfo
github.com/mojocn/base64Captcha v1.3.5/go.mod h1:/tTTXn4WTpX9CfrmipqRytCpJ27Uw3G6I7NcP2WwcmY=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0 h1:6gjqkI8iiRHMvdccRJM8rVKjCWk6ZIm6FTm3ddIe4/c=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
@ -895,6 +901,7 @@ gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375/go.mod h1:lNEQeAhU009zbRxng+XOj5ITVgY24WcbNnQopyfKoYQ=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View file

@ -12,8 +12,10 @@ var shutdownComplete = make(chan bool)
// Start 启动服务
func Start() {
if config.Conf.Session.Hosting && config.Conf.Session.IsCLI == false {
if config.Conf.Session.Store == "server" && config.Conf.Session.IsCLI == false {
share.SessionServerStart()
} else if config.Conf.Session.Store == "redis" {
share.SessionConnect()
}
gou.SetHTTPGuards(Guards)

View file

@ -7,6 +7,7 @@ import (
"log"
klog "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/network"
"github.com/buraksezer/olric"
@ -23,31 +24,34 @@ var SessionPort int
var SessionMemberPort int
func init() {
SessionPort = network.FreePort()
SessionMemberPort = network.FreePort()
klog.Trace("Session port: %d, Member Port: %d", SessionPort, SessionMemberPort)
if config.Conf.Session.Store == "server" {
SessionPort = network.FreePort()
SessionMemberPort = network.FreePort()
klog.Trace("Session port: %d, Member Port: %d", SessionPort, SessionMemberPort)
}
}
// SessionConnect 加载会话信息 (废弃->共享方案用 Redis 替代)
// func SessionConnect() {
// SessionConnect Connect redis server
func SessionConnect() {
args := []string{}
if config.Conf.Session.Port == "" {
config.Conf.Session.Port = "6379"
}
// stats, err := sessServer.Stats()
// fmt.Println(stats, err)
if config.Conf.Session.DB == "" {
config.Conf.Session.DB = "1"
}
// var clientConfig = &client.Config{
// Servers: []string{fmt.Sprintf("%s:%d", "127.0.0.1", SessionPort)},
// Serializer: serializer.NewMsgpackSerializer(),
// Client: config_olric.NewClient(),
// }
args = append(args, config.Conf.Session.Port, config.Conf.Session.DB, config.Conf.Session.Password)
rdb, err := session.NewRedis(config.Conf.Host, args...)
if err != nil {
panic(err)
}
// c, err := client.New(clientConfig)
// if err != nil {
// exception.New("会话服务器连接失败 %s", 500, err.Error()).Throw()
// }
// dm := c.NewDMap("local-session")
// session.MemoryUse(session.ClientDMap{DMap: dm})
// }
session.Register("redis", rdb)
session.Name = "redis"
klog.Trace("Session Store:REDIS HOST:%s PORT:%s DB:%s", config.Conf.Session.Host, config.Conf.Session.Port, config.Conf.Session.DB)
}
// SessionServerStop 关闭会话服务器
func SessionServerStop() {