2017-03-12 19:37:53 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/pressly/chi"
|
|
|
|
"go.rls.moe/nyx/http/errw"
|
|
|
|
"go.rls.moe/nyx/http/middle"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-15 08:13:15 +00:00
|
|
|
panelTmpl = template.New("admin/panel")
|
|
|
|
loginTmpl = template.New("admin/login")
|
|
|
|
statusTmpl = template.New("admin/status")
|
2017-03-12 19:37:53 +00:00
|
|
|
)
|
|
|
|
|
2017-03-15 13:33:27 +00:00
|
|
|
func LoadTemplates() error {
|
2017-03-12 19:37:53 +00:00
|
|
|
var err error
|
2017-03-15 13:39:57 +00:00
|
|
|
box, err := rice.FindBox("admin_res/")
|
2017-03-15 13:33:27 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-12 19:37:53 +00:00
|
|
|
panelTmpl, err = panelTmpl.Parse(box.MustString("panel.html"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
loginTmpl, err = loginTmpl.Parse(box.MustString("index.html"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-15 08:13:15 +00:00
|
|
|
statusTmpl, err = statusTmpl.Parse(box.MustString("status.html"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-15 13:37:37 +00:00
|
|
|
return nil
|
2017-03-12 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Router sets up the Administration Panel
|
|
|
|
// It **must** be setup on the /admin/ basepath
|
2017-03-13 15:04:00 +00:00
|
|
|
func AdminRouter(r chi.Router) {
|
2017-03-12 19:37:53 +00:00
|
|
|
r.Get("/", serveLogin)
|
|
|
|
r.Get("/index.html", serveLogin)
|
|
|
|
r.Get("/panel.html", servePanel)
|
|
|
|
r.Post("/new_board.sh", handleNewBoard)
|
2017-03-15 08:13:15 +00:00
|
|
|
r.Post("/cleanup.sh", handleCleanup)
|
2017-03-12 19:37:53 +00:00
|
|
|
r.Post("/login.sh", handleLogin)
|
|
|
|
r.Post("/logout.sh", handleLogout)
|
2017-03-15 08:13:15 +00:00
|
|
|
r.Post("/new_admin.sh", handleNewAdmin)
|
|
|
|
r.Post("/del_admin.sh", handleDelAdmin)
|
|
|
|
r.Get("/status.sh", serveStatus)
|
2017-03-15 13:14:35 +00:00
|
|
|
r.Post("/set_rules.sh", handleSetRules)
|
2017-03-12 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 15:04:00 +00:00
|
|
|
// Router sets up moderation functions
|
|
|
|
// It **must** be setup on the /mod/ basepath
|
|
|
|
func ModRouter(r chi.Router) {
|
|
|
|
r.Post("/del_reply.sh", handleDelPost)
|
|
|
|
}
|
|
|
|
|
2017-03-12 19:37:53 +00:00
|
|
|
func serveLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dat := bytes.NewBuffer([]byte{})
|
|
|
|
err := loginTmpl.Execute(dat, middle.GetBaseCtx(r))
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "index.html", time.Now(),
|
|
|
|
bytes.NewReader(dat.Bytes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func servePanel(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := middle.GetSession(r)
|
|
|
|
if sess == nil {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte("Unauthorized"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dat := bytes.NewBuffer([]byte{})
|
|
|
|
err := panelTmpl.Execute(dat, middle.GetBaseCtx(r))
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "panel.html", time.Now(),
|
|
|
|
bytes.NewReader(dat.Bytes()))
|
|
|
|
}
|