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
import (
"chat/globals"
"chat/utils"
"fmt"
"github.com/spf13/viper"
"strings"
)
@ -21,7 +21,7 @@ func (c *ChatInstance) CreateImagineRequest(prompt string) (*ImagineResponse, er
ImagineRequest{
NotifyHook: fmt.Sprintf(
"%s/mj/notify",
viper.GetString("system.general.backend"),
globals.NotifyUrl,
),
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) {
user := GetUser(c)
if user == nil {

View File

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

View File

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

View File

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