moth/cmd/mothd/httpd.go

142 lines
3.4 KiB
Go
Raw Normal View History

2020-02-29 16:53:08 -07:00
package main
import (
"log"
2020-02-29 22:37:22 -07:00
"net/http"
2020-02-29 16:53:08 -07:00
"strings"
2020-03-01 14:03:46 -07:00
"strconv"
2020-02-29 16:53:08 -07:00
)
type HTTPServer struct {
*http.ServeMux
2020-03-01 16:10:55 -07:00
server *MothServer
base string
2020-02-29 16:53:08 -07:00
}
2020-03-01 16:10:55 -07:00
func NewHTTPServer(base string, server *MothServer) *HTTPServer {
2020-03-01 14:03:46 -07:00
base = strings.TrimRight(base, "/")
2020-02-29 16:53:08 -07:00
h := &HTTPServer{
ServeMux: http.NewServeMux(),
2020-03-01 16:10:55 -07:00
server: server,
base: base,
2020-02-29 16:53:08 -07:00
}
2020-03-01 16:10:55 -07:00
h.HandleMothFunc("/", h.ThemeHandler)
h.HandleMothFunc("/state", h.StateHandler)
h.HandleMothFunc("/register", h.RegisterHandler)
h.HandleMothFunc("/answer", h.AnswerHandler)
h.HandleMothFunc("/content/", h.ContentHandler)
2020-02-29 16:53:08 -07:00
return h
}
2020-03-01 16:10:55 -07:00
func (h *HTTPServer) HandleMothFunc(
pattern string,
mothHandler func(MothRequestHandler, http.ResponseWriter, *http.Request),
) {
handler := func(w http.ResponseWriter, req *http.Request) {
mh := h.server.NewHandler(req.FormValue("id"))
mothHandler(mh, w, req)
}
h.HandleFunc(h.base + pattern, handler)
2020-02-29 16:53:08 -07:00
}
// This gives Instances the signature of http.Handler
func (h *HTTPServer) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
w := MothResponseWriter{
2020-02-29 22:37:22 -07:00
statusCode: new(int),
2020-02-29 16:53:08 -07:00
ResponseWriter: wOrig,
}
h.ServeMux.ServeHTTP(w, r)
log.Printf(
"%s %s %s %d\n",
r.RemoteAddr,
r.Method,
r.URL,
*w.statusCode,
)
}
2020-03-01 16:10:55 -07:00
type MothResponseWriter struct {
statusCode *int
http.ResponseWriter
}
func (w MothResponseWriter) WriteHeader(statusCode int) {
*w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (h *HTTPServer) Run(bindStr string) {
log.Printf("Listening on %s", bindStr)
log.Fatal(http.ListenAndServe(bindStr, h))
}
func (h *HTTPServer) ThemeHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
2020-02-29 16:53:08 -07:00
path := req.URL.Path
if path == "/" {
path = "/index.html"
}
2020-03-01 16:10:55 -07:00
f, mtime, err := mh.ThemeOpen(path)
2020-02-29 16:53:08 -07:00
if err != nil {
http.NotFound(w, req)
return
}
defer f.Close()
http.ServeContent(w, req, path, mtime, f)
}
2020-03-01 16:10:55 -07:00
func (h *HTTPServer) StateHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
JSONWrite(w, mh.ExportState())
2020-02-29 16:53:08 -07:00
}
2020-03-01 16:10:55 -07:00
func (h *HTTPServer) RegisterHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
2020-02-29 22:37:22 -07:00
teamName := req.FormValue("name")
2020-03-01 16:10:55 -07:00
if err := mh.Register(teamName); err != nil {
2020-02-29 22:37:22 -07:00
JSendf(w, JSendFail, "not registered", err.Error())
} else {
JSendf(w, JSendSuccess, "registered", "Team ID registered")
}
2020-02-29 16:53:08 -07:00
}
2020-03-01 16:10:55 -07:00
func (h *HTTPServer) AnswerHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
cat := req.FormValue("cat")
pointstr := req.FormValue("points")
answer := req.FormValue("answer")
points, _ := strconv.Atoi(pointstr)
if err := mh.CheckAnswer(cat, points, answer); err != nil {
JSendf(w, JSendFail, "not accepted", err.Error())
} else {
JSendf(w, JSendSuccess, "accepted", "%d points awarded in %s", points, cat)
}
2020-02-29 16:53:08 -07:00
}
2020-03-01 16:10:55 -07:00
func (h *HTTPServer) ContentHandler(mh MothRequestHandler, w http.ResponseWriter, req *http.Request) {
trimLen := len(h.base) + len("/content/")
2020-03-01 14:03:46 -07:00
parts := strings.SplitN(req.URL.Path[trimLen:], "/", 3)
if len(parts) < 3 {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
cat := parts[0]
pointsStr := parts[1]
filename := parts[2]
if (filename == "") {
filename = "puzzles.json"
}
points, _ := strconv.Atoi(pointsStr)
2020-03-01 16:10:55 -07:00
mf, mtime, err := mh.PuzzlesOpen(cat, points, filename)
2020-03-01 14:03:46 -07:00
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
defer mf.Close()
2020-03-01 16:10:55 -07:00
http.ServeContent(w, req, filename, mtime, mf)
2020-02-29 16:53:08 -07:00
}