Report configuration and HTTP startup failures
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
+33
-15
@@ -2,7 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"honoka-chan/pkg/utils"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@@ -28,8 +28,13 @@ type Settings struct {
|
|||||||
UnlockAllSpecialRotation bool `json:"unlock_all_special_rotation"`
|
UnlockAllSpecialRotation bool `json:"unlock_all_special_rotation"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig() {
|
func InitConfig() error {
|
||||||
Conf = Load("./config.json")
|
conf, err := Load("./config.json")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Conf = conf
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultConfigs() *AppConfigs {
|
func DefaultConfigs() *AppConfigs {
|
||||||
@@ -43,19 +48,33 @@ func DefaultConfigs() *AppConfigs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load(p string) *AppConfigs {
|
func Load(p string) (*AppConfigs, error) {
|
||||||
if !utils.PathExists(p) {
|
data, err := os.ReadFile(p)
|
||||||
_ = DefaultConfigs().Save(p)
|
if os.IsNotExist(err) {
|
||||||
|
conf := DefaultConfigs()
|
||||||
|
if err := conf.Save(p); err != nil {
|
||||||
|
return nil, fmt.Errorf("create default config: %w", err)
|
||||||
|
}
|
||||||
|
return conf, nil
|
||||||
}
|
}
|
||||||
c := AppConfigs{}
|
|
||||||
err := json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = os.Rename(p, p+".backup"+strconv.FormatInt(time.Now().Unix(), 10))
|
return nil, fmt.Errorf("read config: %w", err)
|
||||||
_ = DefaultConfigs().Save(p)
|
|
||||||
}
|
}
|
||||||
c = AppConfigs{}
|
|
||||||
_ = json.Unmarshal([]byte(utils.ReadAllText(p)), &c)
|
c := AppConfigs{}
|
||||||
return &c
|
if err := json.Unmarshal(data, &c); err == nil {
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
backup := p + ".backup" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
if err := os.Rename(p, backup); err != nil {
|
||||||
|
return nil, fmt.Errorf("backup invalid config: %w", err)
|
||||||
|
}
|
||||||
|
conf := DefaultConfigs()
|
||||||
|
if err := conf.Save(p); err != nil {
|
||||||
|
return nil, fmt.Errorf("write default config: %w", err)
|
||||||
|
}
|
||||||
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AppConfigs) Save(p string) error {
|
func (c *AppConfigs) Save(p string) error {
|
||||||
@@ -63,6 +82,5 @@ func (c *AppConfigs) Save(p string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
utils.WriteAllText(p, string(data)+"\n")
|
return os.WriteFile(p, append(data, '\n'), 0644)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadCreatesDefaultConfig(t *testing.T) {
|
||||||
|
path := filepath.Join(t.TempDir(), "config.json")
|
||||||
|
|
||||||
|
conf, err := Load(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load: %v", err)
|
||||||
|
}
|
||||||
|
if conf.Settings.ListenPort != "8080" {
|
||||||
|
t.Fatalf("ListenPort = %q, want 8080", conf.Settings.ListenPort)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
t.Fatalf("default config was not written: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadBacksUpInvalidConfig(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "config.json")
|
||||||
|
if err := os.WriteFile(path, []byte("{"), 0644); err != nil {
|
||||||
|
t.Fatalf("write invalid config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conf, err := Load(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Load: %v", err)
|
||||||
|
}
|
||||||
|
if conf.Settings.ListenPort != "8080" {
|
||||||
|
t.Fatalf("ListenPort = %q, want 8080", conf.Settings.ListenPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ReadDir: %v", err)
|
||||||
|
}
|
||||||
|
for _, entry := range entries {
|
||||||
|
if strings.HasPrefix(entry.Name(), "config.json.backup") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal("invalid config backup was not created")
|
||||||
|
}
|
||||||
@@ -17,7 +17,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 初始化配置
|
// 初始化配置
|
||||||
config.InitConfig()
|
if err := config.InitConfig(); err != nil {
|
||||||
|
log.Fatalf("初始化配置失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化数据库表和用户数据
|
// 初始化数据库表和用户数据
|
||||||
startup.StartUp()
|
startup.StartUp()
|
||||||
@@ -54,5 +56,7 @@ func main() {
|
|||||||
// SIF
|
// SIF
|
||||||
router.SifRouter(r)
|
router.SifRouter(r)
|
||||||
|
|
||||||
r.Run(":" + config.Conf.Settings.ListenPort) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
if err := r.Run(":" + config.Conf.Settings.ListenPort); err != nil {
|
||||||
|
log.Fatalf("HTTP 服务启动失败: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user