fix: viper config saving life cycle in docker volume

This commit is contained in:
Zhang Minghan 2024-01-14 17:01:56 +08:00
parent c77fd946ee
commit 2466672435
5 changed files with 24 additions and 10 deletions

View File

@ -1,9 +1,9 @@
package midjourney package midjourney
import ( import (
"chat/globals"
"chat/utils" "chat/utils"
"fmt" "fmt"
"github.com/spf13/viper"
"strings" "strings"
) )
@ -21,7 +21,7 @@ func (c *ChatInstance) CreateImagineRequest(prompt string) (*ImagineResponse, er
ImagineRequest{ ImagineRequest{
NotifyHook: fmt.Sprintf( NotifyHook: fmt.Sprintf(
"%s/mj/notify", "%s/mj/notify",
viper.GetString("system.general.backend"), globals.NotifyUrl,
), ),
Prompt: prompt, Prompt: prompt,
}, },

View File

@ -263,6 +263,16 @@ func StateAPI(c *gin.Context) {
}) })
} }
func IndexAPI(c *gin.Context) {
username := utils.GetUserFromContext(c)
c.JSON(http.StatusOK, gin.H{
"status": true,
"auth": len(username) != 0,
"method": c.Request.Method,
})
}
func KeyAPI(c *gin.Context) { func KeyAPI(c *gin.Context) {
user := GetUser(c) user := GetUser(c)
if user == nil { if user == nil {

View File

@ -3,6 +3,7 @@ package auth
import "github.com/gin-gonic/gin" import "github.com/gin-gonic/gin"
func Register(app *gin.RouterGroup) { func Register(app *gin.RouterGroup) {
app.Any("/", IndexAPI)
app.POST("/verify", VerifyAPI) app.POST("/verify", VerifyAPI)
app.POST("/reset", ResetAPI) app.POST("/reset", ResetAPI)
app.POST("/register", RegisterAPI) app.POST("/register", RegisterAPI)

View File

@ -1,6 +1,7 @@
package channel package channel
import ( import (
"chat/globals"
"chat/utils" "chat/utils"
"fmt" "fmt"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -15,8 +16,8 @@ type ApiInfo struct {
} }
type generalState struct { type generalState struct {
Title string `json:"title" mapstructure:"title,omitempty"` Title string `json:"title" mapstructure:"title"`
Logo string `json:"logo" mapstructure:"logo,omitempty"` Logo string `json:"logo" mapstructure:"logo"`
Backend string `json:"backend" mapstructure:"backend"` Backend string `json:"backend" mapstructure:"backend"`
File string `json:"file" mapstructure:"file"` File string `json:"file" mapstructure:"file"`
Docs string `json:"docs" mapstructure:"docs"` Docs string `json:"docs" mapstructure:"docs"`
@ -47,18 +48,18 @@ func NewSystemConfig() *SystemConfig {
panic(err) panic(err)
} }
conf.Load()
return conf return conf
} }
func (c *SystemConfig) Load() {
globals.NotifyUrl = c.GetBackend()
}
func (c *SystemConfig) SaveConfig() error { func (c *SystemConfig) SaveConfig() error {
viper.Set("system", c) viper.Set("system", c)
c.Load()
// fix: import cycle not allowed
{
viper.Set("system.general.backend", c.GetBackend())
viper.Set("system.general.title", c.General.Title)
viper.Set("system.general.logo", c.General.Logo)
}
return viper.WriteConfig() return viper.WriteConfig()
} }

View File

@ -16,6 +16,8 @@ var AllowedOrigins = []string{
"fystart.com", "fystart.com",
} }
var NotifyUrl = ""
func OriginIsAllowed(uri string) bool { func OriginIsAllowed(uri string) bool {
instance, _ := url.Parse(uri) instance, _ := url.Parse(uri)
if instance == nil { if instance == nil {