mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	add API to remove API key, set chat roles
This commit is contained in:
		@@ -4,7 +4,7 @@ build_name:        runner-build
 | 
			
		||||
build_log:         runner-build-errors.log
 | 
			
		||||
valid_ext:         .go, .tpl, .tmpl, .html
 | 
			
		||||
no_rebuild_ext:    .tpl, .tmpl, .html, .js, .vue
 | 
			
		||||
ignored:           assets, tmp, web
 | 
			
		||||
ignored:           assets, tmp, web, .git, .idea, test
 | 
			
		||||
build_delay:       600
 | 
			
		||||
colors:            1
 | 
			
		||||
log_color_main:    cyan
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								main_test.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main_test.go
									
									
									
									
									
								
							@@ -1,11 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestTime(t *testing.T) {
 | 
			
		||||
	fmt.Println(time.Now().Unix())
 | 
			
		||||
}
 | 
			
		||||
@@ -106,6 +106,13 @@ func (s *Server) AddToken(c *gin.Context) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存配置文件
 | 
			
		||||
	err = types.SaveConfig(s.Config, s.ConfigPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Tokens})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -127,11 +134,18 @@ func (s *Server) RemoveToken(c *gin.Context) {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存配置文件
 | 
			
		||||
	err = types.SaveConfig(s.Config, s.ConfigPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Tokens})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AddApiKey 添加一个 API key
 | 
			
		||||
func (s *Server) AddApiKey(c *gin.Context) {
 | 
			
		||||
 | 
			
		||||
	var data map[string]string
 | 
			
		||||
	err := json.NewDecoder(c.Request.Body).Decode(&data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -143,9 +157,45 @@ func (s *Server) AddApiKey(c *gin.Context) {
 | 
			
		||||
		s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, key)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存配置文件
 | 
			
		||||
	err = types.SaveConfig(s.Config, s.ConfigPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RemoveApiKey 移除一个 API key
 | 
			
		||||
func (s *Server) RemoveApiKey(c *gin.Context) {
 | 
			
		||||
	var data map[string]string
 | 
			
		||||
	err := json.NewDecoder(c.Request.Body).Decode(&data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Errorf("Error decode json data: %s", err.Error())
 | 
			
		||||
		c.JSON(http.StatusBadRequest, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if key, ok := data["api_key"]; ok {
 | 
			
		||||
		for i, v := range s.Config.Chat.ApiKeys {
 | 
			
		||||
			if v == key {
 | 
			
		||||
				s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 保存配置文件
 | 
			
		||||
	err = types.SaveConfig(s.Config, s.ConfigPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ListApiKeys 获取 API key 列表
 | 
			
		||||
func (s *Server) ListApiKeys(c *gin.Context) {
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
 | 
			
		||||
}
 | 
			
		||||
@@ -154,7 +204,7 @@ func (s *Server) GetChatRoles(c *gin.Context) {
 | 
			
		||||
	var rolesOrder = []string{"gpt", "programmer", "teacher", "artist", "philosopher", "lu-xun", "english_trainer", "seller"}
 | 
			
		||||
	var roles = make([]interface{}, 0)
 | 
			
		||||
	for _, k := range rolesOrder {
 | 
			
		||||
		if v, ok := s.Config.ChatRoles[k]; ok {
 | 
			
		||||
		if v, ok := s.Config.ChatRoles[k]; ok && v.Enable {
 | 
			
		||||
			roles = append(roles, struct {
 | 
			
		||||
				Key  string `json:"key"`
 | 
			
		||||
				Name string `json:"name"`
 | 
			
		||||
@@ -168,3 +218,53 @@ func (s *Server) GetChatRoles(c *gin.Context) {
 | 
			
		||||
	}
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: roles})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateChatRole 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作
 | 
			
		||||
func (s *Server) UpdateChatRole(c *gin.Context) {
 | 
			
		||||
	var data map[string]string
 | 
			
		||||
	err := json.NewDecoder(c.Request.Body).Decode(&data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Errorf("Error decode json data: %s", err.Error())
 | 
			
		||||
		c.JSON(http.StatusBadRequest, nil)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	key := data["key"]
 | 
			
		||||
	if key == "" {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Must specified the role key"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	role := s.Config.ChatRoles[key]
 | 
			
		||||
	if enable, ok := data["enable"]; ok {
 | 
			
		||||
		v, err := strconv.ParseBool(enable)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			c.JSON(http.StatusOK, types.BizVo{
 | 
			
		||||
				Code:    types.InvalidParams,
 | 
			
		||||
				Message: "enable must be a bool parameter",
 | 
			
		||||
			})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		role.Enable = v
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if name, ok := data["name"]; ok {
 | 
			
		||||
		role.Name = name
 | 
			
		||||
	}
 | 
			
		||||
	if helloMsg, ok := data["hello_msg"]; ok {
 | 
			
		||||
		role.HelloMsg = helloMsg
 | 
			
		||||
	}
 | 
			
		||||
	if icon, ok := data["icon"]; ok {
 | 
			
		||||
		role.Icon = icon
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	s.Config.ChatRoles[key] = role
 | 
			
		||||
 | 
			
		||||
	// 保存配置文件
 | 
			
		||||
	err = types.SaveConfig(s.Config, s.ConfigPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,9 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
 | 
			
		||||
	engine.POST("api/config/token/add", s.AddToken)
 | 
			
		||||
	engine.POST("api/config/token/remove", s.RemoveToken)
 | 
			
		||||
	engine.POST("api/config/apikey/add", s.AddApiKey)
 | 
			
		||||
	engine.POST("api/config/apikey/remove", s.RemoveApiKey)
 | 
			
		||||
	engine.POST("api/config/apikey/list", s.ListApiKeys)
 | 
			
		||||
	engine.POST("api/config/role/set", s.UpdateChatRole)
 | 
			
		||||
 | 
			
		||||
	engine.NoRoute(func(c *gin.Context) {
 | 
			
		||||
		if c.Request.URL.Path == "/favicon.ico" {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								test/test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								test/test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var data = make(map[string]string)
 | 
			
		||||
	fmt.Println(data["key"] == "")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user