Skip to content

Commit

Permalink
200 for api root (#3158)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Worthington <[email protected]>
  • Loading branch information
aronchick and simonwo authored Jan 5, 2024
1 parent aa2d7c2 commit 913c13f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .gitprecommit/codespell_ignore_words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ jbtj
Dreambooth
testdata
modtidy
codespellrc
codespellrc
zerolog
labstack
stretchr
5 changes: 3 additions & 2 deletions cmd/cli/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

var DefaultSwarmPort = 1235
var DefaultWebPort = 8483

const DefaultPeerConnect = "none"

Expand All @@ -51,7 +52,7 @@ var (
# Start a public bacalhau requester node
bacalhau serve --peer env --private-internal-ipfs=false
# Start a public bacalhau node with the WebUI on port 3000 (default:80)
# Start a public bacalhau node with the WebUI on port 3000 (default:8483)
bacalhau serve --web-ui --web-ui-port=3000
`))
)
Expand Down Expand Up @@ -281,7 +282,7 @@ func serve(cmd *cobra.Command) error {
return err
}

// Start up Dashboard
// Start up Dashboard - default: 8483
if startWebUI {
listenPort, err := config.Get[int](types.NodeWebUIPort)
if err != nil {
Expand Down
41 changes: 33 additions & 8 deletions cmd/cli/serve/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,41 +134,42 @@ func (s *ServeSuite) serve(extraArgs ...string) (uint16, error) {
}
s.FailNow("Server did not start in time")
case <-t.C:
livezText, _ := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/api/v1/livez", port))
if string(livezText) == "OK" {
livezText, statusCode, _ := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/api/v1/livez", port))
if string(livezText) == "OK" && statusCode == http.StatusOK {
return port, nil
}
}
}
}

func (s *ServeSuite) curlEndpoint(URL string) ([]byte, error) {
func (s *ServeSuite) curlEndpoint(URL string) ([]byte, int, error) {
req, err := http.NewRequestWithContext(s.ctx, "GET", URL, nil)
if err != nil {
return nil, err
return nil, http.StatusServiceUnavailable, err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
return nil, http.StatusServiceUnavailable, err
}
defer closer.DrainAndCloseWithLogOnError(s.ctx, "test", resp.Body)

responseText, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, resp.StatusCode, err
}

return responseText, nil
return responseText, resp.StatusCode, nil
}

func (s *ServeSuite) TestHealthcheck() {
port, _ := s.serve()
healthzText, err := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/api/v1/healthz", port))
healthzText, statusCode, err := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/api/v1/healthz", port))
s.Require().NoError(err)
var healthzJSON types.HealthInfo
s.Require().NoError(marshaller.JSONUnmarshalWithMax(healthzText, &healthzJSON), "Error unmarshalling healthz JSON.")
s.Require().Greater(int(healthzJSON.DiskFreeSpace.ROOT.All), 0, "Did not report DiskFreeSpace > 0.")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

func (s *ServeSuite) TestAPIPrintedForComputeNode() {
Expand Down Expand Up @@ -288,6 +289,30 @@ func (s *ServeSuite) TestGetPeers() {
s.Require().Error(err)
}

// Begin WebUI Tests
func (s *ServeSuite) Test200ForNotStartingWebUI() {
port, err := s.serve()
s.Require().NoError(err, "Error starting server")

content, statusCode, err := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/", port))
_ = content
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

func (s *ServeSuite) Test200ForRoot() {
webUIPort, err := freeport.GetFreePort()
if err != nil {
s.T().Fatal(err, "Could not get port for web-ui")
}
_, err = s.serve("--web-ui", "--web-ui-port", fmt.Sprintf("%d", webUIPort))
s.Require().NoError(err, "Error starting server")

_, statusCode, err := s.curlEndpoint(fmt.Sprintf("http://127.0.0.1:%d/", webUIPort))
s.Require().NoError(err, "Error curling root endpoint")
s.Require().Equal(http.StatusOK, statusCode, "Did not return 200 OK.")
}

// TODO: Can't figure out how to make this test work, it spits out the help text
// func (s *ServeSuite) TestBadBacalhauDir() {
// badDirString := "/BADDIR"
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configenv/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var Development = types.BacalhauConfig{
Requester: DevelopmentRequesterConfig,
WebUI: types.WebUIConfig{
Enabled: false,
Port: 80,
Port: 8483,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configenv/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var Local = types.BacalhauConfig{
Requester: LocalRequesterConfig,
WebUI: types.WebUIConfig{
Enabled: false,
Port: 80,
Port: 8483,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configenv/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var Production = types.BacalhauConfig{
Requester: ProductionRequesterConfig,
WebUI: types.WebUIConfig{
Enabled: false,
Port: 80,
Port: 8483,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configenv/staging.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var Staging = types.BacalhauConfig{
Requester: StagingRequesterConfig,
WebUI: types.WebUIConfig{
Enabled: false,
Port: 80,
Port: 8483,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/configenv/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var Testing = types.BacalhauConfig{
Requester: TestingRequesterConfig,
WebUI: types.WebUIConfig{
Enabled: false,
Port: 80,
Port: 8483,
},
},
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/publicapi/endpoint/shared/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func NewEndpoint(params EndpointParams) *Endpoint {
pt.GET("/id", e.id)
pt.GET("/livez", e.livez)

// Home group
// TODO: Could we use this to redirect to latest API?
h := e.router.Group("/")
h.Use(middleware.SetContentType(echo.MIMETextPlain))
h.GET("", e.home)

return e
}

Expand Down Expand Up @@ -145,3 +151,14 @@ func (e *Endpoint) livez(c echo.Context) error {
// Extremely simple liveness check (should be fine to be public / no-auth)
return c.String(http.StatusOK, "OK")
}

// home godoc
//
// @ID home
// @Tags Utils
// @Produce text/plain
// @Success 200 {object} string ""
// @Router / [get]
func (e *Endpoint) home(c echo.Context) error {
return c.JSON(http.StatusOK, version.Get())
}

0 comments on commit 913c13f

Please sign in to comment.