This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (42 loc) · 1.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"os"
"preventis.io/translationApi/model"
"preventis.io/translationApi/routing"
)
func main() {
dbDialect := getEnv("DATABASE_TYPE", "sqlite3")
dbArgs := getDbArgs(dbDialect)
db := model.StartDB(dbDialect, dbArgs)
defer db.Close()
routing.StartRouter(db)
}
func getDbArgs(dbDialect string) string {
dbHost := getEnv("DATABASE_HOST", "")
dbPort := getEnv("DATABASE_PORT", "")
dbUser := getEnv("DATABASE_USER", "")
dbPassword := getEnv("DATABASE_PASSWORD", "")
dbDatabase := getEnv("DATABASE_NAME", "/tmp/gorm.db")
dbSSL := getEnv("DATABASE_SSL", "")
if dbDialect == "sqlite3" {
return dbDatabase
} else if dbDialect == "mysql" {
if dbSSL == "" {
dbSSL = "false"
}
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local&tls=%s", dbUser, dbPassword, dbHost, dbPort, dbDatabase, dbSSL)
} else if dbDialect == "postgres" {
if dbSSL == "" {
dbSSL = "disable"
}
return fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s", dbHost, dbPort, dbUser, dbDatabase, dbPassword, dbSSL)
}
return ""
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}