add routes, config, args pkg

This commit is contained in:
Jay
2020-08-03 20:19:47 +08:00
commit f92efa91fc
6 changed files with 261 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
package args
import (
"os"
"git.trj.tw/golang/argparse"
)
type Args struct {
ConfigPath string
DBTool bool
Run string
}
var a *Args
func Parse() error {
a = &Args{}
parser := argparse.New()
parser.StringVar(&a.ConfigPath, "", "c", "config", "config file path", nil)
parser.StringVar(&a.Run, "server", "r", "run", "run mode [server, dbtool], default server", nil)
parser.BoolVar(&a.DBTool, false, "d", "dbtool", "run db migration", nil)
parser.Help("h", "help")
return parser.Parse(os.Args)
}
+53
View File
@@ -0,0 +1,53 @@
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"`
}
type Config struct {
Server Server `yaml:"server"`
Database Database `yaml:"database"`
}
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
}