2023-06-15 09:41:30 +08:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"chatplus/core/types"
|
2023-06-19 07:06:59 +08:00
|
|
|
logger2 "chatplus/logger"
|
2023-06-15 09:41:30 +08:00
|
|
|
"chatplus/utils"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-06-19 07:06:59 +08:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2023-06-15 09:41:30 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 07:06:59 +08:00
|
|
|
var logger = logger2.GetLogger()
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
func NewDefaultConfig() *types.AppConfig {
|
|
|
|
return &types.AppConfig{
|
2023-07-02 20:51:13 +08:00
|
|
|
Listen: "0.0.0.0:5678",
|
|
|
|
ProxyURL: "",
|
|
|
|
Manager: types.Manager{Username: "admin", Password: "admin123"},
|
|
|
|
StaticDir: "./static",
|
|
|
|
StaticUrl: "http://localhost/5678/static",
|
|
|
|
Redis: types.RedisConfig{Host: "localhost", Port: 6379, Password: ""},
|
|
|
|
AesEncryptKey: utils.RandString(24),
|
2023-06-15 09:41:30 +08:00
|
|
|
Session: types.Session{
|
2023-06-27 18:29:46 +08:00
|
|
|
Driver: types.SessionDriverCookie,
|
2023-06-15 09:41:30 +08:00
|
|
|
SecretKey: utils.RandString(64),
|
2023-06-19 07:06:59 +08:00
|
|
|
Name: "CHAT_PLUS_SESSION",
|
2023-06-15 09:41:30 +08:00
|
|
|
Domain: "",
|
|
|
|
Path: "/",
|
|
|
|
MaxAge: 86400,
|
|
|
|
Secure: true,
|
|
|
|
HttpOnly: false,
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
},
|
2023-08-10 17:24:23 +08:00
|
|
|
ApiConfig: types.ChatPlusApiConfig{},
|
|
|
|
ChatPlusExtApiToken: utils.RandString(32),
|
2023-06-15 09:41:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(configFile string) (*types.AppConfig, error) {
|
|
|
|
var config *types.AppConfig
|
|
|
|
_, err := os.Stat(configFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Info("creating new config file: ", configFile)
|
|
|
|
config = NewDefaultConfig()
|
|
|
|
config.Path = configFile
|
|
|
|
// save config
|
|
|
|
err := SaveConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
_, err = toml.DecodeFile(configFile, &config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveConfig(config *types.AppConfig) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
encoder := toml.NewEncoder(buf)
|
|
|
|
if err := encoder.Encode(&config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.WriteFile(config.Path, buf.Bytes(), 0644)
|
|
|
|
}
|