Skip to content

Commit

Permalink
Allow passing multiple addresses to the server
Browse files Browse the repository at this point in the history
This makes the --addr CLI parameter to be a slice and refactors the code
to allow several addresses to be passed. Hence we can listen on as many
HTTP and UNIX sockets as we want.

Signed-off-by: Juan Antonio Osorio Robles <jaosorior@redhat.com>
  • Loading branch information
JAORMX authored and tsandall committed May 30, 2018
1 parent 565786b commit e6bb5d6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ the data document with the following syntax:
runCommand.Flags().StringVarP(&params.ConfigFile, "config-file", "c", "", "set path of configuration file")
runCommand.Flags().BoolVarP(&serverMode, "server", "s", false, "start the runtime in server mode")
runCommand.Flags().StringVarP(&params.HistoryPath, "history", "H", historyPath(), "set path of history file")
runCommand.Flags().StringVarP(&params.Addr, "addr", "a", defaultAddr, "set listening address of the server (e.g., [ip]:<port> for TCP, unix://<path> for UNIX domain socket)")
params.Addrs = runCommand.Flags().StringSliceP("addr", "a", []string{defaultAddr}, "set listening address of the server (e.g., [ip]:<port> for TCP, unix://<path> for UNIX domain socket)")
runCommand.Flags().StringVarP(&params.InsecureAddr, "insecure-addr", "", "", "set insecure listening address of the server")
runCommand.Flags().StringVarP(&params.OutputFormat, "format", "f", "pretty", "set shell output format, i.e, pretty, json")
runCommand.Flags().BoolVarP(&params.Watch, "watch", "w", false, "watch command line files for changes")
Expand Down
8 changes: 4 additions & 4 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type Params struct {
// the runtime will generate one.
ID string

// Addr is the listening address that the OPA server will bind to.
Addr string
// Addrs are the listening addresses that the OPA server will bind to.
Addrs *[]string

// InsecureAddr is the listening address that the OPA server will bind to
// in addition to Addr if TLS is enabled.
Expand Down Expand Up @@ -212,7 +212,7 @@ func (rt *Runtime) StartServer(ctx context.Context) {
setupLogging(rt.Params.Logging)

logrus.WithFields(logrus.Fields{
"addr": rt.Params.Addr,
"addrs": *rt.Params.Addrs,
"insecure_addr": rt.Params.InsecureAddr,
}).Infof("First line of log stream.")

Expand All @@ -224,7 +224,7 @@ func (rt *Runtime) StartServer(ctx context.Context) {
WithStore(rt.Store).
WithManager(rt.Manager).
WithCompilerErrorLimit(rt.Params.ErrorLimit).
WithAddress(rt.Params.Addr).
WithAddresses(*rt.Params.Addrs).
WithInsecureAddress(rt.Params.InsecureAddr).
WithCertificate(rt.Params.Certificate).
WithAuthentication(rt.Params.Authentication).
Expand Down
52 changes: 31 additions & 21 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var systemMainPath = ast.MustParseRef("data.system.main")
type Server struct {
Handler http.Handler

addr string
addrs []string
insecureAddr string
authentication AuthenticationScheme
authorization AuthorizationScheme
Expand Down Expand Up @@ -218,9 +218,9 @@ func (s *Server) Init(ctx context.Context) (*Server, error) {
return s, s.store.Commit(ctx, txn)
}

// WithAddress sets the listening address that the server will bind to.
func (s *Server) WithAddress(addr string) *Server {
s.addr = addr
// WithAddresses sets the listening addresses that the server will bind to.
func (s *Server) WithAddresses(addrs []string) *Server {
s.addrs = addrs
return s
}

Expand Down Expand Up @@ -287,26 +287,36 @@ func (s *Server) WithDecisionIDFactory(f func() string) *Server {

// Listeners returns functions that listen and serve connections.
func (s *Server) Listeners() ([]Loop, error) {
if !strings.Contains(s.addr, "://") {
scheme := "http://"
if s.cert != nil {
scheme = "https://"
loops := []Loop{}
for _, addr := range s.addrs {
if !strings.Contains(addr, "://") {
scheme := "http://"
if s.cert != nil {
scheme = "https://"
}
addr = scheme + addr
}
s.addr = scheme + s.addr
}
parsedURL, err := url.Parse(s.addr)
parsedURL, err := url.Parse(addr)

if err != nil {
return nil, err
}
switch parsedURL.Scheme {
case "unix":
return s.getListenerForUNIXSocket(parsedURL)
case "http", "https":
return s.getListenerForHTTPServer(parsedURL)
}
if err != nil {
return nil, err
}
returnedLoops := []Loop{}
switch parsedURL.Scheme {
case "unix":
returnedLoops, err = s.getListenerForUNIXSocket(parsedURL)
case "http", "https":
returnedLoops, err = s.getListenerForHTTPServer(parsedURL)
default:
return nil, fmt.Errorf("invalid url scheme %q", parsedURL.Scheme)
}

return nil, fmt.Errorf("invalid url scheme %q", parsedURL.Scheme)
if err != nil {
return nil, err
}
loops = append(loops, returnedLoops...)
}
return loops, nil
}

func (s *Server) getListenerForHTTPServer(u *url.URL) ([]Loop, error) {
Expand Down
8 changes: 4 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ func TestQueryWatchMigrateInvalidate(t *testing.T) {
func TestDiagnostics(t *testing.T) {
f := newFixture(t)
f.server, _ = New().
WithAddress(":8182").
WithAddresses([]string{":8182"}).
WithStore(f.server.store).
WithManager(f.server.manager).
WithDiagnosticsBuffer(NewBoundedBuffer(8)).
Expand Down Expand Up @@ -2080,7 +2080,7 @@ func TestAuthorization(t *testing.T) {
}

server, err := New().
WithAddress(":8182").
WithAddresses([]string{":8182"}).
WithStore(store).
WithManager(m).
WithAuthorization(AuthorizationBasic).
Expand Down Expand Up @@ -2222,7 +2222,7 @@ func TestQueryBindingIterationError(t *testing.T) {
panic(err)
}

server, err := New().WithStore(mock).WithManager(m).WithAddress(":8182").Init(ctx)
server, err := New().WithStore(mock).WithManager(m).WithAddresses([]string{":8182"}).Init(ctx)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -2271,7 +2271,7 @@ func newFixture(t *testing.T) *fixture {
}

server, err := New().
WithAddress(":8182").
WithAddresses([]string{":8182"}).
WithStore(store).
WithManager(m).
Init(ctx)
Expand Down

0 comments on commit e6bb5d6

Please sign in to comment.