系统设置页面功能完成

This commit is contained in:
RockYang
2023-05-03 09:48:37 +08:00
parent 556abf5276
commit fcfe0c7913
6 changed files with 181 additions and 99 deletions

View File

@@ -19,8 +19,15 @@ func (s *Server) AddApiKeyHandle(c *gin.Context) {
c.JSON(http.StatusBadRequest, nil)
return
}
// 过滤已存在的 Key
for _,key:=range s.Config.Chat.ApiKeys {
if key.Value == data.ApiKey {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "API KEY 已存在"})
return
}
}
if len(data.ApiKey) > 20 {
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, data.ApiKey)
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, types.APIKey{Value: data.ApiKey})
}
// 保存配置文件
@@ -46,8 +53,9 @@ func (s *Server) RemoveApiKeyHandle(c *gin.Context) {
}
for i, v := range s.Config.Chat.ApiKeys {
if v == data.ApiKey {
if v.Value == data.ApiKey {
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...)
break
}
}

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"openai/types"
"openai/utils"
"strings"
)
func (s *Server) TestHandle(c *gin.Context) {
@@ -17,9 +18,42 @@ func (s *Server) TestHandle(c *gin.Context) {
}
func (s *Server) ConfigGetHandle(c *gin.Context) {
data := struct {
Title string `json:"title"`
ConsoleTitle string `json:"console_title"`
ProxyURL string `json:"proxy_url"`
Model string `json:"model"`
Temperature float32 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
ChatContextExpireTime int `json:"chat_context_expire_time"`
EnableContext bool `json:"enable_context"`
}{
Title: s.Config.Title,
ConsoleTitle: s.Config.ConsoleTitle,
ProxyURL: strings.Join(s.Config.ProxyURL, ","),
Model: s.Config.Chat.Model,
Temperature: s.Config.Chat.Temperature,
MaxTokens: s.Config.Chat.MaxTokens,
EnableContext: s.Config.Chat.EnableContext,
ChatContextExpireTime: s.Config.Chat.ChatContextExpireTime,
}
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: data})
}
// ConfigSetHandle set configs
func (s *Server) ConfigSetHandle(c *gin.Context) {
var data map[string]interface{}
var data struct {
Title string `json:"title"`
ConsoleTitle string `json:"console_title"`
ProxyURL string `json:"proxy_url"`
Model string `json:"model"`
Temperature float32 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
ChatContextExpireTime int `json:"chat_context_expire_time"`
EnableContext bool `json:"enable_context"`
}
err := json.NewDecoder(c.Request.Body).Decode(&data)
if err != nil {
logger.Errorf("Error decode json data: %s", err.Error())
@@ -27,32 +61,15 @@ func (s *Server) ConfigSetHandle(c *gin.Context) {
return
}
if model, ok := data["model"]; ok {
s.Config.Chat.Model = model.(string)
}
// Temperature
if temperature, ok := data["temperature"]; ok {
s.Config.Chat.Temperature = temperature.(float32)
}
// max_users
if maxTokens, ok := data["max_tokens"]; ok {
s.Config.Chat.MaxTokens = int(maxTokens.(float64))
}
// enable Context
if enableContext, ok := data["enable_context"]; ok {
s.Config.Chat.EnableContext = enableContext.(bool)
}
if expireTime, ok := data["chat_context_expire_time"]; ok {
s.Config.Chat.ChatContextExpireTime = int(expireTime.(float64))
}
// enable auth
if enableAuth, ok := data["enable_auth"]; ok {
s.Config.EnableAuth = enableAuth.(bool)
}
s.Config.Title = data.Title
s.Config.ConsoleTitle = data.ConsoleTitle
s.Config.ProxyURL = strings.Split(data.ProxyURL, ",")
s.Config.Chat.Model = data.Model
s.Config.Chat.Temperature = data.Temperature
s.Config.Chat.MaxTokens = data.MaxTokens
s.Config.Chat.EnableContext = data.EnableContext
s.Config.Chat.ChatContextExpireTime = data.ChatContextExpireTime
if debug, ok := data["debug"]; ok {
s.DebugMode = debug.(bool)
}
// 保存配置文件
err = utils.SaveConfig(s.Config, s.ConfigPath)
if err != nil {

View File

@@ -96,9 +96,10 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
engine.POST("api/img/get", s.GetImgURLHandle)
engine.POST("api/img/set", s.SetImgURLHandle)
engine.POST("api/admin/config", s.ConfigSetHandle)
engine.GET("api/admin/chat-roles/get", s.GetChatRoleListHandle)
engine.GET("api/admin/chat-roles/add", s.AddChatRoleHandle)
engine.GET("api/admin/config/get", s.ConfigGetHandle)
engine.POST("api/admin/config/set", s.ConfigSetHandle)
engine.POST("api/admin/chat-roles/get", s.GetChatRoleListHandle)
engine.POST("api/admin/chat-roles/add", s.AddChatRoleHandle)
engine.POST("api/admin/user/add", s.AddUserHandle)
engine.POST("api/admin/user/batch-add", s.BatchAddUserHandle)
engine.POST("api/admin/user/set", s.SetUserHandle)
@@ -214,49 +215,39 @@ func corsMiddleware() gin.HandlerFunc {
// AuthorizeMiddleware 用户授权验证
func AuthorizeMiddleware(s *Server) gin.HandlerFunc {
return func(c *gin.Context) {
if !s.Config.EnableAuth ||
c.Request.URL.Path == "/api/login" ||
c.Request.URL.Path == "/api/config/chat-roles/get" ||
!strings.HasPrefix(c.Request.URL.Path, "/api") {
c.Next()
return
}
//if strings.HasPrefix(c.Request.URL.Path, "/api/config") {
// accessKey := c.GetHeader("ACCESS-KEY")
// if accessKey != strings.TrimSpace(s.Config.AccessKey) {
// c.Abort()
// c.JSON(http.StatusOK, types.BizVo{Code: types.NotAuthorized, Message: "No Permissions"})
// } else {
c.Next()
//if !s.Config.EnableAuth ||
// c.Request.URL.Path == "/api/login" ||
// c.Request.URL.Path == "/api/config/chat-roles/get" ||
// !strings.HasPrefix(c.Request.URL.Path, "/api") {
// c.Next()
// return
//}
//
//// WebSocket 连接请求验证
//if c.Request.URL.Path == "/api/chat" {
// sessionId := c.Query("sessionId")
// if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
// c.Next()
// } else {
// c.Abort()
// }
// return
//}
// WebSocket 连接请求验证
if c.Request.URL.Path == "/api/chat" {
sessionId := c.Query("sessionId")
if session, ok := s.ChatSession[sessionId]; ok && session.ClientIP == c.ClientIP() {
c.Next()
} else {
c.Abort()
}
return
}
sessionId := c.GetHeader(types.TokenName)
session := sessions.Default(c)
userInfo := session.Get(sessionId)
if userInfo != nil {
c.Set(types.SessionKey, userInfo)
c.Next()
} else {
c.Abort()
c.JSON(http.StatusOK, types.BizVo{
Code: types.NotAuthorized,
Message: "Not Authorized",
})
}
//
//sessionId := c.GetHeader(types.TokenName)
//session := sessions.Default(c)
//userInfo := session.Get(sessionId)
//if userInfo != nil {
// c.Set(types.SessionKey, userInfo)
// c.Next()
//} else {
// c.Abort()
// c.JSON(http.StatusOK, types.BizVo{
// Code: types.NotAuthorized,
// Message: "Not Authorized",
// })
//}
}
}