website/pkg/controller/admin/maintenance.go

74 lines
2.0 KiB
Go
Raw Permalink Normal View History

package admin
import (
"net/http"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/templates"
)
// Maintenance controller (/admin/maintenance)
func Maintenance() http.HandlerFunc {
tmpl := templates.Must("admin/maintenance.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Query parameters.
var (
intent = r.FormValue("intent")
)
currentUser, err := session.CurrentUser(r)
if err != nil {
session.FlashError(w, r, "Couldn't get your current user: %s", err)
}
_ = currentUser
// POST event handlers.
if r.Method == http.MethodPost {
// Collect the form parameters.
var (
pauseSignup = r.PostFormValue("signup") == "true"
pauseLogin = r.PostFormValue("login") == "true"
pauseChat = r.PostFormValue("chat") == "true"
pauseInteraction = r.PostFormValue("interaction") == "true"
)
switch intent {
case "everything", "nothing":
pauseSignup = intent == "everything"
pauseLogin = pauseSignup
pauseChat = pauseSignup
pauseInteraction = pauseSignup
intent = "save"
fallthrough
case "save":
// Update and save the site settings.
config.Current.Maintenance.PauseSignup = pauseSignup
config.Current.Maintenance.PauseLogin = pauseLogin
config.Current.Maintenance.PauseChat = pauseChat
config.Current.Maintenance.PauseInteraction = pauseInteraction
if err := config.WriteSettings(); err != nil {
session.FlashError(w, r, "Couldn't write settings.json: %s", err)
} else {
session.Flash(w, r, "Maintenance settings updated!")
}
default:
session.FlashError(w, r, "Unsupported intent: %s", intent)
}
templates.Redirect(w, r.URL.Path)
return
}
var vars = map[string]interface{}{
"Intent": intent,
"Maint": config.Current.Maintenance,
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}