Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gorilla mux with http.TimeoutHandler

In an HTTP server written in go, I use gorilla/mux for routing,
I want to use http.TimeoutHandler (and/or other "middleware") but I can't understand where I can fit them.

To make it clear:

  1. I create a new Router by gorillaMux := mux.NewRouter()
  2. add my routes by calls like gorillaMux.HandleFunc("/", rootHandler)
  3. I create the server by server := &http.Server{Addr:":1234"} and server.ListenAndServe()

Where can I insert the http.TimeoutHandler or any other middleware for that matter?

like image 401
Ali Avatar asked Dec 16 '22 04:12

Ali


2 Answers

Here is how you can do this:

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "net/http"
    "time"
)

func rootHandler(w http.ResponseWriter, r *http.Request) {
    time.Sleep(5 * time.Second)
    fmt.Fprintf(w, "Hello!")
}

func main() {
    mux := mux.NewRouter()
    mux.HandleFunc("/", rootHandler)

    muxWithMiddlewares := http.TimeoutHandler(mux, time.Second*3, "Timeout!")

    http.ListenAndServe(":8080", muxWithMiddlewares)
}

If you have more than one HTTP handler, you can stack them up:

// this is quite synthetic and ugly example, but it illustrates how Handlers works
muxWithMiddlewares := http.StripPrefix("/api", http.TimeoutHandler(mux, time.Second*3, "Timeout!"))
like image 103
Kavu Avatar answered Feb 15 '23 03:02

Kavu


This solution does not answer the use TimeoutHandler. I posted this here in case anybody wants fine-grain control of their server timeout. This alternative will allow one to define the IdleTimeout and RequestHeaderTimeout if necessary. I am using both gorilla/mux and gorilla/handlers in this example. Hope it helps.

// ReadTimeout is a timing constraint on the client http request imposed by the server from the moment
// of initial connection up to the time the entire request body has been read.
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]

// WriteTimeout is a time limit imposed on client connecting to the server via http from the
// time the server has completed reading the request header up to the time it has finished writing the response.
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]

func main() {
    mux := router.EpicMux()

    srv := &http.Server{
        Handler:      handlers.LoggingHandler(os.Stdout, mux),
        Addr:         "localhost:8080",
        WriteTimeout: 15 * time.Second,
        ReadTimeout:  15 * time.Second,
    }

    log.Fatal(srv.ListenAndServe())
}

func EpicMux() http.Handler {
    r := mux.NewRouter()
    r.HandleFunc("/", BaseURLRouter).Methods(http.MethodGet)
    // create the subroutes for v1 and v2
    v1 := r.PathPrefix("api/v1").Subrouter()
    // register handlers to appropriate version
    v1.HandleFunc("/person", PersonHandlerV1).Methods(http.MethodPost)

    v2 := r.PathPrefix("api/v2").Subrouter()
    v2.HandleFunc("/person",    PersonHandlerV2).Methods(http.MethodPost)
    return r
}
like image 35
Ramil Avatar answered Feb 15 '23 04:02

Ramil