-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapt the data service's request handlers to rest.HandlerFunc
When I started working on the alerts configs, I had a discussion with Todd about routing, and how the data service for some unknown reason uses its own authentication method, rather than the various api.Require* methods provided by platform and used in the other services. In the process of working on #671 it became clear to me that the reason was the difference in the function signatures of request handlers in the data service as opposed to those in other services. Specifically, data service request handlers have this signature: func Foo(service.Context) Whereas rest.HandlerFunc has this signature: func Foo(rest.ResponseWriter, *rest.Request) So I modified the data service's MakeRoute function to accept a list of rest.MiddlewareSimple, and added a ToRestRoute method that when provided with the existing adapter function in the data api service, would convert the data service routes to *rest.Route, while also applying middleware. The result is that data service routes can be specified including rest.MiddlewareSimple (like api.Require), and we no longer require a separate Authentication helper for data service routes. There are some TODO comments that indicate that going further down the data service handler rabbit hole might not have been the intended path, but rather moving to a rest.HandlerFunc signature was at one time the plan (in 2018 or so). If so, this should only make it clearer what needs to change to effect that migration.
- Loading branch information
Showing
15 changed files
with
145 additions
and
123 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
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 was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,15 +1,67 @@ | ||
package service | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ant0ine/go-json-rest/rest" | ||
) | ||
|
||
type Route struct { | ||
Method string | ||
Path string | ||
Handler HandlerFunc | ||
Handler HandlerFunc | ||
Method string | ||
Path string | ||
middleware []rest.MiddlewareSimple | ||
} | ||
|
||
func MakeRoute(method string, path string, handler HandlerFunc) Route { | ||
// MakeRoute builds a Route. | ||
// | ||
// Consider using the handy Get, Post, etc helpers. | ||
func MakeRoute(method string, path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return Route{ | ||
Method: method, | ||
Path: path, | ||
Handler: handler, | ||
Method: method, | ||
Path: path, | ||
Handler: handler, | ||
middleware: middleware, | ||
} | ||
} | ||
|
||
// Delete wraps MakeRoute for easy DELETE route creation. | ||
func Delete(path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return MakeRoute(http.MethodDelete, path, handler, middleware...) | ||
} | ||
|
||
// Get wraps MakeRoute for easy GET route creation. | ||
func Get(path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return MakeRoute(http.MethodGet, path, handler, middleware...) | ||
} | ||
|
||
// Patch wraps MakeRoute for easy PATCH route creation. | ||
func Patch(path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return MakeRoute(http.MethodPatch, path, handler, middleware...) | ||
} | ||
|
||
// Post wraps MakeRoute for easy POST route creation. | ||
func Post(path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return MakeRoute(http.MethodPost, path, handler, middleware...) | ||
} | ||
|
||
// Put wraps MakeRoute for easy PUT route creation. | ||
func Put(path string, handler HandlerFunc, middleware ...rest.MiddlewareSimple) Route { | ||
return MakeRoute(http.MethodPut, path, handler, middleware...) | ||
} | ||
|
||
// RestRouteAdapterFunc adapts a HandlerFunc to a rest.HandlerFunc. | ||
type RestRouteAdapterFunc (func(HandlerFunc) rest.HandlerFunc) | ||
|
||
// ToRestRoute converts a Route to a rest.Route. | ||
func (r *Route) ToRestRoute(f RestRouteAdapterFunc) *rest.Route { | ||
var middlewares []rest.Middleware | ||
for _, s := range r.middleware { | ||
middlewares = append(middlewares, rest.MiddlewareSimple(s)) | ||
} | ||
return &rest.Route{ | ||
HttpMethod: r.Method, | ||
PathExp: r.Path, | ||
Func: rest.WrapMiddlewares(middlewares, f(r.Handler)), | ||
} | ||
} |
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
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
Oops, something went wrong.