2020-08-03 12:19:47 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
confLoader "git.trj.tw/golang/config-loader"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server backend config
|
|
|
|
type Server struct {
|
|
|
|
Port int `yaml:"port" env:"SERVER_PORT" default:"10230"`
|
|
|
|
BackendURL string `yaml:"backend_url" env:"SERVER_BACKEND_URL" default:"http://localhost:10230"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Database connect setting
|
|
|
|
type Database struct {
|
|
|
|
DSN string `yaml:"dsn" env:"DB_DSN" default:"postgres://postgres@localhost:5432/dbname"`
|
|
|
|
MaxConn uint `yaml:"max_conn" env:"DB_MAX_CONN" default:"5"`
|
|
|
|
IdleConn uint `yaml:"idle_conn" env:"DB_IDLE_CONN" default:"2"`
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:21:27 +00:00
|
|
|
// Redis connect setting
|
|
|
|
type Redis struct {
|
|
|
|
Host string `yaml:"host" env:"REDIS_HOST" default:"localhost"`
|
|
|
|
Port int `yaml:"port" env:"REDIS_PORT" default:"6379"`
|
|
|
|
Prefix string `yaml:"prefix" env:"REDIS_PREFIX"`
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:19:47 +00:00
|
|
|
type Config struct {
|
|
|
|
Server Server `yaml:"server"`
|
|
|
|
Database Database `yaml:"database"`
|
2020-08-15 17:21:27 +00:00
|
|
|
Redis Redis `yaml:"redis"`
|
2020-08-03 12:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var c *Config
|
|
|
|
|
|
|
|
func Load(p ...string) error {
|
|
|
|
loadOpts := &confLoader.LoadOptions{FromEnv: true}
|
|
|
|
|
|
|
|
if len(p) > 0 && p[0] != "" {
|
|
|
|
loadOpts.ConfigFile = &confLoader.ConfigFile{
|
|
|
|
Type: confLoader.ConfigFileTypeYAML,
|
|
|
|
Path: p[0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c = &Config{}
|
|
|
|
|
|
|
|
if err := confLoader.Load(c, loadOpts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// post loaded proc
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Get() *Config {
|
|
|
|
if c == nil {
|
|
|
|
panic("config not init")
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|