Last active
May 22, 2021 09:24
-
-
Save Yapcheekian/fa542db3a0f55b48f465c0f6348fdd43 to your computer and use it in GitHub Desktop.
go http middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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