Skip to content

Commit

Permalink
fix: viper config saving life cycle in docker volume
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jan 14, 2024
1 parent c77fd94 commit 2466672
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions adapter/midjourney/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package midjourney

import (
"chat/globals"
"chat/utils"
"fmt"
"github.com/spf13/viper"
"strings"
)

Expand All @@ -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,
},
Expand Down
10 changes: 10 additions & 0 deletions auth/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions auth/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions channel/system.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package channel

import (
"chat/globals"
"chat/utils"
"fmt"
"github.com/spf13/viper"
Expand All @@ -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"`
Expand Down Expand Up @@ -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()
}

Expand Down
2 changes: 2 additions & 0 deletions globals/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var AllowedOrigins = []string{
"fystart.com",
}

var NotifyUrl = ""

func OriginIsAllowed(uri string) bool {
instance, _ := url.Parse(uri)
if instance == nil {
Expand Down

0 comments on commit 2466672

Please sign in to comment.