feat: 合并Gin_Vue
This commit is contained in:
32
src/lib_features/account/account.go
Normal file
32
src/lib_features/account/account.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package libfeatures
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ems.agt/lib/dborm"
|
||||
"ems.agt/lib/oauth"
|
||||
libConfig "ems.agt/restagent/config"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/redis"
|
||||
)
|
||||
|
||||
// SessionToken 设置登录会话-兼容旧登录方式
|
||||
func SessionToken(username, sourceAddr string) bool {
|
||||
token, _ := redis.Get("", "session_token")
|
||||
if token != "" {
|
||||
return true
|
||||
}
|
||||
|
||||
token = oauth.GenRandToken("omc") // Generate new token to session ID
|
||||
affected, err := dborm.XormInsertSession(username, sourceAddr, token, libConfig.GetExpiresFromConfig(), libConfig.GetYamlConfig().Auth.Session)
|
||||
if err != nil {
|
||||
logger.Errorf("SessionToken XormInsertSession err %v", err)
|
||||
}
|
||||
if affected == 1 {
|
||||
// 过期时间单位秒 配置1800是半小时
|
||||
expireTime := time.Duration(int64(libConfig.GetExpiresFromConfig())) * time.Second
|
||||
redis.SetByExpire("", "session_token", token, expireTime)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
53
src/lib_features/config/config.go
Normal file
53
src/lib_features/config/config.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package libfeatures
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
libConf "ems.agt/lib/core/conf"
|
||||
libGlobal "ems.agt/lib/global"
|
||||
libConfig "ems.agt/restagent/config"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// BuildInfo 程序-V查看编译版本号信息
|
||||
func BuildInfo() string {
|
||||
return fmt.Sprintf("OMC restagent version: %s\n%s\n%s\n\n", libGlobal.Version, libGlobal.BuildTime, libGlobal.GoVer)
|
||||
}
|
||||
|
||||
// ConfigRead 指定配置文件读取
|
||||
func ConfigRead(configFile string) {
|
||||
// 外层lib和features使用的配置
|
||||
libConfig.ReadConfig(configFile)
|
||||
uriPrefix := libConfig.GetYamlConfig().OMC.UriPrefix
|
||||
if uriPrefix != "" {
|
||||
libConfig.UriPrefix = uriPrefix
|
||||
}
|
||||
if libConfig.GetYamlConfig().TestConfig.Enabled {
|
||||
libConfig.ReadTestConfigYaml(libConfig.GetYamlConfig().TestConfig.File)
|
||||
}
|
||||
// 外层lib和features使用配置
|
||||
libConf.InitConfig(configFile)
|
||||
}
|
||||
|
||||
// 配置文件读取进行内部参数合并
|
||||
func ConfigInMerge() {
|
||||
// 合并外层lib和features使用配置
|
||||
for key, value := range libConf.AllSettings() {
|
||||
// 跳过配置
|
||||
if key == "testconfig" || key == "rest" || key == "logger" {
|
||||
continue
|
||||
}
|
||||
// 数据库配置
|
||||
if key == "database" {
|
||||
item := value.(map[string]any)
|
||||
defaultItem := viper.GetStringMap("gorm.datasource.default")
|
||||
defaultItem["host"] = item["host"]
|
||||
defaultItem["port"] = item["port"]
|
||||
defaultItem["username"] = item["user"]
|
||||
defaultItem["password"] = item["password"]
|
||||
defaultItem["database"] = item["name"]
|
||||
continue
|
||||
}
|
||||
viper.Set(key, value)
|
||||
}
|
||||
}
|
||||
5
src/lib_features/readme.md
Normal file
5
src/lib_features/readme.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# 外层 lib 和 features 粘合层
|
||||
|
||||
- config.go 配置合并: restagent.yaml 文件内容,主要是数据库配置
|
||||
- account.go 登录会话生成 token
|
||||
- session.go 中间件方式设置请求头 token 值
|
||||
23
src/lib_features/session/session.go
Normal file
23
src/lib_features/session/session.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"ems.agt/src/framework/redis"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SessionHeader 旧登录方式token头
|
||||
func SessionHeader() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 读取登录生成的会话token
|
||||
token, err := redis.Get("", "session_token")
|
||||
if err == nil {
|
||||
c.Request.Header.Set("Accesstoken", token)
|
||||
}
|
||||
|
||||
// Accesstoken: omc-ce4d0a86-8515-ad51-3249-4913c95f8e34
|
||||
// 调用下一个处理程序
|
||||
c.Next()
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user