ictgj-voting/admin_endpoints.go
Brian Buller ba35073d95 2017-06-22 Build
* Use 'https://github.com/mjibson/esc' for embedded assets
* Pull database name out of siteData object
* Load site config from database, if available
* Allow customizing site config from command line arguments
* Clean up some templates
* Update Readme (it still needs a lot of updating)
* Started work on vote accumulation/management
2017-06-22 10:34:57 -05:00

65 lines
1.6 KiB
Go

package main
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func initAdminRequest(w http.ResponseWriter, req *http.Request) *pageData {
p := InitPageData(w, req)
p.Stylesheets = append(p.Stylesheets, "/assets/css/admin.css")
p.Scripts = append(p.Scripts, "/assets/js/admin.js")
p.HideAdminMenu = false
return p
}
// Main admin handler, routes the request based on the category
func handleAdmin(w http.ResponseWriter, req *http.Request) {
page := initAdminRequest(w, req)
vars := mux.Vars(req)
if !page.LoggedIn {
if vars["category"] == "clients" &&
vars["id"] != "" &&
(vars["function"] == "add" || vars["function"] == "auth") {
// When authenticating a client, we have an all-in-one login/auth page
handleAdminClients(w, req, page)
} else {
page.SubTitle = "Admin Login"
page.show("admin-login.html", w)
}
} else {
adminCategory := vars["category"]
switch adminCategory {
case "users":
handleAdminUsers(w, req, page)
case "teams":
handleAdminTeams(w, req, page)
case "games":
handleAdminGames(w, req, page)
case "clients":
handleAdminClients(w, req, page)
case "votes":
handleAdminVotes(w, req, page)
case "mode":
handleAdminSetMode(w, req, page)
default:
page.show("admin-main.html", w)
}
}
}
func handleAdminSetMode(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
newMode, err := strconv.Atoi(vars["id"])
if err != nil {
page.session.setFlashMessage("Invalid Mode: "+vars["id"], "error")
}
if dbSetPublicSiteMode(newMode) != nil {
page.session.setFlashMessage("Invalid Mode: "+vars["id"], "error")
}
redirect("/admin", w, req)
}