2015-02-14 23:33:21 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2015-12-05 19:25:36 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-08-30 02:24:16 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-05-20 23:39:11 +01:00
|
|
|
"io"
|
2015-12-05 19:25:36 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2015-04-27 22:21:56 +01:00
|
|
|
"os"
|
2015-12-05 19:25:36 +00:00
|
|
|
"runtime"
|
2015-05-16 00:34:02 +01:00
|
|
|
"strings"
|
2015-04-27 22:21:56 +01:00
|
|
|
"text/tabwriter"
|
|
|
|
"text/template"
|
2015-12-05 19:25:36 +00:00
|
|
|
"time"
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2015-04-27 22:21:56 +01:00
|
|
|
"github.com/codegangsta/cli"
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/broker"
|
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/registry"
|
2015-12-09 19:32:10 +00:00
|
|
|
"github.com/micro/go-micro/selector"
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"github.com/micro/go-micro/transport"
|
2015-12-05 19:25:36 +00:00
|
|
|
"github.com/pborman/uuid"
|
2015-02-14 23:33:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-04-27 22:21:56 +01:00
|
|
|
Flags = []cli.Flag{
|
2015-05-26 22:39:48 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_name",
|
|
|
|
EnvVar: "MICRO_SERVER_NAME",
|
|
|
|
Usage: "Name of the server. go.micro.srv.example",
|
|
|
|
},
|
2015-11-08 01:48:48 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_version",
|
|
|
|
EnvVar: "MICRO_SERVER_VERSION",
|
|
|
|
Usage: "Version of the server. 1.1.0",
|
|
|
|
},
|
2015-05-26 22:39:48 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_id",
|
|
|
|
EnvVar: "MICRO_SERVER_ID",
|
|
|
|
Usage: "Id of the server. Auto-generated if not specified",
|
|
|
|
},
|
2015-05-16 00:34:02 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_address",
|
|
|
|
EnvVar: "MICRO_SERVER_ADDRESS",
|
|
|
|
Value: ":0",
|
|
|
|
Usage: "Bind address for the server. 127.0.0.1:8080",
|
|
|
|
},
|
2015-11-11 18:22:04 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "server_advertise",
|
|
|
|
EnvVar: "MICRO_SERVER_ADVERTISE",
|
|
|
|
Usage: "Used instead of the server_address when registering with discovery. 127.0.0.1:8080",
|
|
|
|
},
|
2015-05-26 22:39:48 +01: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 00:34:02 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "broker",
|
|
|
|
EnvVar: "MICRO_BROKER",
|
|
|
|
Value: "http",
|
2015-10-18 12:12:41 +01:00
|
|
|
Usage: "Broker for pub/sub. http, nats, rabbitmq",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
|
|
|
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",
|
2015-10-18 12:12:41 +01:00
|
|
|
Usage: "Registry for discovery. memory, consul, etcd, kubernetes",
|
2015-05-16 00:34:02 +01:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "registry_address",
|
|
|
|
EnvVar: "MICRO_REGISTRY_ADDRESS",
|
|
|
|
Usage: "Comma-separated list of registry addresses",
|
|
|
|
},
|
2015-12-09 19:32:10 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "selector",
|
|
|
|
EnvVar: "MICRO_SELECTOR",
|
|
|
|
Value: "selector",
|
|
|
|
Usage: "Selector used to pick nodes for querying. random, roundrobin, blacklist",
|
|
|
|
},
|
2015-05-20 22:57:19 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "transport",
|
|
|
|
EnvVar: "MICRO_TRANSPORT",
|
|
|
|
Value: "http",
|
2015-10-18 12:12:41 +01:00
|
|
|
Usage: "Transport mechanism used; http, rabbitmq, nats",
|
2015-05-20 22:57:19 +01:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "transport_address",
|
|
|
|
EnvVar: "MICRO_TRANSPORT_ADDRESS",
|
|
|
|
Usage: "Comma-separated list of transport addresses",
|
|
|
|
},
|
2015-08-30 02:24:16 +01:00
|
|
|
|
2015-12-05 19:25:36 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "disable_ping",
|
|
|
|
EnvVar: "MICRO_DISABLE_PING",
|
|
|
|
Usage: "Disable ping",
|
|
|
|
},
|
|
|
|
|
2015-08-30 02:24:16 +01:00
|
|
|
// logging flags
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "logtostderr",
|
|
|
|
Usage: "log to standard error instead of files",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "alsologtostderr",
|
|
|
|
Usage: "log to standard error as well as files",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "log_dir",
|
|
|
|
Usage: "log files will be written to this directory instead of the default temporary directory",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "stderrthreshold",
|
|
|
|
Usage: "logs at or above this threshold go to stderr",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "v",
|
|
|
|
Usage: "log level for V logs",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "vmodule",
|
|
|
|
Usage: "comma-separated list of pattern=N settings for file-filtered logging",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "log_backtrace_at",
|
|
|
|
Usage: "when logging hits line file:N, emit a stack trace",
|
|
|
|
},
|
2015-04-27 22:21:56 +01:00
|
|
|
}
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
Brokers = map[string]func([]string, ...broker.Option) broker.Broker{
|
2015-11-25 00:17:15 +00:00
|
|
|
"http": broker.NewBroker,
|
2015-06-01 18:55:27 +01:00
|
|
|
}
|
2015-05-16 00:34:02 +01:00
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
Registries = map[string]func([]string, ...registry.Option) registry.Registry{
|
2015-11-25 00:17:15 +00:00
|
|
|
"consul": registry.NewRegistry,
|
2015-05-16 00:34:02 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 19:32:10 +00:00
|
|
|
Selectors = map[string]func(...selector.Option) selector.Selector{
|
|
|
|
"random": selector.NewSelector,
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
Transports = map[string]func([]string, ...transport.Option) transport.Transport{
|
2015-11-25 00:17:15 +00:00
|
|
|
"http": transport.NewTransport,
|
2015-06-01 18:55:27 +01:00
|
|
|
}
|
|
|
|
)
|
2015-02-14 23:33:21 +00:00
|
|
|
|
2015-12-05 19:25:36 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping informs micro-services about this thing
|
|
|
|
func ping() {
|
|
|
|
type Ping struct {
|
|
|
|
Id string
|
|
|
|
Timestamp int64
|
|
|
|
Product string
|
|
|
|
Version string
|
|
|
|
Arch string
|
|
|
|
Os string
|
|
|
|
}
|
|
|
|
|
|
|
|
p := Ping{
|
|
|
|
Id: uuid.NewUUID().String(),
|
|
|
|
Product: "go-micro",
|
|
|
|
Version: "latest",
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
cl := &http.Client{}
|
|
|
|
|
|
|
|
fn := func() {
|
|
|
|
p.Timestamp = time.Now().Unix()
|
|
|
|
b, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
buf.Write(b)
|
|
|
|
rsp, err := cl.Post("https://micro-services.co/_ping", "application/json", buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't ping unless this thing has lived for 30 seconds
|
|
|
|
time.Sleep(time.Second * 30)
|
|
|
|
|
|
|
|
// only ping every 24 hours, be non invasive
|
|
|
|
for {
|
|
|
|
fn()
|
|
|
|
time.Sleep(time.Hour * 24)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
func Setup(c *cli.Context) error {
|
2015-10-18 00:01:43 +01:00
|
|
|
os.Args = os.Args[:1]
|
|
|
|
|
|
|
|
flag.Set("logtostderr", fmt.Sprintf("%v", c.Bool("logtostderr")))
|
|
|
|
flag.Set("alsologtostderr", fmt.Sprintf("%v", c.Bool("alsologtostderr")))
|
|
|
|
flag.Set("stderrthreshold", c.String("stderrthreshold"))
|
|
|
|
flag.Set("log_backtrace_at", c.String("log_backtrace_at"))
|
|
|
|
flag.Set("log_dir", c.String("log_dir"))
|
|
|
|
flag.Set("vmodule", c.String("vmodule"))
|
|
|
|
flag.Set("v", c.String("v"))
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
if b, ok := Brokers[c.String("broker")]; ok {
|
|
|
|
broker.DefaultBroker = b(strings.Split(c.String("broker_address"), ","))
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
if r, ok := Registries[c.String("registry")]; ok {
|
|
|
|
registry.DefaultRegistry = r(strings.Split(c.String("registry_address"), ","))
|
|
|
|
}
|
2015-05-20 22:57:19 +01:00
|
|
|
|
2015-12-09 19:32:10 +00:00
|
|
|
if s, ok := Selectors[c.String("selector")]; ok {
|
|
|
|
selector.DefaultSelector = s(selector.Registry(registry.DefaultRegistry))
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
if t, ok := Transports[c.String("transport")]; ok {
|
|
|
|
transport.DefaultTransport = t(strings.Split(c.String("transport_address"), ","))
|
2015-02-14 23:33:21 +00:00
|
|
|
}
|
2015-04-27 22:21:56 +01:00
|
|
|
|
2015-05-26 22:39:48 +01: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")),
|
2015-11-08 01:48:48 +00:00
|
|
|
server.Version(c.String("server_version")),
|
2015-05-26 22:39:48 +01:00
|
|
|
server.Id(c.String("server_id")),
|
|
|
|
server.Address(c.String("server_address")),
|
2015-11-11 18:22:04 +00:00
|
|
|
server.Advertise(c.String("server_advertise")),
|
2015-05-26 22:39:48 +01:00
|
|
|
server.Metadata(metadata),
|
|
|
|
)
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
client.DefaultClient = client.NewClient()
|
2015-08-30 21:22:35 +01:00
|
|
|
|
2015-12-05 19:25:36 +00:00
|
|
|
if !c.Bool("disable_ping") {
|
|
|
|
go ping()
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:21:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
cli.AppHelpTemplate = `
|
|
|
|
GLOBAL OPTIONS:
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
|
|
|
`
|
|
|
|
|
2015-05-20 23:39:11 +01:00
|
|
|
cli.HelpPrinter = func(writer io.Writer, templ string, data interface{}) {
|
2015-04-27 22:21:56 +01: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-14 23:33:21 +00:00
|
|
|
}
|