Remove flags from server package. Create cmd package to parse flags and cli usage
This commit is contained in:
parent
4b494966fb
commit
606255a7f3
37
cmd/cmd.go
Normal file
37
cmd/cmd.go
Normal 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()
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
@ -10,7 +9,6 @@ import (
|
||||
|
||||
"code.google.com/p/go-uuid/uuid"
|
||||
"github.com/asim/go-micro/registry"
|
||||
"github.com/asim/go-micro/store"
|
||||
log "github.com/golang/glog"
|
||||
)
|
||||
|
||||
@ -25,28 +23,14 @@ type Server interface {
|
||||
}
|
||||
|
||||
var (
|
||||
Address string
|
||||
Name string
|
||||
Id string
|
||||
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 {
|
||||
defer log.Flush()
|
||||
flag.Parse()
|
||||
|
||||
switch flagRegistry {
|
||||
case "kubernetes":
|
||||
registry.DefaultRegistry = registry.NewKubernetesRegistry()
|
||||
store.DefaultStore = store.NewMemcacheStore()
|
||||
}
|
||||
|
||||
if len(Name) == 0 {
|
||||
Name = "go-server"
|
||||
@ -57,7 +41,7 @@ func Init() error {
|
||||
}
|
||||
|
||||
if DefaultServer == nil {
|
||||
DefaultServer = NewRpcServer(flagBindAddress)
|
||||
DefaultServer = NewRpcServer(Address)
|
||||
}
|
||||
|
||||
return DefaultServer.Init()
|
||||
|
@ -1,12 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/asim/go-micro/cmd"
|
||||
"github.com/asim/go-micro/server"
|
||||
"github.com/asim/go-micro/template/handler"
|
||||
log "github.com/golang/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// optionally setup command line usage
|
||||
cmd.Init()
|
||||
|
||||
server.Name = "go.micro.service.template"
|
||||
|
||||
// Initialise Server
|
||||
|
Loading…
x
Reference in New Issue
Block a user