mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Merge pull request #107 from YC/legacy-ini
chore: remove legacy config.ini and .env
This commit is contained in:
commit
db4cb92c26
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,6 +1,5 @@
|
||||
launch.json
|
||||
.vscode
|
||||
.env
|
||||
wakapi
|
||||
.idea
|
||||
build
|
||||
@ -8,5 +7,4 @@ build
|
||||
*.db
|
||||
config*.yml
|
||||
!config.default.yml
|
||||
config.ini
|
||||
pkged.go
|
||||
|
@ -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
|
||||
}
|
2
go.mod
2
go.mod
@ -10,7 +10,6 @@ require (
|
||||
github.com/gorilla/schema v1.1.0
|
||||
github.com/gorilla/securecookie v1.1.1
|
||||
github.com/jinzhu/configor v1.2.0
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/markbates/pkger v0.17.1
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
|
||||
@ -22,7 +21,6 @@ require (
|
||||
go.uber.org/atomic v1.6.0
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
||||
gopkg.in/ini.v1 v1.50.0
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
gorm.io/driver/mysql v1.0.3
|
||||
gorm.io/driver/postgres v1.0.5
|
||||
|
3
go.sum
3
go.sum
@ -216,7 +216,6 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
|
||||
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
|
||||
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
@ -554,8 +553,6 @@ gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||
gopkg.in/gorp.v1 v1.7.2 h1:j3DWlAyGVv8whO7AcIWznQ2Yj7yJkn34B8s63GViAAw=
|
||||
gopkg.in/gorp.v1 v1.7.2/go.mod h1:Wo3h+DBQZIxATwftsglhdD/62zRFPhGhTiu5jUJmCaw=
|
||||
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
|
||||
gopkg.in/ini.v1 v1.50.0 h1:c/4YI/GUgB7d2yOkxdsQyYDhW67nWrTl6Zyd9vagYmg=
|
||||
gopkg.in/ini.v1 v1.50.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
|
Loading…
Reference in New Issue
Block a user