2023-06-16 07:42:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/lib/pq"
|
2024-02-10 16:47:14 +00:00
|
|
|
"holiday-api/db"
|
2023-06-16 07:42:50 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func envMustExist(env string) string {
|
|
|
|
if value, exists := os.LookupEnv(env); !exists {
|
|
|
|
panic(fmt.Sprintf("env variable '%s' not defined", env))
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 09:09:54 +00:00
|
|
|
func envOrDefault(env string, defaultValue string) string {
|
|
|
|
if value, exists := os.LookupEnv(env); exists {
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 07:42:50 +00:00
|
|
|
func connectToDb() (*sqlx.DB, error) {
|
|
|
|
host := envMustExist("PSQL_HOST")
|
|
|
|
port := envMustExist("PSQL_PORT")
|
|
|
|
user := envMustExist("PSQL_USER")
|
|
|
|
password := envMustExist("PSQL_PASSWORD")
|
|
|
|
dbname := envMustExist("PSQL_DB")
|
2024-04-01 09:09:54 +00:00
|
|
|
sslMode := envOrDefault("PSQL_SSLMODE", "disable")
|
|
|
|
schema := envOrDefault("PSQL_SCHEMA", "public")
|
2023-06-16 07:42:50 +00:00
|
|
|
|
2024-04-01 09:09:54 +00:00
|
|
|
return db.ConnectToDbNamed(host, port, user, password, dbname, sslMode, schema)
|
2023-06-16 07:42:50 +00:00
|
|
|
}
|