49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
"countrygame/web"
|
|
)
|
|
|
|
func main() {
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
// Used by the compose healthcheck: the distroless image has no curl.
|
|
if len(os.Args) > 1 && os.Args[1] == "-healthcheck" {
|
|
resp, err := http.Get("http://127.0.0.1:" + port + "/healthz")
|
|
if err != nil || resp.StatusCode != http.StatusOK {
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
root, err := fs.Sub(web.Files, ".")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
})
|
|
mux.Handle("/", noCache(http.FileServer(http.FS(root))))
|
|
|
|
log.Printf("country game listening on :%s", port)
|
|
log.Fatal(http.ListenAndServe(":"+port, mux))
|
|
}
|
|
|
|
// noCache keeps iteration simple: data and scripts are always revalidated.
|
|
func noCache(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|