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

Add ability to configure database port.

This commit is contained in:
Ferdinand Mütsch 2019-11-07 22:56:06 +01:00
parent 9e726028c3
commit caacea3f63
4 changed files with 13 additions and 2 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ launch.json
.vscode
.env
wakapi
.idea

View File

@ -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,

View File

@ -4,6 +4,7 @@ type Config struct {
Port int
Addr string
DbHost string
DbPort uint
DbUser string
DbPassword string
DbName string

View File

@ -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=")