I am using Google App Engine to serve my (semi-)static website generated with Hugo. I have a directory "public" where all the HTML files are stored and are to be served. I also have some server-side scripts for the contact form handling for example. The app.yaml
file looks like this.
// app.yaml
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
secure: always
And the simplified main.go
file looks like this
// main.go
package main
import (
"net/http"
"encoding/json"
"appengine"
"appengine/urlfetch"
)
func init() {
fileHandler := http.FileServer(http.Dir("public"))
http.Handle("/", fileHandler)
http.HandleFunc("/contactus/", HandleContactus)
}
This works perfectly well and serves the html files. However, I am looking at a solution to handle the cases where the pages are not found and the response is 404 Not Found
for example (or any other server error).
My thought was to create a custom handler which can be passed in the http.Handle("/", myCustomHandler)
and would handle the server response and would redirect to a custom 404.html
or the like if necessary. I am new to Go and can't seem to figure out how this should be implemented. I have also looked at the Gorilla Mux, but would prefer (if possible) not to use external libraries to keep it simple.
Based on this post, I have tried the following
package main
import (
"net/http"
"encoding/json"
"appengine"
"appengine/urlfetch"
)
func StaticSiteHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
h.ServeHTTP(w, r)
})
}
func init() {
fileHandler := http.FileServer(http.Dir("public"))
http.Handle("/", StaticSiteHandler(fileHandler))
http.HandleFunc("/contactus/", HandleContactus)
}
This solution works in the sense that it also does serve my HTML pages, however I still can't figure out how to handle the server response codes.
Any help would be highly appreciated. Thanks!