Files
honoka-chan/config/json.go
T

54 lines
1.0 KiB
Go

package config
import (
"encoding/json"
"honoka-chan/utils"
"os"
"strconv"
"time"
)
type AppConfigs struct {
AppName string `json:"app_name"`
Settings Settings `json:"settings"`
}
type Settings struct {
SifCdnServer string `json:"sif_cdn_server"`
AsCdnServer string `json:"as_cdn_server"`
}
func DefaultConfigs() *AppConfigs {
return &AppConfigs{
AppName: "honoka-chan",
Settings: Settings{
SifCdnServer: "http://192.168.1.123/static",
AsCdnServer: "http://192.168.1.123/static",
},
}
}
func Load(p string) *AppConfigs {
if !utils.PathExists(p) {
_ = DefaultConfigs().Save(p)
}
c := AppConfigs{}
err := json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
if err != nil {
_ = os.Rename(p, p+".backup"+strconv.FormatInt(time.Now().Unix(), 10))
_ = DefaultConfigs().Save(p)
}
c = AppConfigs{}
_ = json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
return &c
}
func (c *AppConfigs) Save(p string) error {
data, err := json.Marshal(c)
if err != nil {
return err
}
utils.WriteAllText(p, string(data))
return nil
}