Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang http mux change handler function

Tags:

go

gorilla

I am fairly new to Go and have not been able to find any information on this, maybe it is just not possible at this time.

I am trying to delete or replace a mux route (using http.NewServeMux, or gorilla's mux.Router). My end goal is to be able to enable/disable a route or set of routes without having to restart the program.

I can probably accomplish this on a handler to handler basis and just return 404 if that feature is "disabled", but I would rather find a more general way to do this since I would like to implement it for every route in my application.

Or would I be better off just keeping track of disabled url patterns and using some middleware to prevent handler execution?

If someone can at least point me in the right direction, I will absolutely post code examples of a solution assuming there is one. Thanks!

like image 457
KayoticSully Avatar asked Jun 16 '14 21:06

KayoticSully


People also ask

What is HTTP handler Golang?

The Handler interface is an interface with a single method ServeHTTP which takes a http. Response and a http. Request as inputs. type Handler interface { ServeHTTP(ResponseWriter, *Request)

What is Servemux Go?

Whereas a servemux (also known as a router) stores a mapping between the predefined URL paths for your application and the corresponding handlers. Usually you have one servemux for your application containing all your routes. Go's net/http package ships with the simple but effective http.

What is ResponseWriter Golang?

type ResponseWriter interface { Header() Header Write([]byte) (int, error) WriteHeader(statusCode int) } The Golang net/http Handler interface has serveHTTP method that takes the Response Writer interface as input and this allows the Golang HTTP Server to construct HTTP Response.

Why is gorilla MUX used?

Gorilla Mux provides functionalities for matching routes, serving static files, building single-page applications (SPAs), middleware, handling CORS requests, and testing handlers. This tutorial will walk you through using the Gorilla Mux package as a router for your applications.


1 Answers

There's no built in way, but it is easy enough to implement play.

type HasHandleFunc interface { //this is just so it would work for gorilla and http.ServerMux
    HandleFunc(pattern string, handler func(w http.ResponseWriter, req *http.Request))
}
type Handler struct {
    http.HandlerFunc
    Enabled bool
}
type Handlers map[string]*Handler

func (h Handlers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    path := r.URL.Path
    if handler, ok := h[path]; ok && handler.Enabled {
        handler.ServeHTTP(w, r)
    } else {
        http.Error(w, "Not Found", http.StatusNotFound)
    }
}

func (h Handlers) HandleFunc(mux HasHandleFunc, pattern string, handler http.HandlerFunc) {
    h[pattern] = &Handler{handler, true}
    mux.HandleFunc(pattern, h.ServeHTTP)
}

func main() {
    mux := http.NewServeMux()
    handlers := Handlers{}
    handlers.HandleFunc(mux, "/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("this will show once"))
        handlers["/"].Enabled = false
    })
    http.Handle("/", mux)
    http.ListenAndServe(":9020", nil)
}
like image 100
OneOfOne Avatar answered Nov 09 '22 18:11

OneOfOne