diff --git a/cli/start.go b/cli/start.go index 0bd1510008..ad2f79c928 100644 --- a/cli/start.go +++ b/cli/start.go @@ -135,6 +135,7 @@ func MakeStartCommand() *cobra.Command { } isDevMode := cfg.GetBool("development") + http.IsDevMode = isDevMode if isDevMode { cmd.Printf(devModeBanner) if cfg.GetBool("keyring.disabled") { diff --git a/http/errors.go b/http/errors.go index f1e03d5882..0b6cc78d3d 100644 --- a/http/errors.go +++ b/http/errors.go @@ -19,9 +19,10 @@ import ( ) const ( - errFailedToLoadKeys string = "failed to load given keys" - errMethodIsNotImplemented string = "the method is not implemented" - errFailedToGetContext string = "failed to get context" + errFailedToLoadKeys string = "failed to load given keys" + errMethodIsNotImplemented string = "the method is not implemented" + errFailedToGetContext string = "failed to get context" + errPurgeRequestNonDeveloperMode string = "cannot purge database when development mode is disabled" ) // Errors returnable from this package. diff --git a/http/handler.go b/http/handler.go index 336dfc54d3..7d24245ea4 100644 --- a/http/handler.go +++ b/http/handler.go @@ -21,6 +21,10 @@ import ( "github.com/go-chi/chi/v5" ) +// Global variable for the development mode flag +// This is checked by the http/handler_extras.go/Purge function to determine which response to send +var IsDevMode bool = false + // Version is the identifier for the current API version. var Version string = "v0" diff --git a/http/handler_extras.go b/http/handler_extras.go index 1f14cc40a7..67cba8d0ef 100644 --- a/http/handler_extras.go +++ b/http/handler_extras.go @@ -23,7 +23,14 @@ type extrasHandler struct{} func (s *extrasHandler) Purge(rw http.ResponseWriter, req *http.Request) { db := mustGetContextClientDB(req) - rw.WriteHeader(http.StatusOK) // write the response before we restart to purge + + // Send either 200 or 400 response based on whether the server is in dev mode + if IsDevMode { + rw.WriteHeader(http.StatusOK) + } else { + responseJSON(rw, http.StatusBadRequest, errPurgeRequestNonDeveloperMode) + } + db.Events().Publish(event.NewMessage(event.PurgeName, nil)) } diff --git a/http/handler_extras_test.go b/http/handler_extras_test.go index d7d1398e90..870cad02d3 100644 --- a/http/handler_extras_test.go +++ b/http/handler_extras_test.go @@ -20,8 +20,11 @@ import ( "github.com/stretchr/testify/require" ) -func TestPurge(t *testing.T) { +func TestPurgeDevModeTrue(t *testing.T) { cdb := setupDatabase(t) + + IsDevMode = true + url := "http://localhost:9181/api/v0/purge" req := httptest.NewRequest(http.MethodPost, url, nil) @@ -40,3 +43,27 @@ func TestPurge(t *testing.T) { // test will timeout if purge never received <-purgeSub.Message() } + +func TestPurgeDevModeFalse(t *testing.T) { + cdb := setupDatabase(t) + + IsDevMode = false + + url := "http://localhost:9181/api/v0/purge" + + req := httptest.NewRequest(http.MethodPost, url, nil) + rec := httptest.NewRecorder() + + purgeSub, err := cdb.Events().Subscribe(event.PurgeName) + require.NoError(t, err) + + handler, err := NewHandler(cdb) + require.NoError(t, err) + handler.ServeHTTP(rec, req) + + res := rec.Result() + require.Equal(t, 400, res.StatusCode) + + // test will timeout if purge never received + <-purgeSub.Message() +}