I am trying to learn Go and as way of throwing myself in the deep-end I thought it would be cool to try building a web framework instead of using an already existing solution. I'm coming from the JavaScript world so all the type and interface stuff is new to me as well. I was wondering if this is an acceptable approach to doing something.

Essentially, I am trying to take a request that someone might send to a server and check some information about it to determine whether or not it should be allowed to move on. Specifically, in this example I am looking to see if the request method from a client is the same as the one the user of this framework intended an endpoint to be used with.

To do so, I am using middleware to access the req data, however I am unsure if this is ok... Here is my code, any feedback or suggestions would be much appreciated.

Running of the server:

var mux = http.NewServeMux()

func (r *Router) Run(c RunConfig) {
    log.Fatal(http.ListenAndServe(c.Port, mux))

Small portion of my router code:

func checkMethod(next http.Handler, method string) http.Handler {
    return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
        fmt.Printf("checkMethod\n \tMehtod: %v\n \tpath: %v\n", r.Method, r.URL.Path)
        if method == r.Method {
            next.ServeHTTP(rw, r)
        } else {
            fmt.Println("Wrong method")

// GET is a receiver function of Router and is used to create a route with a GET request.
// path {string}
// handler {http.HandlerFunc}
func (r *Router) GET(path string, handler http.HandlerFunc) {
    r.tree[path] = createNode(path, "GET", handler)
    mux.Handle(path, checkMethod(handler, "GET"))

It seems to work but is this an acceptable solution? Is there any reason not to do this? Would it scale?

Please let me know if you have questions or need to see more code (although this is more-or-less it right now).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.