Simple set of middleware and other utility functions to make running a web server in Go just a little bit more convenient.
package main
import "net/http"
import "github.com/fika-io/httputil"
func main() {
http.HandleFunc("/hello", RequestHello)
http.ListenAndServe(":8080", httputil.Logger(http.DefaultServeMux))
}
func RequestHello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!\n"))
}
You will now get log lines whenever a request is made:
2017/05/09 12:18:06 [::1]:61963 "GET /hello HTTP/1.1" 200 39 "" "curl/7.51.0"
2017/05/09 12:18:08 [::1]:61964 "GET /bye HTTP/1.1" 404 19 "" "curl/7.51.0"
func main() {
http.HandleFunc("/hello", RequestHello)
http.ListenAndServe(":8080", httputil.Logger(httputil.Gzipper(http.DefaultServeMux)))
}
Notes:
- The middleware will check
Accept-Encoding
before serving gzip - Make sure to set
Content-Type
first if you‘re callingWriteHeader
func main() {
http.HandleFunc("/hello", RequestHello)
http.Handle("/s/", httputil.Cacher(168*time.Hour, http.StripPrefix("/s/", http.FileServer(http.Dir("static")))))
http.Handle("/favicon.ico", httputil.FileWithCache("static/favicon.ico", 168*time.Hour))
http.Handle("/robots.txt", httputil.FileWithCache("static/robots.txt", 24*time.Hour))
http.ListenAndServe(":8080", httputil.Logger(httputil.Gzipper(http.DefaultServeMux)))
}
Notes:
- This is intended for seamless CDN integration and won‘t do any caching for you
- This adds
Vary: Accept-Encoding
to work with and without gzip
func main() {
http.HandleFunc("/hello", httputil.Handler(RequestHello))
http.ListenAndServe(":8080", httputil.Logger(httputil.Gzipper(http.DefaultServeMux)))
}
func RequestHello(r *http.Request) (interface{}, error) {
name := r.URL.Query().Get("name")
if name == "" {
return nil, httputil.ErrorMessage(400, "no name provided")
}
return map[string]string{"hello": name}, nil
}
Now you get JSON responses:
$ curl http://localhost:8080/hello?name=bob
{"hello":"bob"}