本文整理匯總了Golang中github.com/micro/go-micro/cmd.DefaultOptions函數的典型用法代碼示例。如果您正苦於以下問題:Golang DefaultOptions函數的具體用法?Golang DefaultOptions怎麽用?Golang DefaultOptions使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了DefaultOptions函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getService
func getService(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
service := r.Form.Get("service")
var s []*registry.Service
var err error
if len(service) == 0 {
s, err = (*cmd.DefaultOptions().Registry).ListServices()
} else {
s, err = (*cmd.DefaultOptions().Registry).GetService(service)
}
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if s == nil || (len(service) > 0 && (len(s) == 0 || len(s[0].Name) == 0)) {
http.Error(w, "Service not found", 404)
return
}
b, err := json.Marshal(s)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.Write(b)
}
示例2: web
func web(ctx *cli.Context) {
opts := []gweb.Option{
gweb.Name("go.micro.web.discovery"),
gweb.Handler(whandler.Router()),
}
opts = append(opts, helper.WebOpts(ctx)...)
templateDir := "discovery/templates"
if dir := ctx.GlobalString("html_dir"); len(dir) > 0 {
templateDir = dir
}
whandler.Init(
templateDir,
proto.NewDiscoveryClient("go.micro.srv.discovery", *cmd.DefaultOptions().Client),
proto2.NewRegistryClient("go.micro.srv.discovery", *cmd.DefaultOptions().Client),
)
service := gweb.NewService(opts...)
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
示例3: brokerHandler
func brokerHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
r.ParseForm()
topic := r.Form.Get("topic")
if len(topic) == 0 {
http.Error(w, "Topic not specified", 400)
return
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error(err.Error())
return
}
once.Do(func() {
(*cmd.DefaultOptions().Broker).Init()
(*cmd.DefaultOptions().Broker).Connect()
})
c := &conn{
topic: topic,
ws: ws,
}
go c.writeLoop()
c.readLoop()
}
示例4: streamService
// TODO: stream via HTTP
func streamService(c *cli.Context) {
if len(c.Args()) < 2 {
fmt.Println("require service and method")
return
}
service := c.Args()[0]
method := c.Args()[1]
var request map[string]interface{}
json.Unmarshal([]byte(strings.Join(c.Args()[2:], " ")), &request)
req := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request)
stream, err := (*cmd.DefaultOptions().Client).Stream(context.Background(), req)
if err != nil {
fmt.Printf("error calling %s.%s: %v\n", service, method, err)
return
}
if err := stream.Send(request); err != nil {
fmt.Printf("error sending to %s.%s: %v\n", service, method, err)
return
}
for {
var response map[string]interface{}
if err := stream.Recv(&response); err != nil {
fmt.Printf("error receiving from %s.%s: %v\n", service, method, err)
return
}
b, _ := json.MarshalIndent(response, "", "\t")
fmt.Println(string(b))
// artificial delay
time.Sleep(time.Millisecond * 10)
}
}
示例5: QueryService
func QueryService(c *cli.Context, args []string) ([]byte, error) {
if len(args) < 2 {
return nil, errors.New("require service and method")
}
var req, service, method string
service = args[0]
method = args[1]
if len(args) > 2 {
req = strings.Join(args[2:], " ")
}
// empty request
if len(req) == 0 {
req = `{}`
}
var request map[string]interface{}
var response json.RawMessage
if p := c.GlobalString("proxy_address"); len(p) > 0 {
request = map[string]interface{}{
"service": service,
"method": method,
"request": req,
}
b, err := json.Marshal(request)
if err != nil {
return nil, err
}
if err := post(p+"/rpc", b, &response); err != nil {
return nil, err
}
} else {
d := json.NewDecoder(strings.NewReader(req))
d.UseNumber()
if err := d.Decode(&request); err != nil {
return nil, err
}
creq := (*cmd.DefaultOptions().Client).NewJsonRequest(service, method, request)
err := (*cmd.DefaultOptions().Client).Call(context.Background(), creq, &response)
if err != nil {
return nil, fmt.Errorf("error calling %s.%s: %v\n", service, method, err)
}
}
var out bytes.Buffer
defer out.Reset()
if err := json.Indent(&out, response, "", "\t"); err != nil {
return nil, err
}
return out.Bytes(), nil
}
示例6: registryHandler
func registryHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
svc := r.Form.Get("service")
if len(svc) > 0 {
s, err := (*cmd.DefaultOptions().Registry).GetService(svc)
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
if len(s) == 0 {
http.Error(w, "Not found", 404)
return
}
if r.Header.Get("Content-Type") == "application/json" {
b, err := json.Marshal(map[string]interface{}{
"services": s,
})
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
render(w, r, serviceTemplate, s)
return
}
services, err := (*cmd.DefaultOptions().Registry).ListServices()
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
sort.Sort(sortedServices{services})
if r.Header.Get("Content-Type") == "application/json" {
b, err := json.Marshal(map[string]interface{}{
"services": services,
})
if err != nil {
http.Error(w, "Error occurred:"+err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
render(w, r, registryTemplate, services)
}
示例7: addService
func addService(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer r.Body.Close()
var opts []registry.RegisterOption
// parse ttl
if ttl := r.Form.Get("ttl"); len(ttl) > 0 {
d, err := time.ParseDuration(ttl)
if err == nil {
opts = append(opts, registry.RegisterTTL(d))
}
}
var service *registry.Service
err = json.Unmarshal(b, &service)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
err = (*cmd.DefaultOptions().Registry).Register(service, opts...)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
示例8: writeLoop
func (c *conn) writeLoop() {
ticker := time.NewTicker(pingTime)
subscriber, err := (*cmd.DefaultOptions().Broker).Subscribe(c.topic, func(p broker.Publication) error {
b, err := json.Marshal(p.Message())
if err != nil {
return nil
}
return c.write(websocket.TextMessage, b)
})
defer func() {
subscriber.Unsubscribe()
ticker.Stop()
c.ws.Close()
}()
if err != nil {
log.Error(err.Error())
return
}
for _ = range ticker.C {
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
示例9: run
// run healthchecker
func (s *Sidecar) run(exit chan bool) {
parts := strings.Split(s.address, ":")
host := strings.Join(parts[:len(parts)-1], ":")
port, _ := strconv.Atoi(parts[len(parts)-1])
id := s.name + "-" + uuid.NewUUID().String()
node := ®istry.Node{
Id: id,
Address: host,
Port: port,
}
service := ®istry.Service{
Name: s.name,
Nodes: []*registry.Node{node},
}
log.Printf("Registering %s", node.Id)
(*cmd.DefaultOptions().Registry).Register(service)
if len(s.hcUrl) == 0 {
return
}
log.Print("Starting sidecar healthchecker")
go s.hcLoop(service, exit)
<-exit
}
示例10: readLoop
func (c *conn) readLoop() {
defer func() {
c.close()
c.ws.Close()
}()
// set read limit/deadline
c.ws.SetReadLimit(readLimit)
c.ws.SetReadDeadline(time.Now().Add(readDeadline))
// set close handler
ch := c.ws.CloseHandler()
c.ws.SetCloseHandler(func(code int, text string) error {
err := ch(code, text)
c.close()
return err
})
// set pong handler
c.ws.SetPongHandler(func(string) error {
c.ws.SetReadDeadline(time.Now().Add(readDeadline))
return nil
})
for {
_, message, err := c.ws.ReadMessage()
if err != nil {
return
}
(*cmd.DefaultOptions().Broker).Publish(c.topic, &broker.Message{
Header: map[string]string{"Content-Type": c.cType},
Body: message,
})
}
}
示例11: proxy
func (s *server) proxy() http.Handler {
sel := selector.NewSelector(
selector.Registry((*cmd.DefaultOptions().Registry)),
)
director := func(r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 2 {
return
}
if !re.MatchString(parts[1]) {
return
}
next, err := sel.Select(Namespace + "." + parts[1])
if err != nil {
return
}
r.URL.Scheme = "http"
s, err := next()
if err != nil {
return
}
r.URL.Host = fmt.Sprintf("%s:%d", s.Address, s.Port)
r.URL.Path = "/" + strings.Join(parts[2:], "/")
}
return &httputil.ReverseProxy{
Director: director,
}
}
示例12: run
func (s *Sidecar) run() {
parts := strings.Split(s.address, ":")
host := strings.Join(parts[:len(parts)-1], ":")
port, _ := strconv.Atoi(parts[len(parts)-1])
id := s.name + "-" + uuid.NewUUID().String()
node := ®istry.Node{
Id: id,
Address: host,
Port: port,
}
service := ®istry.Service{
Name: s.name,
Nodes: []*registry.Node{node},
}
log.Infof("Registering %s", node.Id)
(*cmd.DefaultOptions().Registry).Register(service)
if len(s.hcUrl) > 0 {
log.Info("Starting sidecar healthchecker")
exitCh := make(chan bool, 1)
go s.hcLoop(service, exitCh)
defer func() {
exitCh <- true
}()
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
<-ch
}
示例13: ListServices
func ListServices(c *cli.Context) ([]byte, error) {
var rsp []*registry.Service
var err error
if p := c.GlobalString("proxy_address"); len(p) > 0 {
if err := get(p+"/registry", &rsp); err != nil {
return nil, err
}
} else {
rsp, err = (*cmd.DefaultOptions().Registry).ListServices()
if err != nil {
return nil, err
}
}
sort.Sort(sortedServices{rsp})
var services []string
for _, service := range rsp {
services = append(services, service.Name)
}
return []byte(strings.Join(services, "\n")), nil
}
示例14: DeregisterService
func DeregisterService(c *cli.Context, args []string) ([]byte, error) {
if len(args) == 0 {
return nil, errors.New("require service definition")
}
req := strings.Join(args, " ")
if p := c.GlobalString("proxy_address"); len(p) > 0 {
if err := del(p+"/registry", []byte(req), nil); err != nil {
return nil, err
}
return []byte("ok"), nil
}
var service *registry.Service
d := json.NewDecoder(strings.NewReader(req))
d.UseNumber()
if err := d.Decode(&service); err != nil {
return nil, err
}
if err := (*cmd.DefaultOptions().Registry).Deregister(service); err != nil {
return nil, err
}
return []byte("ok"), nil
}
示例15: ServeHTTP
// API handler is the default handler which takes api.Request and returns api.Response
func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request, err := requestToProto(r)
if err != nil {
er := errors.InternalServerError("go.micro.api", err.Error())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
w.Write([]byte(er.Error()))
return
}
// get service and method
service, method := pathToReceiver(a.Namespace, r.URL.Path)
// create request and response
req := (*cmd.DefaultOptions().Client).NewRequest(service, method, request)
rsp := &api.Response{}
// create the context from headers
ctx := helper.RequestToContext(r)
if err := (*cmd.DefaultOptions().Client).Call(ctx, req, rsp); err != nil {
w.Header().Set("Content-Type", "application/json")
ce := errors.Parse(err.Error())
switch ce.Code {
case 0:
w.WriteHeader(500)
default:
w.WriteHeader(int(ce.Code))
}
w.Write([]byte(ce.Error()))
return
}
for _, header := range rsp.GetHeader() {
for _, val := range header.Values {
w.Header().Add(header.Key, val)
}
}
if len(w.Header().Get("Content-Type")) == 0 {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(int(rsp.StatusCode))
w.Write([]byte(rsp.Body))
}