improve config setup

improve config setup
This commit is contained in:
Ruidy 2024-08-21 09:48:47 +02:00
parent 47ee4f2168
commit f90b8a1c67
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 29 additions and 3 deletions

28
config/config.go Normal file
View file

@ -0,0 +1,28 @@
package config
import (
"os"
"github.com/joho/godotenv"
"github.com/labstack/gommon/log"
)
type Config map[string]string
var DefaultConfig = Config{
"PORT": "8000",
}
func NewConfig() func(string) string {
_ = godotenv.Load()
log.Info("loaded env variables")
return func(key string) string {
if value := os.Getenv(key); value != "" {
return value
} else {
log.Warnf("no value found for %s using defaults", key)
return DefaultConfig[key]
}
}
}

View file

@ -11,7 +11,6 @@ import (
"strings"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -28,9 +27,8 @@ var static embed.FS
func main() {
ctx := context.Background()
_ = godotenv.Load()
if err := run(ctx, os.Getenv); err != nil {
if err := run(ctx, config.NewConfig()); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}