-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add HandlerAPI() function to strato 8000 scale template #1821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HandlerAPI() function to stratoscale template #1821
Conversation
@posener, @michaelf-stratoscale could you please review this PR? |
Codecov Report
@@ Coverage Diff @@
## master #1821 +/- ##
==========================================
+ Coverage 80.47% 80.66% +0.19%
==========================================
Files 38 38
Lines 7544 7521 -23
==========================================
- Hits 6071 6067 -4
+ Misses 997 978 -19
Partials 476 476
Continue to review full report at Codecov.
|
@maxatome i don't understand where are you going to use this |
@michaelf-stratoscale in my main() func I have: handler, err := restapi.Handler(restapi.Config{...})
if err != nil {
logrus.Fatal(err)
} then I chain this handler to a more generic one that handles authentication, request signature, logs and so on: handler = GenericMiddleware(handler, GenericMiddlewareConfig{...}) In this generic middleware, I log incoming requests, their method, path, status, duration. Later I can analyse these logs to know how much time is spent in this method or this other one... But the Request.Path is not a good input for comparing routes amongst themselves. On the other hand, the route.PathPattern is a good input, as it does not depend on user parameters. To access this route.PathPattern, I need the api.Context() and so the api instance. In my case, with this PR, I could do: handler, api, err := restapi.HandlerAPI(restapi.Config{...})
if err != nil {
logrus.Fatal(err)
}
handler = GenericMiddleware(handler, GenericMiddlewareConfig{
CanonicalizePath: func(req *http.Request) string {
route, ok := api.Context().LookupRoute(req)
if ok {
return route.PathPattern // /pet/{id} here
}
return ""
},
...
}) So my log feature in my generic middleware just have to call the CanonicalizePath closure with the request to get the path pattern from the request it currently handles... Perhaps you know another way to achieve this? |
@maxatome please make sure it don't break existing functionality |
@michaelf-stratoscale I tested it on my side and I didn't see any problem. |
It allows to access API instance. Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
I just signed the commit |
It allows to access API instance.
The use case is to allow logging of route pattern (eg.
/pet/{id}
).The route pattern can be retrieved using the following code, but the API instance is needed:
Note: I
go generate
d generator/bindata.go but did notgo fmt
ed it coz it seems it was not before...