1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: proper parsing of environment variables

This commit is contained in:
Ferdinand Mütsch 2020-04-26 13:55:19 +02:00
parent 7b486b390e
commit 91a4bb2db3
4 changed files with 22 additions and 15 deletions

2
go.mod
View File

@ -14,8 +14,6 @@ require (
github.com/rs/cors v1.7.0 github.com/rs/cors v1.7.0
github.com/rubenv/sql-migrate v0.0.0-20200402132117-435005d389bc github.com/rubenv/sql-migrate v0.0.0-20200402132117-435005d389bc
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/t-tiger/gorm-bulk-insert v0.0.0-20191014134946-beb77b81825f github.com/t-tiger/gorm-bulk-insert v0.0.0-20191014134946-beb77b81825f
google.golang.org/appengine v1.6.5 // indirect
gopkg.in/ini.v1 v1.50.0 gopkg.in/ini.v1 v1.50.0
) )

19
main.go
View File

@ -40,20 +40,15 @@ func readConfig() *models.Config {
log.Fatal(err) log.Fatal(err)
} }
// TODO: Use jinzhu/configor or so env := utils.LookupFatal("ENV")
env, _ := os.LookupEnv("ENV") dbType := utils.LookupFatal("WAKAPI_DB_TYPE")
dbType, valid := os.LookupEnv("WAKAPI_DB_TYPE") dbUser := utils.LookupFatal("WAKAPI_DB_USER")
dbUser, valid := os.LookupEnv("WAKAPI_DB_USER") dbPassword := utils.LookupFatal("WAKAPI_DB_PASSWORD")
dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD") dbHost := utils.LookupFatal("WAKAPI_DB_HOST")
dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST") dbName := utils.LookupFatal("WAKAPI_DB_NAME")
dbName, valid := os.LookupEnv("WAKAPI_DB_NAME") dbPortStr := utils.LookupFatal("WAKAPI_DB_PORT")
dbPortStr, valid := os.LookupEnv("WAKAPI_DB_PORT")
dbPort, err := strconv.Atoi(dbPortStr) dbPort, err := strconv.Atoi(dbPortStr)
if !valid {
log.Fatal("Environment variables missing or invalid.")
}
cfg, err := ini.Load("config.ini") cfg, err := ini.Load("config.ini")
if err != nil { if err != nil {
log.Fatalf("Fail to read file: %v", err) log.Fatalf("Fail to read file: %v", err)

14
utils/environment.go Normal file
View File

@ -0,0 +1,14 @@
package utils
import (
"log"
"os"
)
func LookupFatal(key string) string {
v, ok := os.LookupEnv(key)
if !ok {
log.Fatalf("missing env variable '%s'", key)
}
return v
}

View File

@ -1 +1 @@
1.2.0 1.2.1