-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): migrate v1 to v1beta1 (#61)
* refactor(api): migrate v1 to v1beta1 * fix: add default notfound and methodnotallowed handlers
- Loading branch information
Showing
7 changed files
with
180 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func NotFound(w http.ResponseWriter, r *http.Request) { | ||
writeJSONError(w, http.StatusNotFound, mux.ErrNotFound.Error()) | ||
} | ||
|
||
func MethodNotAllowed(w http.ResponseWriter, r *http.Request) { | ||
writeJSONError(w, http.StatusMethodNotAllowed, mux.ErrMethodMismatch.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/odpf/columbus/api/handlers" | ||
) | ||
|
||
func TestNotFoundHandler(t *testing.T) { | ||
handler := http.HandlerFunc(handlers.NotFound) | ||
rr := httptest.NewRequest("GET", "/xxx", nil) | ||
rw := httptest.NewRecorder() | ||
handler.ServeHTTP(rw, rr) | ||
if rw.Code != 404 { | ||
t.Errorf("expected handler to respond with HTTP 404, got HTTP %d instead", rw.Code) | ||
return | ||
} | ||
expectedResponse := "{\"reason\":\"no matching route was found\"}\n" | ||
actualResponse := rw.Body.String() | ||
if actualResponse != expectedResponse { | ||
t.Errorf("expected handler response to be %q, was %q instead", expectedResponse, actualResponse) | ||
return | ||
} | ||
} | ||
|
||
func TestMethodNotAllowedHandler(t *testing.T) { | ||
handler := http.HandlerFunc(handlers.MethodNotAllowed) | ||
rr := httptest.NewRequest("POST", "/ping", nil) | ||
rw := httptest.NewRecorder() | ||
handler.ServeHTTP(rw, rr) | ||
if rw.Code != 405 { | ||
t.Errorf("expected handler to respond with HTTP 405, got HTTP %d instead", rw.Code) | ||
return | ||
} | ||
expectedResponse := "{\"reason\":\"method is not allowed\"}\n" | ||
actualResponse := rw.Body.String() | ||
if actualResponse != expectedResponse { | ||
t.Errorf("expected handler response to be %q, was %q instead", expectedResponse, actualResponse) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/odpf/columbus/api/handlers" | ||
) | ||
|
||
func setupV1Beta1Router(router *mux.Router, handlers *Handlers) *mux.Router { | ||
setupV1Beta1TypeRoutes(router, handlers.Type, handlers.Record) | ||
setupV1Beta1TagRoutes(router, "/tags", handlers.Tag, handlers.TagTemplate) | ||
|
||
router.Path("/search"). | ||
Methods(http.MethodGet). | ||
HandlerFunc(handlers.Search.Search) | ||
|
||
router.Path("/search/suggest"). | ||
Methods(http.MethodGet). | ||
HandlerFunc(handlers.Search.Suggest) | ||
|
||
router.PathPrefix("/lineage/{type}/{id}"). | ||
Methods(http.MethodGet). | ||
HandlerFunc(handlers.Lineage.GetLineage) | ||
|
||
router.PathPrefix("/lineage"). | ||
Methods(http.MethodGet). | ||
HandlerFunc(handlers.Lineage.ListLineage) | ||
|
||
return router | ||
} | ||
|
||
func setupV1Beta1TypeRoutes(router *mux.Router, th *handlers.TypeHandler, rh *handlers.RecordHandler) { | ||
typeURL := "/types" | ||
|
||
router.Path(typeURL). | ||
Methods(http.MethodGet, http.MethodHead). | ||
HandlerFunc(th.Get) | ||
|
||
recordURL := "/types/{name}/records" | ||
router.Path(recordURL). | ||
Methods(http.MethodPut, http.MethodHead). | ||
HandlerFunc(rh.UpsertBulk) | ||
|
||
router.Path(recordURL). | ||
Methods(http.MethodGet, http.MethodHead). | ||
HandlerFunc(rh.GetByType) | ||
|
||
router.Path(recordURL+"/{id}"). | ||
Methods(http.MethodGet, http.MethodHead). | ||
HandlerFunc(rh.GetOneByType) | ||
|
||
router.Path(recordURL+"/{id}"). | ||
Methods(http.MethodDelete, http.MethodHead). | ||
HandlerFunc(rh.Delete) | ||
|
||
} | ||
|
||
func setupV1Beta1TagRoutes(router *mux.Router, baseURL string, th *handlers.TagHandler, tth *handlers.TagTemplateHandler) { | ||
router.Methods(http.MethodPost).Path(baseURL).HandlerFunc(th.Create) | ||
|
||
url := baseURL + "/types/{type}/records/{record_urn}/templates/{template_urn}" | ||
router.Methods(http.MethodGet).Path(url).HandlerFunc(th.FindByRecordAndTemplate) | ||
router.Methods(http.MethodPut).Path(url).HandlerFunc(th.Update) | ||
router.Methods(http.MethodDelete).Path(url).HandlerFunc(th.Delete) | ||
|
||
router.Methods(http.MethodGet).Path(baseURL + "/types/{type}/records/{record_urn}").HandlerFunc(th.GetByRecord) | ||
|
||
templateURL := baseURL + "/templates" | ||
router.Methods(http.MethodGet).Path(templateURL).HandlerFunc(tth.Index) | ||
router.Methods(http.MethodPost).Path(templateURL).HandlerFunc(tth.Create) | ||
router.Methods(http.MethodGet).Path(templateURL + "/{template_urn}").HandlerFunc(tth.Find) | ||
router.Methods(http.MethodPut).Path(templateURL + "/{template_urn}").HandlerFunc(tth.Update) | ||
router.Methods(http.MethodDelete).Path(templateURL + "/{template_urn}").HandlerFunc(tth.Delete) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters