新增接口开启调试模式

This commit is contained in:
RockYang 2023-03-28 12:12:41 +08:00
parent 1e8aaff193
commit 95f9dfa9cb
3 changed files with 32 additions and 28 deletions

View File

@ -1,31 +1,19 @@
SHELL=/usr/bin/env bash
NAME := wechatGPT
all: window_x86 window_amd64 linux_x86 linux_amd64 mac_x86 mac_64
all: window linux darwin
window_x86:
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -o bin/$(NAME).exe main.go
.PHONY: window_x86
window_amd64:
window:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/$(NAME)-amd64.exe main.go
.PHONY: window_amd64
.PHONY: window
linux_x86:
CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o bin/$(NAME)-386-linux main.go
.PHONY: linux_x86
linux_amd64:
linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/$(NAME)-amd64-linux main.go
.PHONY: linux_amd64
.PHONY: linux
mac_x86:
CGO_ENABLED=1 GOOS=darwin GOARCH=386 go build -o bin/$(NAME)-386-darwin main.go
.PHONY: mac_x86
mac_64:
darwin:
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o bin/$(NAME)-amd64-darwin main.go
.PHONY: mac_64
.PHONY: darwin
clean:
rm -rf bin/$(NAME)-*

View File

@ -86,13 +86,27 @@ func (s *Server) ConfigSetHandle(c *gin.Context) {
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg})
}
// SetDebug 开启/关闭调试模式
func (s *Server) SetDebug(c *gin.Context) {
var data struct {
Debug bool `json:"debug"`
}
err := json.NewDecoder(c.Request.Body).Decode(&data)
if err != nil {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
return
}
s.DebugMode = data.Debug
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg})
}
// AddToken 添加 Token
func (s *Server) AddToken(c *gin.Context) {
var data types.Token
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)
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
return
}
@ -151,13 +165,10 @@ func (s *Server) SetToken(c *gin.Context) {
var data types.Token
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)
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
return
}
logger.Info(data)
// 参数处理
if data.Name == "" || data.MaxCalls < 0 {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
@ -170,7 +181,12 @@ func (s *Server) SetToken(c *gin.Context) {
return
}
token.RemainingCalls += data.MaxCalls - token.MaxCalls
if data.MaxCalls > 0 {
token.RemainingCalls += data.MaxCalls - token.MaxCalls
if token.RemainingCalls < 0 {
token.RemainingCalls = 0
}
}
token.MaxCalls = data.MaxCalls
err = PutToken(*token)
@ -187,8 +203,7 @@ func (s *Server) RemoveToken(c *gin.Context) {
var data types.Token
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)
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid args"})
return
}

View File

@ -95,6 +95,7 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
engine.POST("api/config/role/set", s.UpdateChatRole)
engine.POST("api/config/proxy/add", s.AddProxy)
engine.POST("api/config/proxy/remove", s.RemoveProxy)
engine.POST("api/config/debug", s.SetDebug)
engine.NoRoute(func(c *gin.Context) {
if c.Request.URL.Path == "/favicon.ico" {