As of https://github.com/YumeMichi/elichika/commit/81a78e964dcdb8bb2d123237f966f95e7fba190e Signed-off-by: Yuan Si <do4suki@gmail.com>
54 lines
1.0 KiB
Go
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
|
|
}
|