Remove flags from server package. Create cmd package to parse flags and cli usage

This commit is contained in:
Asim 2015-02-14 23:33:21 +00:00
parent 4b494966fb
commit 606255a7f3
3 changed files with 43 additions and 18 deletions

37
cmd/cmd.go Normal file
View File

@ -0,0 +1,37 @@
package cmd
import (
"flag"
"github.com/asim/go-micro/registry"
"github.com/asim/go-micro/server"
"github.com/asim/go-micro/store"
)
var (
flagBindAddress string
flagRegistry string
flagStore string
)
func init() {
flag.StringVar(&flagBindAddress, "bind_address", ":0", "Bind address for the server. 127.0.0.1:8080")
flag.StringVar(&flagRegistry, "registry", "consul", "Registry for discovery. kubernetes, consul, etc")
flag.StringVar(&flagStore, "store", "consul", "Store used as a basic key/value store using consul, memcached, etc")
}
func Init() {
flag.Parse()
server.Address = flagBindAddress
switch flagRegistry {
case "kubernetes":
registry.DefaultRegistry = registry.NewKubernetesRegistry()
}
switch flagStore {
case "memcached":
store.DefaultStore = store.NewMemcacheStore()
}
}

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"flag"
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
@ -10,7 +9,6 @@ import (
"code.google.com/p/go-uuid/uuid" "code.google.com/p/go-uuid/uuid"
"github.com/asim/go-micro/registry" "github.com/asim/go-micro/registry"
"github.com/asim/go-micro/store"
log "github.com/golang/glog" log "github.com/golang/glog"
) )
@ -25,28 +23,14 @@ type Server interface {
} }
var ( var (
Address string
Name string Name string
Id string Id string
DefaultServer Server DefaultServer Server
flagRegistry string
flagBindAddress string
) )
func init() {
flag.StringVar(&flagRegistry, "registry", "consul", "Registry for discovery. kubernetes, consul, etc")
flag.StringVar(&flagBindAddress, "bind_address", ":0", "Bind address for the server. 127.0.0.1:8080")
}
func Init() error { func Init() error {
defer log.Flush() defer log.Flush()
flag.Parse()
switch flagRegistry {
case "kubernetes":
registry.DefaultRegistry = registry.NewKubernetesRegistry()
store.DefaultStore = store.NewMemcacheStore()
}
if len(Name) == 0 { if len(Name) == 0 {
Name = "go-server" Name = "go-server"
@ -57,7 +41,7 @@ func Init() error {
} }
if DefaultServer == nil { if DefaultServer == nil {
DefaultServer = NewRpcServer(flagBindAddress) DefaultServer = NewRpcServer(Address)
} }
return DefaultServer.Init() return DefaultServer.Init()

View File

@ -1,12 +1,16 @@
package main package main
import ( import (
"github.com/asim/go-micro/cmd"
"github.com/asim/go-micro/server" "github.com/asim/go-micro/server"
"github.com/asim/go-micro/template/handler" "github.com/asim/go-micro/template/handler"
log "github.com/golang/glog" log "github.com/golang/glog"
) )
func main() { func main() {
// optionally setup command line usage
cmd.Init()
server.Name = "go.micro.service.template" server.Name = "go.micro.service.template"
// Initialise Server // Initialise Server