mirror of
https://github.com/makayabou/asg-server.git
synced 2026-05-02 17:43:36 +02:00
Improved: simplify config loading
This commit is contained in:
parent
26cced8641
commit
3da33e1512
3
go.mod
3
go.mod
@ -5,10 +5,12 @@ go 1.20
|
||||
require (
|
||||
firebase.google.com/go/v4 v4.12.1
|
||||
github.com/go-playground/validator/v10 v10.15.5
|
||||
github.com/go-sql-driver/mysql v1.7.1
|
||||
github.com/gofiber/contrib/fiberzap/v2 v2.1.1
|
||||
github.com/gofiber/fiber/v2 v2.50.0
|
||||
github.com/jaevor/go-nanoid v1.3.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/nyaruka/phonenumbers v1.1.8
|
||||
github.com/valyala/fasthttp v1.50.0
|
||||
go.uber.org/fx v1.20.1
|
||||
@ -34,7 +36,6 @@ require (
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@ -96,6 +96,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
||||
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
|
||||
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
|
||||
@ -1,31 +1,25 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/capcom6/sms-gateway/internal/infra/config"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/db"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/http"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/services"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var Module = fx.Module(
|
||||
"appconfig",
|
||||
fx.Provide(
|
||||
fx.Annotate(
|
||||
func() any {
|
||||
return &defaultConfig
|
||||
},
|
||||
fx.ResultTags(`name:"config:source"`),
|
||||
),
|
||||
func(log *zap.Logger) Config {
|
||||
if err := config.LoadConfig(&defaultConfig); err != nil {
|
||||
log.Error("Error loading config", zap.Error(err))
|
||||
}
|
||||
|
||||
return defaultConfig
|
||||
},
|
||||
),
|
||||
fx.Provide(
|
||||
fx.Annotate(
|
||||
func(cfg any) Config {
|
||||
return *cfg.(*Config)
|
||||
},
|
||||
fx.ParamTags(`name:"config:result"`),
|
||||
),
|
||||
),
|
||||
// fx.Provide(GetConfig),
|
||||
fx.Provide(func(cfg Config) http.Config {
|
||||
return http.Config{
|
||||
Listen: cfg.HTTP.Listen,
|
||||
|
||||
@ -5,19 +5,28 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"go.uber.org/zap"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
}
|
||||
|
||||
func New(params Param) any {
|
||||
func LoadConfig(config any) error {
|
||||
err := godotenv.Load()
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
params.Logger.Error("Error loading .env file", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
if err := loadFromYaml(config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := loadFromEnv(config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromYaml(config any) error {
|
||||
configPath := "config.yml"
|
||||
if envPath := os.Getenv("CONFIG_PATH"); envPath != "" {
|
||||
configPath = envPath
|
||||
@ -25,13 +34,16 @@ func New(params Param) any {
|
||||
|
||||
yamlFile, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
params.Logger.Error("Error reading config file", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(yamlFile, params.Config)
|
||||
if err != nil {
|
||||
params.Logger.Error("Error unmarshalling config file", zap.Error(err))
|
||||
if err := yaml.Unmarshal(yamlFile, config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return params.Config
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromEnv(config any) error {
|
||||
return envconfig.Process("", config)
|
||||
}
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var Module = fx.Module(
|
||||
"config",
|
||||
fx.Decorate(func(log *zap.Logger) *zap.Logger {
|
||||
return log.Named("config")
|
||||
}),
|
||||
fx.Provide(
|
||||
fx.Annotate(
|
||||
New,
|
||||
fx.ResultTags(`name:"config:result"`),
|
||||
),
|
||||
),
|
||||
)
|
||||
@ -1,13 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Param struct {
|
||||
fx.In
|
||||
|
||||
Logger *zap.Logger
|
||||
Config any `name:"config:source"`
|
||||
}
|
||||
@ -3,7 +3,6 @@ package smsgateway
|
||||
import (
|
||||
appconfig "github.com/capcom6/sms-gateway/internal/config"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/cli"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/config"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/db"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/http"
|
||||
"github.com/capcom6/sms-gateway/internal/infra/logger"
|
||||
@ -22,7 +21,6 @@ var Module = fx.Module(
|
||||
"server",
|
||||
cli.Module,
|
||||
appconfig.Module,
|
||||
config.Module,
|
||||
logger.Module,
|
||||
http.Module,
|
||||
validator.Module,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user