Skip to content

Commit

Permalink
feat: enable listen address for booster-http (#1461)
Browse files Browse the repository at this point in the history
* enable listen address

* modify tests
  • Loading branch information
LexLuthr authored May 24, 2023
1 parent 24a9bb3 commit 4986e2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const testFile = "test/test_file"
func TestNewHttpServer(t *testing.T) {
// Create a new mock Http server
ctrl := gomock.NewController(t)
httpServer := NewHttpServer("", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err := httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
Expand All @@ -42,7 +42,7 @@ func TestHttpGzipResponse(t *testing.T) {
// Create a new mock Http server with custom functions
ctrl := gomock.NewController(t)
mockHttpServer := mocks_booster_http.NewMockHttpServerApi(ctrl)
httpServer := NewHttpServer("", 7777, mockHttpServer, nil)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mockHttpServer, nil)
err := httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestHttpInfo(t *testing.T) {

// Create a new mock Http server
ctrl := gomock.NewController(t)
httpServer := NewHttpServer("", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err := httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
Expand Down
2 changes: 1 addition & 1 deletion cmd/booster-http/mocks/mock_booster_http.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions cmd/booster-http/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ var runCmd = &cli.Command{
Usage: "the base path at which to run the web server",
Value: "",
},
&cli.StringFlag{
Name: "address",
Aliases: []string{"addr"},
Usage: "the listen address for the web server",
Value: "0.0.0.0",
},
&cli.UintFlag{
Name: "port",
Usage: "the port the web server listens on",
Expand Down Expand Up @@ -201,14 +207,15 @@ var runCmd = &cli.Command{
sapi := serverApi{ctx: ctx, bapi: bapi, sa: sa}
server := NewHttpServer(
cctx.String("base-path"),
cctx.String("address"),
cctx.Int("port"),
sapi,
opts,
)

// Start the server
log.Infof("Starting booster-http node on port %d with base path '%s'",
cctx.Int("port"), cctx.String("base-path"))
log.Infof("Starting booster-http node on listen address %s and port %d with base path '%s'",
cctx.String("address"), cctx.Int("port"), cctx.String("base-path"))
err = server.Start(ctx)
if err != nil {
return fmt.Errorf("starting http server: %w", err)
Expand Down
17 changes: 9 additions & 8 deletions cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ type apiVersion struct {
}

type HttpServer struct {
path string
port int
api HttpServerApi
opts HttpServerOptions
idxPage string
path string
listenAddr string
port int
api HttpServerApi
opts HttpServerOptions
idxPage string

ctx context.Context
cancel context.CancelFunc
Expand All @@ -65,11 +66,11 @@ type HttpServerOptions struct {
SupportedResponseFormats []string
}

func NewHttpServer(path string, port int, api HttpServerApi, opts *HttpServerOptions) *HttpServer {
func NewHttpServer(path string, listenAddr string, port int, api HttpServerApi, opts *HttpServerOptions) *HttpServer {
if opts == nil {
opts = &HttpServerOptions{ServePieces: true}
}
return &HttpServer{path: path, port: port, api: api, opts: *opts, idxPage: parseTemplate(*opts)}
return &HttpServer{path: path, listenAddr: listenAddr, port: port, api: api, opts: *opts, idxPage: parseTemplate(*opts)}
}

func (s *HttpServer) pieceBasePath() string {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *HttpServer) Start(ctx context.Context) error {
handler.HandleFunc("/info", s.handleInfo)
handler.Handle("/metrics", metrics.Exporter("booster_http")) // metrics
s.server = &http.Server{
Addr: fmt.Sprintf(":%d", s.port),
Addr: fmt.Sprintf("%s:%d", s.listenAddr, s.port),
Handler: handler,
// This context will be the parent of the context associated with all
// incoming requests
Expand Down

0 comments on commit 4986e2f

Please sign in to comment.