Caching proxy as middleware for Go.
First, this was part of the job test assessment. But I think it shouldn't go to the trash bin and might be useful for someone. So, generally speaking - Just for fun 😊
go get -u github.com/vokinneberg/http-cache
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"hello\": \"world\"}"))
})
handler := httpcache.NewDefault().Handler(mux)
http.ListenAndServe(":8080", handler)
}
func main() {
mux := mux.NewRouter()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"hello\": \"world\"}"))
})
handler := httpcache.NewDefault().Handler(mux)
http.ListenAndServe(":8080", handler)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"hello\": \"world\"}"))
})
n := negroni.Classic()
n.Use(httpcache.NewDefault())
n.UseHandler(mux)
n.Run(":8080")
}
- Add Unit tests.
- Add benchmarks - I really interested in how efficient this implementation is?
- Add Debug option.
- Make middleware RFC7234 complaint.
- Add support for Cache-Control header.
- Add more data store adapters such as: Redis, memcached, DynamoDB, etc.