Multiple registrations for "/"

300 views
Skip to first unread message

mhali...@gmail.com

unread,
Dec 14, 2016, 2:16:35 AM12/14/16
to google-appengine-go
Hi,
I have a Go workspace with multiple GAE services inside.

src/
    pkgA/
        main.go
        app.yaml
    pkgB/
        main.go
        app.yaml

Both pkgA and pkgB are services, which means that they both some handlers registered in init() (I use gorilla/mux, if it matters). Also, since I have some of the model structs defined in pkgA, pkgB imports pkgA.

When I try to start pkgB, I get "panic: http: multiple registrations for /". I suppose this is because of importing the pkgA. Is there a way to overcome this? Or should I move common classes to separate packages?

Thanks,
Mateusz

Evan Jones

unread,
Dec 14, 2016, 10:35:14 AM12/14/16
to google-appengine-go, mhali...@gmail.com
I'm not sure this is an App Engine specific issue. I think this would happen with any Go web application. My suggestion: move the reusable code into its own package, that is imported by both. This might mean that pkgA ends up just being an init() function (or a main() function, whatever is equivalent). Good luck,

Evan

mhali...@gmail.com

unread,
Dec 15, 2016, 12:36:44 AM12/15/16
to google-appengine-go, mhali...@gmail.com
Actually, I tried moving that to a separate package inside within the same repository (pkgA/model), but it fails as AppEngine imports all the app files and then the dependencies, and the duplicates are not allowed. I don't want to move it to very separate directory, as I believe making the packages too granular will actually harm the readability of the packages (I'm a Java guy that recently started Go).

What I found out that works better is:

src/
  pkgA/
    appengine/
      app.yaml
      appengine.go
    main.go
  pkgB/
    appengine/
      app.yaml
      appengine.go
    main.go

Now, the main.go in both packages don't include init() function, so it's not called when importing. And it keeps the GAE logic nicely separated from the actual logic of my package:)

Evan Jones

unread,
Dec 15, 2016, 9:40:55 AM12/15/16
to google-appengine-go, mhali...@gmail.com
This seems like a very sensible solution to me! Nice, thanks for following up.
Reply all
Reply to author
Forward
0 new messages