Skip to content

Instantly share code, notes, and snippets.

@mayankcpdixit
Last active November 30, 2018 10:55
Show Gist options
  • Save mayankcpdixit/d543ac8d47e24eda959c96c7eefa2a40 to your computer and use it in GitHub Desktop.
Save mayankcpdixit/d543ac8d47e24eda959c96c7eefa2a40 to your computer and use it in GitHub Desktop.
Serving GRPC gateway and docs on same endpoint

Serving GRPC gateway and docs(swagger) on same endpoint

So idea is pretty simple:

  1. We'll create a new httpMux
  2. We'll serve runtimeMux on (/)
  3. We'll serve index.html (using fileserver) on (/docs/)
  4. We'll not forget succeeding "/" on /docs/

In action:

Modify the example pt. 6 in this way:

runtimeMux := runtime.NewServeMux()
...
...  // things to do with runtime mux
...
httpMux := http.NewServeMux()
httpMux.Handle("/", runtimeMux)
prefix := "/docs/"
fileServer := http.FileServer(http.Dir(string("my_docs_folder")))
httpMux.Handle(prefix, http.StripPrefix(prefix, fileServer))
return http.ListenAndServe(":8080", httpMux)

PS: While defining prefix := "/docs/". If you forget putting the "/" in the end. You're gonna waste quite a lot of time wondering what happened!

That's it. Cheers!

PS: blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment