tech5usa / TLSential

A server for providing short-lived TLS certificates to all services within a firewall restricted network.
GNU General Public License v3.0
15 stars 2 forks source link

Take care of trailing slash issue. #39

Closed debus closed 4 years ago

debus commented 4 years ago

Right now gorilla mux treats /some/url differently than /some/url/. And will route requests for /some/url/ to a handler set up to handle /some/url/{some_resource_id}.

The way the /api/certificate endpoint gets around this is by specifying the route as r.HandleFunc("/api/{certificate:certificate(?:\\/)?}",.

This is going to get out of hand very quickly if we have to do this for every endpoint.

We should think about doing something like what is suggested here

https://husainalshehhi.com/blog/gorilla-mux-trailing-slashes/

func removeTrailingSlash(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
        next.ServeHTTP(w, r)
    })
}

log.Fatal(http.ListenAndServe(":8888", removeTrailingSlash(router)))

Although we may have an edge case for the url "/". We may not want to remove that slash so that the "/" routes will actually work.

d1str0 commented 4 years ago

Single "/" might be special and automatically route. Will have to test.