Skip to content

Commit

Permalink
Add new v1 prefixed route (#76)
Browse files Browse the repository at this point in the history
* Add new v1 prefixed route

The route is duplicated, so it keeps compatibility.

* Make integration test use the new route
  • Loading branch information
jademcosta committed Sep 8, 2023
1 parent b3af025 commit 0717479
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/adapters/http_in/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

const API_COMPONENT_TYPE = "api"
const apiVersion = "v1"

type Api struct {
mux *chi.Mux
Expand Down Expand Up @@ -46,7 +47,7 @@ func New(l *zap.SugaredLogger, conf config.ApiConfig, metricRegistry *prometheus
initializeMetrics(metricRegistry)
registerDefaultMiddlewares(api, sizeLimit, logg, metricRegistry)

RegisterIngestingRoutes(api, flws)
RegisterIngestingRoutes(api, apiVersion, flws)
RegisterOperatinalRoutes(api, appVersion, metricRegistry)
api.mux.Mount("/debug", middleware.Profiler())

Expand Down
16 changes: 14 additions & 2 deletions pkg/adapters/http_in/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ func TestPassesDataFlows(t *testing.T) {

resp, err = http.Post(fmt.Sprintf("%s/flow2/async_ingestion", srvr.URL), "application/json", strings.NewReader("world!"))
assert.NoError(t, err, "error on posting data to flow2", err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "status should be OK(200) on flow 2")

//Same test, but with v1 on URL
resp, err = http.Post(fmt.Sprintf("%s/v1/flow-1/async_ingestion", srvr.URL), "application/json", strings.NewReader("helloooooo1"))
assert.NoError(t, err, "error on posting data to flow1", err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "status should be OK(200) on flow 1")

resp, err = http.Post(fmt.Sprintf("%s/v1/flow2/async_ingestion", srvr.URL), "application/json", strings.NewReader("world2"))
assert.NoError(t, err, "error on posting data to flow2", err)
resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "status should be OK(200) on flow 2")

assert.Equal(t, [][]byte{[]byte("helloooooo")}, mockDF.calledWith, "the posted data should have been sent to flow 1")
assert.Equal(t, [][]byte{[]byte("world!")}, mockDF2.calledWith, "the posted data should have been sent to flow 2")
assert.Equal(t, [][]byte{[]byte("helloooooo"), []byte("helloooooo1")},
mockDF.calledWith, "the posted data should have been sent to flow 1")
assert.Equal(t, [][]byte{[]byte("world!"), []byte("world2")},
mockDF2.calledWith, "the posted data should have been sent to flow 2")
}

func TestAnswersAnErrorIfNoBodyIsSent(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions pkg/adapters/http_in/ingest_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ var payloadMaxSizeErr *http.MaxBytesError

func RegisterIngestingRoutes(
api *Api,
version string,
flws []flow.Flow,
) {

for _, flw := range flws {
flwCopy := flw

path := fmt.Sprintf("/%s/async_ingestion", flwCopy.Name)
if flwCopy.Token != "" {
api.mux.With(httpmiddleware.Auth(flwCopy.Token)).
Post(fmt.Sprintf("/%s/async_ingestion", flwCopy.Name),
asyncIngestion(api.log, &flwCopy))
Post(path, asyncIngestion(api.log, &flwCopy))
api.mux.With(httpmiddleware.Auth(flwCopy.Token)).
Post("/"+version+path, asyncIngestion(api.log, &flwCopy))
} else {
api.mux.Post(fmt.Sprintf("/%s/async_ingestion", flwCopy.Name),
asyncIngestion(api.log, &flwCopy))
api.mux.Post(path, asyncIngestion(api.log, &flwCopy))
api.mux.Post("/"+version+path, asyncIngestion(api.log, &flwCopy))
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func testWithBatchSize(t *testing.T, confYaml string, stringExemplarSizes ...int

generatedValuesWithoutAcc = append(generatedValuesWithoutAcc, expected)

response, err = http.Post("http://localhost:9099/int_flow2/async_ingestion", "application/json", strings.NewReader(expected))
response, err = http.Post("http://localhost:9099/v1/int_flow2/async_ingestion", "application/json", strings.NewReader(expected))
assert.NoError(t, err, "enqueueing items should not return error on int_flow2")
assert.Equal(t, 200, response.StatusCode, "data enqueueing should have been successful on int_flow2")
response.Body.Close()
Expand Down

0 comments on commit 0717479

Please sign in to comment.