mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
chore: remove legacy config.ini and .env
This commit is contained in:
@ -22,8 +22,6 @@ import (
|
||||
|
||||
const (
|
||||
defaultConfigPath = "config.yml"
|
||||
defaultConfigPathLegacy = "config.ini"
|
||||
defaultEnvConfigPathLegacy = ".env"
|
||||
|
||||
SQLDialectMysql = "mysql"
|
||||
SQLDialectPostgres = "postgres"
|
||||
@ -288,8 +286,6 @@ func Load() *Config {
|
||||
|
||||
flag.Parse()
|
||||
|
||||
maybeMigrateLegacyConfig()
|
||||
|
||||
if err := configor.New(&configor.Config{}).Load(config, mustReadConfigLocation()); err != nil {
|
||||
logbuch.Fatal("failed to read config: %v", err)
|
||||
}
|
||||
|
124
config/legacy.go
124
config/legacy.go
@ -1,124 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/joho/godotenv"
|
||||
"gopkg.in/ini.v1"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func maybeMigrateLegacyConfig() {
|
||||
if yes, err := shouldMigrateLegacyConfig(); err != nil {
|
||||
logbuch.Fatal("failed to determine whether to migrate legacy config: %v", err)
|
||||
} else if yes {
|
||||
logbuch.Info("migrating legacy config (%s, %s) to new format (%s); see https://github.com/muety/wakapi/issues/54", defaultConfigPathLegacy, defaultEnvConfigPathLegacy, defaultConfigPath)
|
||||
if err := migrateLegacyConfig(); err != nil {
|
||||
logbuch.Fatal("failed to migrate legacy config: %v", err)
|
||||
}
|
||||
logbuch.Info("config migration successful; please delete %s and %s now", defaultConfigPathLegacy, defaultEnvConfigPathLegacy)
|
||||
}
|
||||
}
|
||||
|
||||
func shouldMigrateLegacyConfig() (bool, error) {
|
||||
if _, err := os.Stat(defaultConfigPath); err == nil {
|
||||
return false, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return true, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func migrateLegacyConfig() error {
|
||||
// step 1: read envVars file parameters
|
||||
envFile, err := os.Open(defaultEnvConfigPathLegacy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
envVars, err := godotenv.Parse(envFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
env := envVars["ENV"]
|
||||
dbType := envVars["WAKAPI_DB_TYPE"]
|
||||
dbUser := envVars["WAKAPI_DB_USER"]
|
||||
dbPassword := envVars["WAKAPI_DB_PASSWORD"]
|
||||
dbHost := envVars["WAKAPI_DB_HOST"]
|
||||
dbName := envVars["WAKAPI_DB_NAME"]
|
||||
dbPortStr := envVars["WAKAPI_DB_PORT"]
|
||||
passwordSalt := envVars["WAKAPI_PASSWORD_SALT"]
|
||||
dbPort, _ := strconv.Atoi(dbPortStr)
|
||||
|
||||
// step 2: read ini file
|
||||
cfg, err := ini.Load(defaultConfigPathLegacy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if dbType == "" {
|
||||
dbType = SQLDialectSqlite
|
||||
}
|
||||
|
||||
dbMaxConn := cfg.Section("database").Key("max_connections").MustUint(2)
|
||||
addr := cfg.Section("server").Key("listen").MustString("127.0.0.1")
|
||||
insecureCookies := cfg.Section("server").Key("insecure_cookies").MustBool(false)
|
||||
port, err := strconv.Atoi(os.Getenv("PORT"))
|
||||
if err != nil {
|
||||
port = cfg.Section("server").Key("port").MustInt()
|
||||
}
|
||||
|
||||
basePathEnv, basePathEnvExists := os.LookupEnv("WAKAPI_BASE_PATH")
|
||||
basePath := cfg.Section("server").Key("base_path").MustString("/")
|
||||
if basePathEnvExists {
|
||||
basePath = basePathEnv
|
||||
}
|
||||
|
||||
// Read custom languages
|
||||
customLangs := make(map[string]string)
|
||||
languageKeys := cfg.Section("languages").Keys()
|
||||
for _, k := range languageKeys {
|
||||
customLangs[k.Name()] = k.MustString("unknown")
|
||||
}
|
||||
|
||||
// step 3: instantiate config
|
||||
config := &Config{
|
||||
Env: env,
|
||||
App: appConfig{
|
||||
CustomLanguages: customLangs,
|
||||
},
|
||||
Security: securityConfig{
|
||||
PasswordSalt: passwordSalt,
|
||||
InsecureCookies: insecureCookies,
|
||||
},
|
||||
Db: dbConfig{
|
||||
Host: dbHost,
|
||||
Port: uint(dbPort),
|
||||
User: dbUser,
|
||||
Password: dbPassword,
|
||||
Name: dbName,
|
||||
Dialect: dbType,
|
||||
MaxConn: dbMaxConn,
|
||||
},
|
||||
Server: serverConfig{
|
||||
Port: port,
|
||||
ListenIpV4: addr,
|
||||
BasePath: basePath,
|
||||
},
|
||||
}
|
||||
|
||||
// step 4: serialize to yaml
|
||||
yamlConfig, err := yaml.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// step 5: write file
|
||||
if err := ioutil.WriteFile(defaultConfigPath, yamlConfig, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user