Refactor config and app entry

This commit is contained in:
cuigh 2017-10-27 20:31:30 +08:00
parent 59c8d6cf36
commit 9de2f2e62c
4 changed files with 39 additions and 33 deletions

View File

@ -13,12 +13,9 @@ const (
apiVersion = "1.32" apiVersion = "1.32"
) )
var mgr = &manager{ var mgr = &manager{}
host: misc.DockerHost,
}
type manager struct { type manager struct {
host string
client *client.Client client *client.Client
locker sync.Mutex locker sync.Mutex
logger *log.Logger logger *log.Logger
@ -38,10 +35,10 @@ func (m *manager) Client() (ctx context.Context, cli *client.Client, err error)
defer m.locker.Unlock() defer m.locker.Unlock()
if m.client == nil { if m.client == nil {
if m.host == "" { if misc.Options.DockerEndpoint == "" {
m.client, err = client.NewEnvClient() m.client, err = client.NewEnvClient()
} else { } 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 { if err != nil {
return return

View File

@ -32,11 +32,11 @@ func Get() (Interface, error) {
} }
func create() (d Interface, err error) { func create() (d Interface, err error) {
switch misc.DBType { switch misc.Options.DBType {
case "", "mongo": case "", "mongo":
return mongo.New(misc.DBAddress) return mongo.New(misc.Options.DBAddress)
default: default:
err = errors.New("Unknown database type: " + misc.DBType) err = errors.New("Unknown database type: " + misc.Options.DBType)
} }
return return
} }

19
main.go
View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/cuigh/auxo/app" "github.com/cuigh/auxo/app"
"github.com/cuigh/auxo/app/flag"
"github.com/cuigh/auxo/config" "github.com/cuigh/auxo/config"
"github.com/cuigh/auxo/net/web" "github.com/cuigh/auxo/net/web"
"github.com/cuigh/auxo/net/web/auth" "github.com/cuigh/auxo/net/web/auth"
@ -19,6 +20,20 @@ import (
) )
func main() { 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() setting, err := biz.Setting.Get()
if err != nil { if err != nil {
panic(fmt.Sprintf("Load setting failed: %v", err)) panic(fmt.Sprintf("Load setting failed: %v", err))
@ -40,7 +55,7 @@ func main() {
AddFunc("i18n", misc.Message(setting.Language)). AddFunc("i18n", misc.Message(setting.Language)).
AddFuncs(misc.Funcs). AddFuncs(misc.Funcs).
AddVariable("language", setting.Language). AddVariable("language", setting.Language).
AddVariable("version", misc.Version). AddVariable("version", app.Version).
AddVariable("go_version", runtime.Version()) AddVariable("go_version", runtime.Version())
//ws.Validator = valid.New() //ws.Validator = valid.New()
@ -78,5 +93,5 @@ func main() {
g.Handle("/system/setting", controller.Setting()) g.Handle("/system/setting", controller.Setting())
g.Handle("/system/event", controller.Event()) g.Handle("/system/event", controller.Event())
app.Run(ws) return ws
} }

View File

@ -1,38 +1,32 @@
package misc package misc
import ( import (
"os"
"github.com/cuigh/auxo/config" "github.com/cuigh/auxo/config"
) )
const ( const (
// Version is the version of Swirl keyDockerEndpoint = "swirl.docker_endpoint"
Version = "0.6" keyDBType = "swirl.db_type"
) keyDBAddress = "swirl.db_address"
const (
envDockerEndpoint = "DOCKER_ENDPOINT" envDockerEndpoint = "DOCKER_ENDPOINT"
envDBType = "DB_TYPE" envDBType = "DB_TYPE"
envDBAddress = "DB_ADDRESS" envDBAddress = "DB_ADDRESS"
) )
var ( var Options = &struct {
DockerHost string DockerEndpoint string
DBType string DBType string
DBAddress string DBAddress string
) }{}
func init() { func BindOptions() {
DockerHost = loadOption("swirl.docker_endpoint", envDockerEndpoint) config.BindEnv(keyDockerEndpoint, envDockerEndpoint)
DBType = loadOption("swirl.db_type", envDBType) config.BindEnv(keyDBType, envDBType)
DBAddress = loadOption("swirl.db_address", envDBAddress) config.BindEnv(keyDBAddress, envDBAddress)
} }
func loadOption(key, env string) (opt string) { func LoadOptions() {
opt = config.GetString(key) Options.DockerEndpoint = config.GetString(keyDockerEndpoint)
if opt == "" { Options.DBType = config.GetString(keyDBType)
opt = os.Getenv(env) Options.DBAddress = config.GetString(keyDBAddress)
}
return
} }