You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
968 B
Go

package handler
import (
"net/http"
"strings"
"github.com/denisovdennis/autohero/internal/game"
)
// APITimePausedMiddleware blocks mutating /api/v1 requests while global simulation time is frozen.
// GET/HEAD/OPTIONS still work so clients can read state.
func APITimePausedMiddleware(engine *game.Engine) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
next.ServeHTTP(w, r)
return
}
// Client preference only; not part of combat/world simulation.
if strings.HasSuffix(r.URL.Path, "/hero/changelog/ack") {
next.ServeHTTP(w, r)
return
}
if engine != nil && engine.IsTimePaused() {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
"error": "server time is paused",
})
return
}
next.ServeHTTP(w, r)
})
}
}