How to serve static file to dynamic path in Go?

Viewed 187

My file structure is so main.go is inside folder co and inside folder cmd.

-assets/
-cmd/co/main.go

Here is my code in main.go,

func main() {
    router := NewRouter()
    router.HandleFunc("/search", api.SearchHandler)
    router.HandleFunc("/product-details/{author}/{name}/{id}//", api.DetailsHandler)
    // ... listenAndServe
}

func NewRouter() *mux.Router {
    router := mux.NewRouter()
    staticDir := "/assets/"
    router.
        PathPrefix(staticDir).
        Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir("."+staticDir))))
    return router
}

Handle /search which is static url works perfectly but when I go to product-details/{author} ... (dynamic url path) with subroutes an error 404 is sent to me by web browser. So go is not serving folder in dynamic path in this manner

0 Answers
Related