I have simple web application with CRUD operation, I want to serve web pages and API routes using same port address and different Handle pattern. As follows,
fs := http.FileServer(http.Dir("server/webapps/play_maths"))
http.Handle("/", fs)
http.Handle("/api", call API routes)
Following is my API routes
func UserRoutes() *mux.Router {
var router = mux.NewRouter()
router = mux.NewRouter().StrictSlash(true)
router.HandleFunc("/user/create", api.CreateUser)
router.HandleFunc("/user/get/all", api.GetAllUsers)
return router
}
I did following changers to my code and now it's running as I expected.
Then I change my routes as follows.
This is supported by the
net/http
package out-of-the-box. Quoting fromhttp.ServeMux
:So simply you can register your file handler to path
/
, and register an API handler to e.g./api/
path. In this scenario any requests that start with/api/
will be directed to the API handler, and any other requests will be directed to the file handler.Note that this of course means if there are files that are in the
/api/
folder (or more specifically whose request paths start with/api/
), they won't be reachable for the above mentioned reason.