Serving GRPC gateway and docs(swagger) on same endpoint
So idea is pretty simple:
- We'll create a new httpMux
- We'll serve runtimeMux on (/)
- We'll serve index.html (using fileserver) on (/docs/)
- We'll not forget succeeding "/" on /docs/
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