From 9de2f2e62c5fad976e2f3e5beeb983997d5415ab Mon Sep 17 00:00:00 2001 From: cuigh Date: Fri, 27 Oct 2017 20:31:30 +0800 Subject: [PATCH] Refactor config and app entry --- biz/docker/docker.go | 9 +++------ dao/dao.go | 6 +++--- main.go | 19 +++++++++++++++++-- misc/misc.go | 38 ++++++++++++++++---------------------- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/biz/docker/docker.go b/biz/docker/docker.go index 907a59a..a9e097d 100644 --- a/biz/docker/docker.go +++ b/biz/docker/docker.go @@ -13,12 +13,9 @@ const ( apiVersion = "1.32" ) -var mgr = &manager{ - host: misc.DockerHost, -} +var mgr = &manager{} type manager struct { - host string client *client.Client locker sync.Mutex logger *log.Logger @@ -38,10 +35,10 @@ func (m *manager) Client() (ctx context.Context, cli *client.Client, err error) defer m.locker.Unlock() if m.client == nil { - if m.host == "" { + if misc.Options.DockerEndpoint == "" { m.client, err = client.NewEnvClient() } else { - m.client, err = client.NewClient(m.host, apiVersion, nil, nil) + m.client, err = client.NewClient(misc.Options.DockerEndpoint, apiVersion, nil, nil) } if err != nil { return diff --git a/dao/dao.go b/dao/dao.go index 564abb1..9d8c22f 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -32,11 +32,11 @@ func Get() (Interface, error) { } func create() (d Interface, err error) { - switch misc.DBType { + switch misc.Options.DBType { case "", "mongo": - return mongo.New(misc.DBAddress) + return mongo.New(misc.Options.DBAddress) default: - err = errors.New("Unknown database type: " + misc.DBType) + err = errors.New("Unknown database type: " + misc.Options.DBType) } return } diff --git a/main.go b/main.go index 09d62f3..579a3ac 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "time" "github.com/cuigh/auxo/app" + "github.com/cuigh/auxo/app/flag" "github.com/cuigh/auxo/config" "github.com/cuigh/auxo/net/web" "github.com/cuigh/auxo/net/web/auth" @@ -19,6 +20,20 @@ import ( ) func main() { + misc.BindOptions() + + app.Name = "Swirl" + app.Version = "0.6" + app.Desc = "A web management UI for Docker, focused on swarm cluster" + app.Action = func(ctx *app.Context) { + misc.LoadOptions() + app.Run(server()) + } + app.Register(flag.All) + app.Start() +} + +func server() *web.Server { setting, err := biz.Setting.Get() if err != nil { panic(fmt.Sprintf("Load setting failed: %v", err)) @@ -40,7 +55,7 @@ func main() { AddFunc("i18n", misc.Message(setting.Language)). AddFuncs(misc.Funcs). AddVariable("language", setting.Language). - AddVariable("version", misc.Version). + AddVariable("version", app.Version). AddVariable("go_version", runtime.Version()) //ws.Validator = valid.New() @@ -78,5 +93,5 @@ func main() { g.Handle("/system/setting", controller.Setting()) g.Handle("/system/event", controller.Event()) - app.Run(ws) + return ws } diff --git a/misc/misc.go b/misc/misc.go index 2075933..a3fec2c 100644 --- a/misc/misc.go +++ b/misc/misc.go @@ -1,38 +1,32 @@ package misc import ( - "os" - "github.com/cuigh/auxo/config" ) const ( - // Version is the version of Swirl - Version = "0.6" -) - -const ( + keyDockerEndpoint = "swirl.docker_endpoint" + keyDBType = "swirl.db_type" + keyDBAddress = "swirl.db_address" envDockerEndpoint = "DOCKER_ENDPOINT" envDBType = "DB_TYPE" envDBAddress = "DB_ADDRESS" ) -var ( - DockerHost string - DBType string - DBAddress string -) +var Options = &struct { + DockerEndpoint string + DBType string + DBAddress string +}{} -func init() { - DockerHost = loadOption("swirl.docker_endpoint", envDockerEndpoint) - DBType = loadOption("swirl.db_type", envDBType) - DBAddress = loadOption("swirl.db_address", envDBAddress) +func BindOptions() { + config.BindEnv(keyDockerEndpoint, envDockerEndpoint) + config.BindEnv(keyDBType, envDBType) + config.BindEnv(keyDBAddress, envDBAddress) } -func loadOption(key, env string) (opt string) { - opt = config.GetString(key) - if opt == "" { - opt = os.Getenv(env) - } - return +func LoadOptions() { + Options.DockerEndpoint = config.GetString(keyDockerEndpoint) + Options.DBType = config.GetString(keyDBType) + Options.DBAddress = config.GetString(keyDBAddress) }