Is there a way to serve static files over HTTP in Go with a custom status code (without re-writing a significant amount of private code)?
From what I can see:
http.StatusOK header here.I think I already know the answer for this, but if someone has an alternative solution it'd be useful.
The use case is serving 500.html, 404.html, et. al files. I'd normally use nginx to catch Go's usual plain http.Error responses and have nginx serve the file off disk, but I'm in an environment where that's not an option.
Wrap http.ResponseWriter:
type MyResponseWriter struct {
http.ResponseWriter
code int
}
func (m MyResponseWriter) WriteHeader(int) {
m.ResponseWriter.WriteHeader(m.code)
}
And then (for an HTTP 500):
http.ServeFile(MyResponseWriter{rw, 500}, rq, "file.name")
Where rw is the "actual" http.ResponseWriter and rq is the *http.Request object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With