From caacea3f638ba592cf21cc835d65e9bb40cfc89d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Thu, 7 Nov 2019 22:56:06 +0100 Subject: [PATCH] Add ability to configure database port. --- .gitignore | 3 ++- main.go | 8 +++++++- models/config.go | 1 + utils/common.go | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 14ad0fb..c689ca7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ launch.json .vscode .env -wakapi \ No newline at end of file +wakapi +.idea \ No newline at end of file diff --git a/main.go b/main.go index 477a505..7ab984b 100644 --- a/main.go +++ b/main.go @@ -35,9 +35,14 @@ func readConfig() *models.Config { dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD") dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST") dbName, valid := os.LookupEnv("WAKAPI_DB_NAME") + dbPortStr, valid := os.LookupEnv("WAKAPI_DB_PORT") + dbPort, err := strconv.Atoi(dbPortStr) + if err != nil { + dbPort = 3306 + } if !valid { - log.Fatal("Environment variables missing.") + log.Fatal("Environment variables missing or invalid.") } cfg, err := ini.Load("config.ini") @@ -63,6 +68,7 @@ func readConfig() *models.Config { Port: port, Addr: addr, DbHost: dbHost, + DbPort: uint(dbPort), DbUser: dbUser, DbPassword: dbPassword, DbName: dbName, diff --git a/models/config.go b/models/config.go index 9e91fd7..a5fee7c 100644 --- a/models/config.go +++ b/models/config.go @@ -4,6 +4,7 @@ type Config struct { Port int Addr string DbHost string + DbPort uint DbUser string DbPassword string DbName string diff --git a/utils/common.go b/utils/common.go index 2117fa1..e189bc2 100644 --- a/utils/common.go +++ b/utils/common.go @@ -3,6 +3,7 @@ package utils import ( "errors" "regexp" + "strconv" "strings" "time" @@ -34,6 +35,8 @@ func MakeConnectionString(config *models.Config) string { str.WriteString(config.DbPassword) str.WriteString("@tcp(") str.WriteString(config.DbHost) + str.WriteString(":") + str.WriteString(strconv.Itoa(int(config.DbPort))) str.WriteString(")/") str.WriteString(config.DbName) str.WriteString("?charset=utf8&parseTime=true&loc=")