Skip to content
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

using the gin framework and dep #304

Closed
LewisWatson opened this issue Nov 8, 2017 · 5 comments
Closed

using the gin framework and dep #304

LewisWatson opened this issue Nov 8, 2017 · 5 comments

Comments

@LewisWatson
Copy link

LewisWatson commented Nov 8, 2017

Hello, I'm having some trouble getting api2go working with gin.

I followed the instructions on the main readme but I get the following error:

vendor/github.com/manyminds/api2go/routing/httprouter.go:51:26: cannot use notAllowedHandler (type http.Handler) as type http.HandlerFunc in assignment: need type assertion

I suspect that go get -tags=gingonic github.com/manyminds/api2go might not be working. I haven't encountered the -tags flag before, but does it not look for a "gingionic" tag in git? If so then that tag doesn't exist. I also plan to use go-dep, and I'm not sure how the -tags flag translates to a goPkg.toml file.

Example code available at https://github.com/LewisWatson/api2go-with-gin-example
Full build log available at https://travis-ci.org/LewisWatson/api2go-with-gin-example/jobs/298967492

Regards

Lewis

edit: fix typo

@sharpner
Copy link
Member

sharpner commented Nov 8, 2017

hi, sorry to hear that, we hoped that using build tags would make things easier.

The -tags flag has nothing to do with git. As you can see here it means that the gin router will then be included but not the other ones.

The error you have normally means you have outdated dependencies. Please try a clean gopath or update (go get -u -a).

Maybe this issue also helps: #292

Edit: Apparently build flags are currently not possible with dep golang/dep#291 but in the meantime you can still use the old variant for api2go which means installing api2go normally, and installing the matching adapter https://github.com/manyminds/api2go-adapter/tree/master/gingonic

@LewisWatson
Copy link
Author

Hi @sharpner, thanks for the reply. Knowing its a build tag has helped me get a bit further.

Support for dep sounds like a separate issue now, so I created a new branch in my example repo where I stripped out all the dep files. This helped me learn two things:

  1. go get always downloads routing/gingonic.go, but will ignore the dependencies of that file unless ran with the gingonic build tag.
  2. go build will always ignore routing/gingonic.go unless also ran with the gingonic build tag.
    • this is an additional step that isn't obvious from the gin section of the main readme.

I still want to use dep though. The lack of build tag support can be worked around by explicitly adding gin as a dependency.

Gopkg.toml

...

[[constraint]]
  name = "github.com/manyminds/api2go"
  version = "1.0-RC4"

