8000 Add HandlerAPI() function to stratoscale template by maxatome · Pull Request #1821 · go-swagger/go-swagger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 3 commits into from
Dec 3, 2018
Merged

Add HandlerAPI() function to stratoscale template #1821

merged 3 commits into from
Dec 3, 2018

Conversation

maxatome
Copy link
Contributor

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:

route, ok := api.Context().LookupRoute(req) // req is a *http.Request
if ok {
  return route.PathPattern // /pet/{id} here
}

Note: I go generated generator/bindata.go but did not go fmted it coz it seems it was not before...

@fredbi
Copy link
Contributor
fredbi commented Nov 29, 2018

@posener, @michaelf-stratoscale could you please review this PR?

@codecov
Copy link
codecov bot commented Nov 29, 2018

Codecov Report

Merging #1821 into master will increase coverage by 0.19%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
generator/bindata.go 70.92% <77.77%> (+2.57%) ⬆️
scan/validators.go 76.67% <0%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b824dd...9a6fa6f. Read the comment docs.

@michaelf-stratoscale
Copy link
Contributor

@maxatome i don't understand where are you going to use this api, this file is auto generated and shouldn't be changed, so when you return it to Handler you are not using it.

@maxatome
Copy link
Contributor Author
maxatome commented Nov 29, 2018

@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?

@michaelf-stratoscale
Copy link
Contributor

@maxatome please make sure it don't break existing functionality

@maxatome
Copy link
Contributor Author

@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>
@maxatome
Copy link
Contributor Author
maxatome commented Dec 3, 2018

I just signed the commit

@casualjim casualjim merged commit c30a20b into go-swagger:master Dec 3, 2018
@maxatome maxatome deleted the stratoscale_tmpl_api_access branch December 3, 2018 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0