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"
)
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

View File

@ -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
}

19
main.go
View File

@ -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
}

View File

@ -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)
}