A config/config.go => config/config.go +34 -0
@@ 0,0 1,34 @@
+package config
+
+import (
+ "log"
+ "tixe/util"
+)
+
+var TixeConfig *Config
+
+type Config struct {
+ PsqlHost string
+ PsqlPort string
+ PsqlUser string
+ PsqlPwd string
+ PsqlDb string
+
+ OidcGithub bool
+ OidcCustom bool
+}
+
+func ParseConfig() {
+ log.Print("[tixe/config] Parsing config")
+
+ TixeConfig = &Config{
+ PsqlHost: util.LoadVar("TIXE_PSQL_HOST", ""),
+ PsqlPort: util.LoadVar("TIXE_PSQL_PORT", "5432"),
+ PsqlUser: util.LoadVar("TIXE_PSQL_USER", ""),
+ PsqlPwd: util.LoadVar("TIXE_PSQL_PASSWORD", ""),
+ PsqlDb: util.LoadVar("TIXE_PSQL_DB", ""),
+
+ OidcGithub: util.LoadVarBool("TIXE_OIDC_GITHUB", false),
+ OidcCustom: util.LoadVarBool("TIXE_OIDC_CUSTOM", false),
+ }
+}
M db/db.go => db/db.go +6 -6
@@ 5,7 5,7 @@ import (
"fmt"
"log"
"time"
- "tixe/util"
+ "tixe/config"
"github.com/jackc/pgx/v5/pgxpool"
)
@@ 33,11 33,11 @@ func NewPgPool() {
}
func pgConnectionString() string {
- user := util.LoadVar("TIXE_PSQL_USER")
- pwd := util.LoadVar("TIXE_PSQL_PASSWORD")
- host := util.LoadVar("TIXE_PSQL_HOST")
- port := util.LoadVar("TIXE_PSQL_PORT")
- db := util.LoadVar("TIXE_PSQL_DB")
+ user := config.TixeConfig.PsqlUser
+ pwd := config.TixeConfig.PsqlPwd
+ host := config.TixeConfig.PsqlHost
+ port := config.TixeConfig.PsqlPort
+ db := config.TixeConfig.PsqlDb
return fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", user, pwd, host, port, db)
}
M tixe.go => tixe.go +3 -0
@@ 4,6 4,7 @@ import (
"log"
"net/http"
"tixe/api"
+ "tixe/config"
"tixe/db"
"tixe/template"
@@ 44,6 45,8 @@ func setupRouter() *gin.Engine {
func main() {
log.Print("[tixe] Starting up...")
+ config.ParseConfig()
+
db.NewPgPool()
defer db.PgPool.Close()
db.RunMigrations()
M util/env.go => util/env.go +26 -5
@@ 3,12 3,33 @@ package util
import (
"log"
"os"
+ "strconv"
)
-func LoadVar(key string) string {
- value := os.Getenv(key)
- if key == "" {
- log.Fatalf("[tixe/util] Environment variable %s is empty!", key)
+func LoadVar(key string, def string) string {
+ value, found := os.LookupEnv(key)
+ if found {
+ return value
}
- return value
+
+ if def == "" {
+ log.Fatalf("[tixe/util] Environment variable %s is empty, and has no default!", key)
+ } else {
+ log.Printf("[tixe/util] Environment variable %s is empty, using default '%s'", key, def)
+ }
+ return def
+}
+
+func LoadVarBool(key string, def bool) bool {
+ value, found := os.LookupEnv(key)
+ if found {
+ res, err := strconv.ParseBool(value)
+ if err != nil {
+ log.Fatalf("[tixe/util] Environment variable %s failed to parse to bool from '%s'", key, value)
+ }
+ return res
+ }
+
+ log.Printf("[tixe/util] Environment variable %s is empty, using default '%t'", key, def)
+ return def
}