Skip to content

Instantly share code, notes, and snippets.

@Yapcheekian
Last active May 22, 2021 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yapcheekian/fa542db3a0f55b48f465c0f6348fdd43 to your computer and use it in GitHub Desktop.
Save Yapcheekian/fa542db3a0f55b48f465c0f6348fdd43 to your computer and use it in GitHub Desktop.
go http middleware
func errorHandler(fn http.HandleFunc) http.HandleFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e, of := recover().(os.Error); ok {
w.WriteHeader(500)
errorTemplate.Execute(w, e)
}
}()
fn(w, r)
}
}
func main() {
http.HandleFunc("/", errorHandler(upload))
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment