From 913c13f6e4c54bbb7a0e0149d1d485262bd5e465 Mon Sep 17 00:00:00 2001 From: David Aronchick Date: Fri, 5 Jan 2024 10:45:11 -0800 Subject: [PATCH] 200 for api root (#3158) Co-authored-by: Simon Worthington --- .gitprecommit/codespell_ignore_words.txt | 5 ++- cmd/cli/serve/serve.go | 5 +-- cmd/cli/serve/serve_test.go | 41 ++++++++++++++++++----- pkg/config/configenv/dev.go | 2 +- pkg/config/configenv/local.go | 2 +- pkg/config/configenv/production.go | 2 +- pkg/config/configenv/staging.go | 2 +- pkg/config/configenv/test.go | 2 +- pkg/publicapi/endpoint/shared/endpoint.go | 17 ++++++++++ 9 files changed, 62 insertions(+), 16 deletions(-) diff --git a/.gitprecommit/codespell_ignore_words.txt b/.gitprecommit/codespell_ignore_words.txt index 552c0f32f2..ce145e8900 100644 --- a/.gitprecommit/codespell_ignore_words.txt +++ b/.gitprecommit/codespell_ignore_words.txt @@ -90,4 +90,7 @@ jbtj Dreambooth testdata modtidy -codespellrc \ No newline at end of file +codespellrc +zerolog +labstack +stretchr diff --git a/cmd/cli/serve/serve.go b/cmd/cli/serve/serve.go index 17e9ec2c4f..9e0ffbc18a 100644 --- a/cmd/cli/serve/serve.go +++ b/cmd/cli/serve/serve.go @@ -26,6 +26,7 @@ import ( ) var DefaultSwarmPort = 1235 +var DefaultWebPort = 8483 const DefaultPeerConnect = "none" @@ -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 `)) ) @@ -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 { diff --git a/cmd/cli/serve/serve_test.go b/cmd/cli/serve/serve_test.go index a8d92b9cba..72564b5000 100644 --- a/cmd/cli/serve/serve_test.go +++ b/cmd/cli/serve/serve_test.go @@ -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() { @@ -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" diff --git a/pkg/config/configenv/dev.go b/pkg/config/configenv/dev.go index 90c4fb44bd..93a00e6682 100644 --- a/pkg/config/configenv/dev.go +++ b/pkg/config/configenv/dev.go @@ -71,7 +71,7 @@ var Development = types.BacalhauConfig{ Requester: DevelopmentRequesterConfig, WebUI: types.WebUIConfig{ Enabled: false, - Port: 80, + Port: 8483, }, }, } diff --git a/pkg/config/configenv/local.go b/pkg/config/configenv/local.go index 8600a584cf..2496699994 100644 --- a/pkg/config/configenv/local.go +++ b/pkg/config/configenv/local.go @@ -62,7 +62,7 @@ var Local = types.BacalhauConfig{ Requester: LocalRequesterConfig, WebUI: types.WebUIConfig{ Enabled: false, - Port: 80, + Port: 8483, }, }, } diff --git a/pkg/config/configenv/production.go b/pkg/config/configenv/production.go index bbe4c6b2e9..599b5c2b61 100644 --- a/pkg/config/configenv/production.go +++ b/pkg/config/configenv/production.go @@ -79,7 +79,7 @@ var Production = types.BacalhauConfig{ Requester: ProductionRequesterConfig, WebUI: types.WebUIConfig{ Enabled: false, - Port: 80, + Port: 8483, }, }, } diff --git a/pkg/config/configenv/staging.go b/pkg/config/configenv/staging.go index 6cf6c7ee26..54565d33dc 100644 --- a/pkg/config/configenv/staging.go +++ b/pkg/config/configenv/staging.go @@ -77,7 +77,7 @@ var Staging = types.BacalhauConfig{ Requester: StagingRequesterConfig, WebUI: types.WebUIConfig{ Enabled: false, - Port: 80, + Port: 8483, }, }, } diff --git a/pkg/config/configenv/test.go b/pkg/config/configenv/test.go index bd1f2b8770..6cd6fdd009 100644 --- a/pkg/config/configenv/test.go +++ b/pkg/config/configenv/test.go @@ -66,7 +66,7 @@ var Testing = types.BacalhauConfig{ Requester: TestingRequesterConfig, WebUI: types.WebUIConfig{ Enabled: false, - Port: 80, + Port: 8483, }, }, } diff --git a/pkg/publicapi/endpoint/shared/endpoint.go b/pkg/publicapi/endpoint/shared/endpoint.go index 46f8bbd5e2..801bb73cfe 100644 --- a/pkg/publicapi/endpoint/shared/endpoint.go +++ b/pkg/publicapi/endpoint/shared/endpoint.go @@ -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 } @@ -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()) +}