2015-02-15 01:33:21 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2015-05-21 00:39:11 +02:00
|
|
|
"io"
|
2015-04-27 23:21:56 +02:00
|
|
|
"os"
|
2015-05-16 01:34:02 +02:00
|
|
|
"strings"
|
2015-04-27 23:21:56 +02:00
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
2015-02-15 01:33:21 +02:00
|
|
|
|
2015-04-27 23:21:56 +02:00
|
|
|
"github.com/codegangsta/cli"
|
2015-05-16 01:34:02 +02:00
|
|
|
"github.com/myodc/go-micro/broker"
|
2015-05-22 00:06:01 +02:00
|
|
|
"github.com/myodc/go-micro/client"
|
2015-05-05 20:05:06 +02:00
|
|
|
"github.com/myodc/go-micro/registry"
|
|
|
|
"github.com/myodc/go-micro/server"
|
2015-05-20 23:57:19 +02:00
|
|
|
"github.com/myodc/go-micro/transport"
|
2015-05-23 21:04:16 +02:00
|
|
|
|
|
|
|
// brokers
|
|
|
|
"github.com/myodc/go-micro/broker/http"
|
|
|
|
"github.com/myodc/go-micro/broker/nats"
|
2015-05-28 14:33:04 +02:00
|
|
|
"github.com/myodc/go-micro/broker/rabbitmq"
|
2015-05-23 21:04:16 +02:00
|
|
|
|
|
|
|
// registries
|
|
|
|
"github.com/myodc/go-micro/registry/consul"
|
2015-05-27 23:22:02 +02:00
|
|
|
"github.com/myodc/go-micro/registry/etcd"
|
2015-05-23 21:04:16 +02:00
|
|
|
"github.com/myodc/go-micro/registry/kubernetes"
|
|
|
|
|
|
|
|
// transport
|
|
|
|
thttp "github.com/myodc/go-micro/transport/http"
|
|
|
|
tnats "github.com/myodc/go-micro/transport/nats"
|
2015-05-28 14:33:04 +02:00
|
|
|
trmq "github.com/myodc/go-micro/transport/rabbitmq"
|
2015-02-15 01:33:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-04-27 23:21:56 +02:00
|
|
|
Flags = []cli.Flag{
|
2015-05-26 23:39:48 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_name",
|
|
|
|
EnvVar: "MICRO_SERVER_NAME",
|
|
|
|
Usage: "Name of the server. go.micro.srv.example",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_id",
|
|
|
|
EnvVar: "MICRO_SERVER_ID",
|
|
|
|
Usage: "Id of the server. Auto-generated if not specified",
|
|
|
|
},
|
2015-05-16 01:34:02 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_address",
|
|
|
|
EnvVar: "MICRO_SERVER_ADDRESS",
|
|
|
|
Value: ":0",
|
|
|
|
Usage: "Bind address for the server. 127.0.0.1:8080",
|
|
|
|
},
|
2015-05-26 23:39:48 +02:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "server_metadata",
|
|
|
|
EnvVar: "MICRO_SERVER_METADATA",
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
Usage: "A list of key-value pairs defining metadata. version=1.0.0",
|
|
|
|
},
|
2015-05-16 01:34:02 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "broker",
|
|
|
|
EnvVar: "MICRO_BROKER",
|
|
|
|
Value: "http",
|
|
|
|
Usage: "Broker for pub/sub. http, nats, etc",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "broker_address",
|
|
|
|
EnvVar: "MICRO_BROKER_ADDRESS",
|
|
|
|
Usage: "Comma-separated list of broker addresses",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "registry",
|
|
|
|
EnvVar: "MICRO_REGISTRY",
|
|
|
|
Value: "consul",
|
|
|
|
Usage: "Registry for discovery. kubernetes, consul, etc",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "registry_address",
|
|
|
|
EnvVar: "MICRO_REGISTRY_ADDRESS",
|
|
|
|
Usage: "Comma-separated list of registry addresses",
|
|
|
|
},
|
2015-05-20 23:57:19 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "transport",
|
|
|
|
EnvVar: "MICRO_TRANSPORT",
|
|
|
|
Value: "http",
|
|
|
|
Usage: "Transport mechanism used; http, rabbitmq, etc",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "transport_address",
|
|
|
|
EnvVar: "MICRO_TRANSPORT_ADDRESS",
|
|
|
|
Usage: "Comma-separated list of transport addresses",
|
|
|
|
},
|
2015-04-27 23:21:56 +02:00
|
|
|
}
|
2015-02-15 01:33:21 +02:00
|
|
|
)
|
|
|
|
|
2015-04-27 23:21:56 +02:00
|
|
|
func Setup(c *cli.Context) error {
|
2015-05-20 23:57:19 +02:00
|
|
|
bAddrs := strings.Split(c.String("broker_address"), ",")
|
2015-05-16 01:34:02 +02:00
|
|
|
|
|
|
|
switch c.String("broker") {
|
|
|
|
case "http":
|
2015-05-23 21:04:16 +02:00
|
|
|
broker.DefaultBroker = http.NewBroker(bAddrs)
|
2015-05-16 01:34:02 +02:00
|
|
|
case "nats":
|
2015-05-23 21:04:16 +02:00
|
|
|
broker.DefaultBroker = nats.NewBroker(bAddrs)
|
2015-05-28 14:33:04 +02:00
|
|
|
case "rabbitmq":
|
|
|
|
broker.DefaultBroker = rabbitmq.NewBroker(bAddrs)
|
2015-05-16 01:34:02 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
rAddrs := strings.Split(c.String("registry_address"), ",")
|
2015-02-15 01:33:21 +02:00
|
|
|
|
2015-04-27 23:21:56 +02:00
|
|
|
switch c.String("registry") {
|
2015-02-15 01:33:21 +02:00
|
|
|
case "kubernetes":
|
2015-05-23 21:04:16 +02:00
|
|
|
registry.DefaultRegistry = kubernetes.NewRegistry(rAddrs)
|
2015-05-16 01:34:02 +02:00
|
|
|
case "consul":
|
2015-05-23 21:04:16 +02:00
|
|
|
registry.DefaultRegistry = consul.NewRegistry(rAddrs)
|
2015-05-27 23:22:02 +02:00
|
|
|
case "etcd":
|
|
|
|
registry.DefaultRegistry = etcd.NewRegistry(rAddrs)
|
2015-02-15 01:33:21 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
tAddrs := strings.Split(c.String("transport_address"), ",")
|
|
|
|
|
|
|
|
switch c.String("transport") {
|
|
|
|
case "http":
|
2015-05-23 21:04:16 +02:00
|
|
|
transport.DefaultTransport = thttp.NewTransport(tAddrs)
|
2015-05-20 23:57:19 +02:00
|
|
|
case "rabbitmq":
|
2015-05-28 14:33:04 +02:00
|
|
|
transport.DefaultTransport = trmq.NewTransport(tAddrs)
|
2015-05-20 23:57:19 +02:00
|
|
|
case "nats":
|
2015-05-23 21:04:16 +02:00
|
|
|
transport.DefaultTransport = tnats.NewTransport(tAddrs)
|
2015-02-15 01:33:21 +02:00
|
|
|
}
|
2015-04-27 23:21:56 +02:00
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
metadata := make(map[string]string)
|
|
|
|
for _, d := range c.StringSlice("server_metadata") {
|
|
|
|
var key, val string
|
|
|
|
parts := strings.Split(d, "=")
|
|
|
|
key = parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
val = strings.Join(parts[1:], "=")
|
|
|
|
}
|
|
|
|
metadata[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
server.DefaultServer = server.NewServer(
|
|
|
|
server.Name(c.String("server_name")),
|
|
|
|
server.Id(c.String("server_id")),
|
|
|
|
server.Address(c.String("server_address")),
|
|
|
|
server.Metadata(metadata),
|
|
|
|
)
|
|
|
|
|
2015-05-23 21:04:16 +02:00
|
|
|
client.DefaultClient = client.NewClient()
|
2015-05-22 00:06:01 +02:00
|
|
|
|
2015-04-27 23:21:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
cli.AppHelpTemplate = `
|
|
|
|
GLOBAL OPTIONS:
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
|
2015-05-21 00:39:11 +02:00
|
|
|
cli.HelpPrinter = func(writer io.Writer, templ string, data interface{}) {
|
2015-04-27 23:21:56 +02:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
|
|
|
t := template.Must(template.New("help").Parse(templ))
|
|
|
|
err := t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.HideVersion = true
|
|
|
|
app.Usage = "a go micro app"
|
|
|
|
app.Action = func(c *cli.Context) {}
|
|
|
|
app.Before = Setup
|
|
|
|
app.Flags = Flags
|
|
|
|
app.RunAndExitOnError()
|
2015-02-15 01:33:21 +02:00
|
|
|
}
|