Exemple #1
0
func newOS(opts ...selector.Option) Router {
	options := selector.Options{
		Context: context.TODO(),
	}

	for _, o := range opts {
		o(&options)
	}

	c, ok := client.FromContext(options.Context)
	if !ok {
		c = client.DefaultClient
	}

	s, ok := server.FromContext(options.Context)
	if !ok {
		s = server.DefaultServer
	}

	o := &os{
		exit:   make(chan bool),
		opts:   options,
		client: c,
		server: s,
		cache:  make(map[string]*cache),
		stats:  make(map[string]*stats),
		r:      proto.NewRouterClient("go.micro.srv.router", c),
	}

	go o.run()
	return o
}
Exemple #2
0
func web(ctx *cli.Context) {
	opts := []gweb.Option{
		gweb.Name("go.micro.web.router"),
		gweb.Handler(whandler.Router()),
	}

	opts = append(opts, helper.WebOpts(ctx)...)

	templateDir := "router/templates"
	if dir := ctx.GlobalString("html_dir"); len(dir) > 0 {
		templateDir = dir
	}

	whandler.Init(
		templateDir,
		label.NewLabelClient("go.micro.srv.router", *cmd.DefaultOptions().Client),
		rule.NewRuleClient("go.micro.srv.router", *cmd.DefaultOptions().Client),
		proto.NewRouterClient("go.micro.srv.router", *cmd.DefaultOptions().Client),
	)

	service := gweb.NewService(opts...)

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
Exemple #3
0
func (o *os) Init(opts ...selector.Option) error {
	var options selector.Options
	for _, o := range opts {
		o(&options)
	}

	// TODO: Fix. This might all be really bad and hacky

	if c, ok := client.FromContext(options.Context); ok {
		o.client = c
		o.r = proto.NewRouterClient("go.micro.srv.router", c)
	}

	if s, ok := server.FromContext(options.Context); ok {
		o.server = s
	}

	return nil
}
Exemple #4
0
func main() {
	cmd.Init()

	r := proto.NewRouterClient(service, client.DefaultClient)

	stream, err := r.SelectStream(context.TODO(), &proto.SelectRequest{Service: service})
	if err != nil {
		fmt.Println("error streaming", err)
		return
	}

	for i := 0; i <= 3; {
		fmt.Println("waiting on stream")
		rsp, err := stream.Recv()
		if err != nil {
			fmt.Println("error receiving", err)
			return
		}
		fmt.Println("got stream response, expires", rsp.Expires)
		for _, s := range rsp.Services {
			fmt.Printf("received %s %s %+v\n", s.Name, s.Version, s.Nodes)
		}
	}
}