refactor(admintimeout): merge the admin monitor middleware into the admin monitor service

This commit is contained in:
Oscar Zhou
2022-03-30 15:08:06 +13:00
parent 4d698c532a
commit 03ac0c8aed
10 changed files with 67 additions and 223 deletions

View File

@@ -10,14 +10,16 @@ import (
// Handler represents an HTTP API handler for managing static files.
type Handler struct {
http.Handler
wasInstanceDisabled func() bool
}
// NewHandler creates a handler to serve static files.
func NewHandler(assetPublicPath string) *Handler {
func NewHandler(assetPublicPath string, wasInstanceDisabled func() bool) *Handler {
h := &Handler{
Handler: handlers.CompressHandler(
http.FileServer(http.Dir(assetPublicPath)),
),
wasInstanceDisabled: wasInstanceDisabled,
}
return h
@@ -33,6 +35,11 @@ func isHTML(acceptContent []string) bool {
}
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if handler.wasInstanceDisabled() && (r.RequestURI == "/" || r.RequestURI == "/index.html") {
http.Redirect(w, r, "/timeout.html", http.StatusTemporaryRedirect)
return
}
if !isHTML(r.Header["Accept"]) {
w.Header().Set("Cache-Control", "max-age=31536000")
} else {