add http config loader
This commit is contained in:
@@ -5,16 +5,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Static string
|
Static *string
|
||||||
Host string
|
Host *string
|
||||||
Port uint
|
Port uint
|
||||||
Https bool
|
Tls *Tls
|
||||||
HttpsCert string
|
Cors *Cors
|
||||||
HttpKey string
|
}
|
||||||
Cors struct {
|
|
||||||
Mode string
|
type Tls struct {
|
||||||
Whitelist []Whitelist
|
Cert string
|
||||||
}
|
Key string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cors struct {
|
||||||
|
Mode string
|
||||||
|
Whitelist []Whitelist
|
||||||
}
|
}
|
||||||
|
|
||||||
type Whitelist struct {
|
type Whitelist struct {
|
||||||
@@ -26,7 +31,10 @@ type Whitelist struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Config) Address() string {
|
func (h *Config) Address() string {
|
||||||
return fmt.Sprintf("%s:%d", h.Host, h.Port)
|
if h.Host == nil {
|
||||||
|
return h.PortString()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%d", *h.Host, h.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Config) PortString() string {
|
func (h *Config) PortString() string {
|
||||||
|
|||||||
44
providers/http/env.go
Normal file
44
providers/http/env.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/rogeecn/atom/utils/fs"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DefaultPrefix = "HTTP"
|
||||||
|
|
||||||
|
func AutoLoadConfig() *Config {
|
||||||
|
return LoadConfig("", DefaultPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadConfig(file, envPrefix string) *Config {
|
||||||
|
if file == "" {
|
||||||
|
file = "config.toml"
|
||||||
|
}
|
||||||
|
|
||||||
|
if envPrefix == "" {
|
||||||
|
envPrefix = DefaultPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
viper.SetEnvPrefix(envPrefix)
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
|
||||||
|
if !fs.FileExist(file) {
|
||||||
|
return &Config{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// load file
|
||||||
|
viper.SetConfigFile(file)
|
||||||
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var config Config
|
||||||
|
if err := viper.Unmarshal(&config); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config
|
||||||
|
}
|
||||||
@@ -26,8 +26,8 @@ func (e *Service) GetEngine() interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Service) Serve() error {
|
func (e *Service) Serve() error {
|
||||||
if e.conf.Https {
|
if e.conf.Tls != nil {
|
||||||
return e.Engine.RunTLS(e.conf.PortString(), e.conf.HttpsCert, e.conf.HttpKey)
|
return e.Engine.RunTLS(e.conf.PortString(), e.conf.Tls.Cert, e.conf.Tls.Key)
|
||||||
}
|
}
|
||||||
return e.Engine.Run(e.conf.PortString())
|
return e.Engine.Run(e.conf.PortString())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user