[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "1.2"

I now encounter an error similar to the one in #292

go build -tags=gingonic
# github.com/LewisWatson/api2go-with-gin-example/vendor/github.com/manyminds/api2go/routing
vendor/github.com/manyminds/api2go/routing/httprouter.go:51:26: cannot use notAllowedHandler (type http.Handler) as type http.HandlerFunc in assignment: need type assertion

Pinning httprouter package to master didn't fix the error for me, unfortunatley.

There is hope that this is the last problem. As described in this comment, changing line 51 of vendor/github.com/manyminds/api2go/routing/httprouter.go from router.MethodNotAllowed = notAllowedHandler to router.MethodNotAllowed = notAllowedHandler.(http.HandlerFunc) does allow the app to build. So I plan to look into why that is next, hopefully it will lead to a proper fix.

I've updated the master branch of github.com/LewisWatson/api2go-with-gin-example to contain what I have so far (minus the vendor hack)

Finally, here is the full output of dep status

PROJECT                              CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
github.com/gedex/inflector           *              branch master  16278e9   16278e9  1  
github.com/gin-contrib/sse           *              branch master  22d885f   22d885f  1  
github.com/gin-gonic/gin             ^1.2.0         v1.2           d459835   d459835  3  
github.com/golang/protobuf           *              branch master  1643683   1643683  1  
github.com/gorilla/context           *              v1.1           1ea2538   1ea2538  1  
github.com/gorilla/mux               *              v1.6.0         7f08801   7f08801  1  
github.com/julienschmidt/httprouter  branch master  v1.1           8c199fb   e1b9828  1  
github.com/labstack/echo             *              3.2.3          cec7629   b28538b  1  
github.com/labstack/gommon           *              0.2.3          57409ad   57409ad  2  
github.com/manyminds/api2go          ^1.0.0-RC4     1.0-RC4        e7b6938   e7b6938  6  
github.com/mattn/go-colorable        *              v0.0.9         167de6b   167de6b  1  
github.com/mattn/go-isatty           *              v0.0.3         0360b2a   0360b2a  1  
github.com/ugorji/go                 *              branch master  00a57e0   427fecf  1  
github.com/valyala/bytebufferpool    *              branch master  e746df9   e746df9  1  
github.com/valyala/fasttemplate      *              branch master  dcecefd   dcecefd  1  
golang.org/x/crypto                  *              branch master  687d4b8   6a293f2  2  
golang.org/x/sys                     *              branch master  75813c6   1e2299c  1  
gopkg.in/go-playground/validator.v8  *              v8.18.2        5f1438d   5f1438d  1  
gopkg.in/yaml.v2                     *              branch v2      eb3733d   eb3733d  1 

@sharpner
Copy link
Member

sharpner commented Nov 10, 2017

Thanks for your thorough investigation!
That's right, we use the build tags explicitly so that one user who wants to use gin, does not inherit dependencies to N other frameworks as well :)

For the httprouter it seams you still have v1.1 which is a version from 2015. I think the master pinning did not work as you expected.

You can see this because REVISION and LATEST do not match. I linked this issue in httprouter as well, to further push the tagging of a new release ;)

@sharpner sharpner changed the title gin framework using the gin framework and dep Nov 10, 2017
@LewisWatson
Copy link
Author

Switching the github.com/julienschmidt/httprouter from a "constraint" to an "override" got me over the last hurdle to a working build 👍

This is how my gopkg.toml looks like now

[[constraint]]
  name = "github.com/manyminds/api2go"
  version = "1.0-RC4"

[[constraint]]
  name = "github.com/gin-gonic/gin"
  version = "1.2"

[[override]]
  name = "github.com/julienschmidt/httprouter"
  branch = "master"

dep status

PROJECT                              CONSTRAINT    VERSION        REVISION  LATEST   PKGS USED
github.com/gedex/inflector           *             branch master  16278e9   16278e9  1  
github.com/gin-contrib/sse           *             branch master  22d885f   22d885f  1  
github.com/gin-gonic/gin             ^1.2.0        v1.2           d459835   d459835  3  
github.com/golang/protobuf           *             branch master  1643683   1643683  1  
github.com/gorilla/context           *             v1.1           1ea2538   1ea2538  1  
github.com/gorilla/mux               *             v1.6.0         7f08801   7f08801  1  
github.com/julienschmidt/httprouter  * (override)  branch master  e1b9828   8c199fb  1  
github.com/labstack/echo             *             3.2.3          cec7629   b28538b  1  
github.com/labstack/gommon           *             0.2.3          57409ad   57409ad  2  
github.com/manyminds/api2go          ^1.0.0-RC4    1.0-RC4        e7b6938   e7b6938  6  
github.com/mattn/go-colorable        *             v0.0.9         167de6b   167de6b  1  
github.com/mattn/go-isatty           *             v0.0.3         0360b2a   0360b2a  1  
github.com/ugorji/go                 *             branch master  00a57e0   f894406  1  
github.com/valyala/bytebufferpool    *             branch master  e746df9   e746df9  1  
github.com/valyala/fasttemplate      *             branch master  dcecefd   dcecefd  1  
golang.org/x/crypto                  *             branch master  687d4b8   6a293f2  2  
golang.org/x/sys                     *             branch master  75813c6   1e2299c  1  
gopkg.in/go-playground/validator.v8  *             v8.18.2        5f1438d   5f1438d  1  
gopkg.in/yaml.v2                     *             branch v2      eb3733d   eb3733d  1

@sharpner
Copy link
Member

Thank you for posting your solution!

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

No branches or pull requests

2 participants