Reorganize directory structures
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
+63
-30
@@ -1,57 +1,90 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"honoka-chan/utils"
|
||||
"encoding/json"
|
||||
"honoka-chan/pkg/utils"
|
||||
"os"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
"xorm.io/xorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Conf = &AppConfigs{}
|
||||
|
||||
ExampleDb = "assets/data.example.db"
|
||||
MainDb = "assets/main.db"
|
||||
UserDb = "assets/data.db"
|
||||
MainEng *xorm.Engine
|
||||
UserEng *xorm.Engine
|
||||
|
||||
PackageVersion = "97.4.6"
|
||||
|
||||
PrivateKeyPath = "assets/certs/privatekey.pem"
|
||||
PublicKeyPath = "assets/certs/publickey.pem"
|
||||
)
|
||||
|
||||
type AppConfigs struct {
|
||||
AppName string `json:"app_name"`
|
||||
Settings Settings `json:"settings"`
|
||||
UserPrefs UserPrefs `json:"user_prefs"`
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
ListenPort string `json:"listen_port"`
|
||||
SifCdnServer string `json:"sif_cdn_server"`
|
||||
}
|
||||
|
||||
type UserPrefs struct {
|
||||
Name string `json:"name"` // 用户名
|
||||
Level int `json:"level"` // 用户等级
|
||||
ExpNumerator int `json:"exp_numerator"` // Exp 分子
|
||||
ExpDenominator int `json:"exp_denominator"` // Exp 分母
|
||||
GameCoin int `json:"game_coin"` // 游戏金币
|
||||
SnsCoin int `json:"sns_coin"` // 游戏爱心
|
||||
EnergyMax int `json:"energy_max"` // 体力上限
|
||||
OverMaxEnergy int `json:"over_max_energy"` // 实际体力,为 0 时与 EnergyMax 一致
|
||||
InviteCode string `json:"invite_code"` // 用户 ID
|
||||
}
|
||||
|
||||
func init() {
|
||||
Conf = Load("./config.json")
|
||||
}
|
||||
|
||||
_, err := os.Stat(UserDb)
|
||||
if err != nil {
|
||||
utils.WriteAllText(UserDb, utils.ReadAllText(ExampleDb))
|
||||
func DefaultConfigs() *AppConfigs {
|
||||
return &AppConfigs{
|
||||
AppName: "honoka-chan",
|
||||
Settings: Settings{
|
||||
ListenPort: "80",
|
||||
SifCdnServer: "http://192.168.1.123/static",
|
||||
},
|
||||
UserPrefs: UserPrefs{
|
||||
Name: "梦路 @bilibili",
|
||||
Level: 1028,
|
||||
ExpNumerator: 1089696,
|
||||
ExpDenominator: 1207185,
|
||||
GameCoin: 112124104,
|
||||
SnsCoin: 0,
|
||||
EnergyMax: 417,
|
||||
OverMaxEnergy: 0,
|
||||
InviteCode: "377385143",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
eng, err := xorm.NewEngine("sqlite", MainDb)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func Load(p string) *AppConfigs {
|
||||
if !utils.PathExists(p) {
|
||||
_ = DefaultConfigs().Save(p)
|
||||
}
|
||||
err = eng.Ping()
|
||||
c := AppConfigs{}
|
||||
err := json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
_ = os.Rename(p, p+".backup"+strconv.FormatInt(time.Now().Unix(), 10))
|
||||
_ = DefaultConfigs().Save(p)
|
||||
}
|
||||
MainEng = eng
|
||||
MainEng.SetMaxOpenConns(50)
|
||||
MainEng.SetMaxIdleConns(10)
|
||||
c = AppConfigs{}
|
||||
_ = json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
|
||||
return &c
|
||||
}
|
||||
|
||||
eng, err = xorm.NewEngine("sqlite", UserDb)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = eng.Ping()
|
||||
func (c *AppConfigs) Save(p string) error {
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
UserEng = eng
|
||||
UserEng.SetMaxOpenConns(50)
|
||||
MainEng.SetMaxIdleConns(10)
|
||||
utils.WriteAllText(p, string(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user