opt: 通过环境变量来传参,修正 docker compose 配置参数

This commit is contained in:
RockYang
2023-06-27 18:29:46 +08:00
parent 5d13b4b705
commit f7748d51df
13 changed files with 109 additions and 93 deletions

View File

@@ -8,6 +8,8 @@ import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"io"
@@ -54,8 +56,6 @@ func (s *AppServer) Init(debug bool) {
s.Engine.Use(sessionMiddleware(s.AppConfig))
s.Engine.Use(authorizeMiddleware(s))
s.Engine.Use(errorHandler)
//gob.Register(model.User{})
// 添加静态资源访问
s.Engine.Static("/static", s.AppConfig.StaticDir)
}
@@ -92,7 +92,27 @@ func errorHandler(c *gin.Context) {
// 会话处理
func sessionMiddleware(config *types.AppConfig) gin.HandlerFunc {
// encrypt the cookie
store := cookie.NewStore([]byte(config.Session.SecretKey))
var store sessions.Store
var err error
switch config.Session.Driver {
case types.SessionDriverMem:
store = memstore.NewStore([]byte(config.Session.SecretKey))
break
case types.SessionDriverRedis:
store, err = redis.NewStore(10, "tcp", config.Redis.Url(), config.Redis.Password, []byte(config.Session.SecretKey))
if err != nil {
logger.Fatal(err)
}
break
case types.SessionDriverCookie:
store = cookie.NewStore([]byte(config.Session.SecretKey))
break
default:
store = cookie.NewStore([]byte(config.Session.SecretKey))
}
logger.Info("Session driver: ", config.Session.Driver)
store.Options(sessions.Options{
Path: config.Session.Path,
Domain: config.Session.Domain,
@@ -143,6 +163,7 @@ func authorizeMiddleware(s *AppServer) gin.HandlerFunc {
if c.Request.URL.Path == "/api/user/login" ||
c.Request.URL.Path == "/api/admin/login" ||
c.Request.URL.Path == "/api/user/register" ||
strings.HasPrefix(c.Request.URL.Path, "/static/") ||
c.Request.URL.Path == "/api/admin/config/get" {
c.Next()
return

View File

@@ -20,8 +20,10 @@ func NewDefaultConfig() *types.AppConfig {
Manager: types.Manager{Username: "admin", Password: "admin123"},
StaticDir: "./static",
StaticUrl: "http://localhost/5678/static",
Redis: types.RedisConfig{Host: "localhost", Port: 6379, Password: ""},
Session: types.Session{
Driver: types.SessionDriverCookie,
SecretKey: utils.RandString(64),
Name: "CHAT_PLUS_SESSION",
Domain: "",

View File

@@ -1,6 +1,7 @@
package types
import (
"fmt"
"net/http"
)
@@ -9,10 +10,21 @@ type AppConfig struct {
Listen string
Session Session
ProxyURL string
MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL
MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL
Redis RedisConfig // redis 连接信息
}
type RedisConfig struct {
Host string
Port int
Password string
}
func (c RedisConfig) Url() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
// Manager 管理员
@@ -21,9 +33,18 @@ type Manager struct {
Password string `json:"password"`
}
type SessionDriver string
const (
SessionDriverMem = SessionDriver("mem")
SessionDriverRedis = SessionDriver("redis")
SessionDriverCookie = SessionDriver("cookie")
)
// Session configs struct
type Session struct {
SecretKey string // session encryption key
Driver SessionDriver // session 存储驱动 mem|cookie|redis
SecretKey string // session encryption key
Name string
Path string
Domain string