to options mode
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const ConfigPrefix = "Http"
|
||||
|
||||
type Config struct {
|
||||
Static *string
|
||||
Host *string
|
||||
|
||||
@@ -3,42 +3,29 @@ package http
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/rogeecn/atom/utils/fs"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "HTTP"
|
||||
const DefaultPrefix = "Http"
|
||||
|
||||
func AutoLoadConfig() *Config {
|
||||
return LoadConfig("", DefaultPrefix)
|
||||
return LoadConfig("")
|
||||
}
|
||||
|
||||
func LoadConfig(file, envPrefix string) *Config {
|
||||
func LoadConfig(file string) *Config {
|
||||
if file == "" {
|
||||
file = "config.toml"
|
||||
}
|
||||
|
||||
if envPrefix == "" {
|
||||
envPrefix = DefaultPrefix
|
||||
}
|
||||
|
||||
v := viper.NewWithOptions(viper.KeyDelimiter("_"))
|
||||
|
||||
v.SetEnvPrefix(envPrefix)
|
||||
v.AutomaticEnv()
|
||||
|
||||
if !fs.FileExist(file) {
|
||||
return &Config{}
|
||||
}
|
||||
|
||||
// load file
|
||||
v.SetConfigFile(file)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var config Config
|
||||
if err := v.Unmarshal(&config); err != nil {
|
||||
if err := v.UnmarshalKey(DefaultPrefix, &config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/rogeecn/atom/providers"
|
||||
"github.com/rogeecn/atom/providers/http"
|
||||
"github.com/rogeecn/atom/providers/log"
|
||||
"go.uber.org/dig"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -32,7 +32,12 @@ func (e *Service) Serve() error {
|
||||
return e.Engine.Run(e.conf.PortString())
|
||||
}
|
||||
|
||||
func Provide(cfg *http.Config, opts ...dig.ProvideOption) error {
|
||||
func Provide(o *providers.Options) error {
|
||||
var config http.Config
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (http.Service, error) {
|
||||
gin.DefaultWriter = log.LevelWriter{Level: log.InfoLevel}
|
||||
gin.DefaultErrorWriter = log.LevelWriter{Level: log.ErrorLevel}
|
||||
@@ -53,6 +58,6 @@ func Provide(cfg *http.Config, opts ...dig.ProvideOption) error {
|
||||
)
|
||||
}))
|
||||
|
||||
return &Service{Engine: engine, conf: cfg}, nil
|
||||
}, opts...)
|
||||
return &Service{Engine: engine, conf: &config}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user