add saving cfg

This commit is contained in:
shoopea 2024-11-17 16:25:39 +01:00
parent e1806fd27a
commit 3bcd6664a6
5 changed files with 51 additions and 4 deletions

View File

@ -113,6 +113,18 @@ func (a *AdminConfig) Run() {
}) })
}) })
r.GET("/save", func(c *gin.Context) {
if err := cfg.Save(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "done",
})
}
})
fsys, _ := fs.Sub(assets, "assets/static") fsys, _ := fs.Sub(assets, "assets/static")
r.StaticFS("/assets", http.FS(fsys)) r.StaticFS("/assets", http.FS(fsys))

View File

@ -13,6 +13,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tailscale/hujson" "github.com/tailscale/hujson"
"github.com/tidwall/pretty"
) )
type Config struct { type Config struct {
@ -180,6 +181,37 @@ func LoadConfigByte(conf []byte) (*Config, error) {
return c, nil return c, nil
} }
// Save config
func (c *Config) Save() error {
log.WithFields(log.Fields{}).Debugf("starting")
defer log.WithFields(log.Fields{}).Debugf("done")
cfgMx.Lock()
defer cfgMx.Unlock()
b, err := json.Marshal(cfg)
if err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
r := pretty.Pretty(b)
f, err := os.Open(*cfgFile)
if err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
if _, err := f.Write(r); err != nil {
log.WithFields(log.Fields{"error": err}).Errorf("")
return err
}
return nil
}
// Run config // Run config
func (c *Config) Run() { func (c *Config) Run() {
log.WithFields(log.Fields{}).Debugf("starting") log.WithFields(log.Fields{}).Debugf("starting")

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/silenceper/pool v1.0.0 github.com/silenceper/pool v1.0.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
github.com/tidwall/pretty v1.2.1
golang.org/x/crypto v0.9.0 golang.org/x/crypto v0.9.0
) )

2
go.sum
View File

@ -71,6 +71,8 @@ github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gt
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw= github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw=
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8= github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=

View File

@ -1,7 +1,7 @@
// Code generated by version.sh (@generated) DO NOT EDIT. // Code generated by version.sh (@generated) DO NOT EDIT.
package main package main
var githash = "26c324c" var githash = "e1806fd"
var branch = "master" var branch = "master"
var buildstamp = "2024-11-17_14:13:25" var buildstamp = "2024-11-17_15:23:14"
var commits = "88" var commits = "89"
var version = "26c324c-b88 - 2024-11-17_14:13:25" var version = "e1806fd-b89 - 2024-11-17_15:23:14"