mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
improve config setup
improve config setup
This commit is contained in:
parent
47ee4f2168
commit
f90b8a1c67
2 changed files with 29 additions and 3 deletions
28
config/config.go
Normal file
28
config/config.go
Normal 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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
main.go
4
main.go
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
|
@ -28,9 +27,8 @@ var static embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
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)
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue