62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
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"`
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
type Config struct {
|
|
Server Server `yaml:"server"`
|
|
Database Database `yaml:"database"`
|
|
Redis Redis `yaml:"redis"`
|
|
}
|
|
|
|
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
|
|
}
|