63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
const MAX = 12
|
|
|
|
var addr = flag.String("addr", ":8000", "address I'll listen on.")
|
|
|
|
var store = sessions.NewCookieStore([]byte(os.Getenv("MMG_SECRET_KEY")))
|
|
var statics map[string][]byte
|
|
|
|
func main() {
|
|
statics = map[string][]byte{
|
|
"/": staticIndexHtml,
|
|
"/jquery.js": staticJqueryJs,
|
|
"/math.css": staticMathCss,
|
|
"/math.js": staticMathJs,
|
|
"/addsub/": staticAddsubHtml,
|
|
"/mul/": staticMulHtml,
|
|
}
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
flag.Parse()
|
|
http.HandleFunc("/api/v0/addsub/problem/", addsub)
|
|
http.HandleFunc("/api/v0/mul/problem/", mul)
|
|
http.HandleFunc("/api/v0/attempt/", attempt)
|
|
http.HandleFunc(
|
|
"/",
|
|
static,
|
|
)
|
|
http.HandleFunc(
|
|
"/reset/",
|
|
reset,
|
|
)
|
|
if err := http.ListenAndServe(*addr, nil); err != nil {
|
|
log.Fatal("ListenAndServe:", err)
|
|
}
|
|
}
|
|
|
|
func static(w http.ResponseWriter, req *http.Request) {
|
|
if content, ok := statics[req.URL.Path]; !ok {
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
|
return
|
|
} else {
|
|
if req.URL.Path == "/math.css" {
|
|
w.Header().Set("Content-Type", "text/css")
|
|
}
|
|
_, err := w.Write(content)
|
|
if err != nil {
|
|
http.Error(w, "problem writing response", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|