Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
Using cli.Context in server, updating readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Sigrest committed Oct 2, 2017
1 parent dd750c6 commit 660fb40
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
Binary file modified bin/linux_amd64/grpc-lookaside
Binary file not shown.
Binary file modified bin/windows_amd64/grpc-lookaside.exe
Binary file not shown.
5 changes: 2 additions & 3 deletions cmd/grpc-lookaside.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
app := cli.NewApp()
app.Name = "grpc-lookaside"
app.Usage = "A lookaside load balancer for gRPC service requests."
app.Version = "0.0.4"
app.Version = "0.0.6"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bind,b",
Expand Down Expand Up @@ -50,13 +50,12 @@ func main() {
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}

log.Printf("Listening for requests at %s\n", c.String("bind"))
defer listener.Close()

// register the server implementation with the generated handlers and listen for incoming requests
server := grpc.NewServer()
pb.RegisterLookasideServer(server, lookaside.NewServer(c.String("consul"), c.String("datacenter"), c.Float64("refresh")))
pb.RegisterLookasideServer(server, lookaside.NewServer(c))
return server.Serve(listener)

}
Expand Down
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ Unsurprisingly, this service uses a gRPC-based interface to request addresses (s
services and messages).

## CLI
The application has a simple CLI interface and supports the following arguments:
The application has a simple CLI interface and supports the following arguments and environment variable configurations:

### --bind, b
The address that the service will bind to in {host}:{port} format. Defaults to `:3000`.

### --consul, c
### --consul, c [$CONSUL_ADDRESS]
The address of the Consul agent used for service discovery. Defaults to `127.0.0.1:8500` (local agent).

### --datacenter, d
### --datacenter, d [$CONSUL_DATACENTER]
The Consul datacenter to query for services. Defaults to `dc1`.

### --refresh, r [$REFRESH]
Time, in seconds, between service address list refreshes. Defaults to 10 seconds.

## Building
You'll need a working install of the Go programming language to compile the go code and the GNU `make` tool for running
the builds. Once everything is installed and setup, just run:
Expand Down
20 changes: 9 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"time"

"github.com/hashicorp/consul/api"
"github.com/urfave/cli"
"golang.org/x/net/context"

pb "github.com/markitondemand/grpc-lookaside/_proto"
)

type Server struct {
ConsulAddress string
ConsulDatacenter string
routers map[string]*Router
refreshInterval float64
context *cli.Context
routers map[string]*Router
refreshInterval float64
}

func (s *Server) Resolve(ctx context.Context, input *pb.Request) (*pb.Response, error) {
Expand All @@ -32,7 +32,7 @@ func (s *Server) Resolve(ctx context.Context, input *pb.Request) (*pb.Response,
return nil, err
}

s.routers[input.Service] = &Router{Addresses: addresses, LastRefresh: time.Now(), RefreshInterval: s.refreshInterval}
s.routers[input.Service] = &Router{Addresses: addresses, LastRefresh: time.Now(), RefreshInterval: s.context.Float64("refresh")}
}

// determine the type of routing requested, and resolve an address
Expand All @@ -58,7 +58,7 @@ func (s *Server) refreshAddresses(service string) ([]string, error) {
addressSet := make(map[string]struct{})

// create a consul client
consul, err := api.NewClient(&api.Config{Address: s.ConsulAddress, Datacenter: s.ConsulDatacenter})
consul, err := api.NewClient(&api.Config{Address: s.context.String("address"), Datacenter: s.context.String("datacenter")})
if err != nil {
return make([]string, 0), err
}
Expand All @@ -84,11 +84,9 @@ func (s *Server) refreshAddresses(service string) ([]string, error) {

}

func NewServer(address, datacenter string, refresh float64) *Server {
func NewServer(c *cli.Context) *Server {
return &Server{
ConsulAddress: address,
ConsulDatacenter: datacenter,
routers: map[string]*Router{},
refreshInterval: refresh,
context: c,
routers: map[string]*Router{},
}
}

0 comments on commit 660fb40

Please sign in to comment.