init project
This commit is contained in:
100
providers/config/loader.go
Normal file
100
providers/config/loader.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"app/container"
|
||||
"app/utils"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-micro/plugins/v4/config/encoder/toml"
|
||||
"github.com/go-micro/plugins/v4/config/source/etcd"
|
||||
"github.com/rogeecn/fabfile"
|
||||
"go-micro.dev/v4/config"
|
||||
"go-micro.dev/v4/config/reader"
|
||||
"go-micro.dev/v4/config/reader/json"
|
||||
"go-micro.dev/v4/config/source"
|
||||
"go-micro.dev/v4/config/source/env"
|
||||
"go-micro.dev/v4/config/source/file"
|
||||
"go-micro.dev/v4/logger"
|
||||
)
|
||||
|
||||
var c *Config
|
||||
|
||||
type Config struct {
|
||||
App App
|
||||
Http Http
|
||||
Log Log
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(Load); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
var err error
|
||||
confFile := utils.ShareConfigFile
|
||||
if confFile == "" {
|
||||
confFile, err = fabfile.Find("config.toml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
options := []config.Option{}
|
||||
|
||||
options = append(options, config.WithSource(file.NewSource(
|
||||
file.WithPath(confFile),
|
||||
source.WithEncoder(toml.NewEncoder()),
|
||||
)))
|
||||
|
||||
etcdEndpoints := etcdEndpoints()
|
||||
if len(etcdEndpoints) > 0 {
|
||||
logger.Info("etcd endpoints: ", etcdEndpoints, len(etcdEndpoints))
|
||||
options = append(options, config.WithSource(etcd.NewSource(
|
||||
etcd.WithAddress(etcdEndpoints...),
|
||||
etcd.WithPrefix("/micro/config/api.web"),
|
||||
etcd.StripPrefix(true),
|
||||
)))
|
||||
}
|
||||
|
||||
options = append(options, config.WithSource(env.NewSource()))
|
||||
|
||||
options = append(options, config.WithReader(json.NewReader(reader.WithEncoder(toml.NewEncoder()))))
|
||||
|
||||
conf, err := config.NewConfig(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := conf.Scan(&c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 10)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
if err := conf.Scan(&c); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func etcdEndpoints() []string {
|
||||
var endpoints []string
|
||||
envVars := strings.Split(os.Getenv("ETCD_ENDPOINTS"), ",")
|
||||
for _, env := range envVars {
|
||||
if strings.TrimSpace(env) == "" {
|
||||
continue
|
||||
}
|
||||
endpoints = append(endpoints, strings.TrimSpace(env))
|
||||
}
|
||||
return endpoints
|
||||
}
|
||||
18
providers/config/section_app.go
Normal file
18
providers/config/section_app.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package config
|
||||
|
||||
import "app/utils"
|
||||
|
||||
type App struct {
|
||||
Mode string
|
||||
}
|
||||
|
||||
func (a App) IsDebug() bool {
|
||||
return a.Mode == "debug"
|
||||
}
|
||||
|
||||
func (a App) IsRelease() bool {
|
||||
return a.Mode == "release"
|
||||
}
|
||||
func (a App) IsTesting() bool {
|
||||
return a.Mode == "testing" || utils.IsInTesting()
|
||||
}
|
||||
20
providers/config/section_http.go
Normal file
20
providers/config/section_http.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Http struct {
|
||||
Static string
|
||||
Host string
|
||||
Port uint
|
||||
Https bool
|
||||
HttpsCert string
|
||||
HttpKey string
|
||||
}
|
||||
|
||||
func (h Http) Address() string {
|
||||
return fmt.Sprintf("%s:%d", h.Host, h.Port)
|
||||
}
|
||||
|
||||
func (h Http) PortString() string {
|
||||
return fmt.Sprintf(":%d", h.Port)
|
||||
}
|
||||
5
providers/config/section_log.go
Normal file
5
providers/config/section_log.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type Log struct {
|
||||
Level string
|
||||
}
|
||||
Reference in New Issue
Block a user