website/pkg/middleware/maintenance.go

72 lines
2.1 KiB
Go

package middleware
import (
"net/http"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/templates"
)
var tmplMaint = templates.Must("errors/maintenance.html")
// MaintenanceMode check at the middleware level, e.g. to block
// LoginRequired and CertificationRequired if site-wide interaction
// is currently on hold. Returns true if handled.
func MaintenanceMode(currentUser *models.User, w http.ResponseWriter, r *http.Request) bool {
// Is the site under a Maintenance Mode restriction?
if config.Current.Maintenance.PauseInteraction && !currentUser.IsAdmin {
var vars = map[string]interface{}{
"Reason": "interaction",
}
if err := tmplMaint.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return true
}
return false
}
// SignupMaintenance may handle maintenance mode requests for signup gating.
func SignupMaintenance(w http.ResponseWriter, r *http.Request) bool {
if config.Current.Maintenance.PauseSignup {
var vars = map[string]interface{}{
"Reason": "signup",
}
if err := tmplMaint.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return true
}
return false
}
// LoginMaintenance may handle maintenance mode requests for login gating.
func LoginMaintenance(currentUser *models.User, w http.ResponseWriter, r *http.Request) bool {
if config.Current.Maintenance.PauseLogin && !currentUser.IsAdmin {
var vars = map[string]interface{}{
"Reason": "login",
}
if err := tmplMaint.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return true
}
return false
}
// ChatMaintenance may handle maintenance mode requests for chat room gating.
func ChatMaintenance(currentUser *models.User, w http.ResponseWriter, r *http.Request) bool {
if config.Current.Maintenance.PauseChat && !currentUser.IsAdmin {
var vars = map[string]interface{}{
"Reason": "chat",
}
if err := tmplMaint.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return true
}
return false
}