2013-05-08 22:43:18 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2015-01-12 20:01:38 -08:00
|
|
|
"fmt"
|
2013-05-08 22:43:18 -07:00
|
|
|
"log"
|
2013-05-09 00:48:56 -07:00
|
|
|
"math/rand"
|
2013-05-08 22:43:18 -07:00
|
|
|
"net/http"
|
2013-05-09 00:53:17 -07:00
|
|
|
"os"
|
2013-05-09 00:48:56 -07:00
|
|
|
"time"
|
2014-12-07 21:23:31 -08:00
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
2013-05-08 22:43:18 -07:00
|
|
|
)
|
|
|
|
|
2013-05-08 23:21:18 -07:00
|
|
|
const MAX = 12
|
|
|
|
|
2015-01-12 20:01:38 -08:00
|
|
|
var port = flag.Int("port", 8000, "listen on this port")
|
2013-05-08 22:43:18 -07:00
|
|
|
|
2013-05-09 00:53:17 -07:00
|
|
|
var store = sessions.NewCookieStore([]byte(os.Getenv("MMG_SECRET_KEY")))
|
2014-12-07 21:44:05 -08:00
|
|
|
var statics map[string][]byte
|
2013-05-08 22:43:18 -07:00
|
|
|
|
|
|
|
func main() {
|
2014-12-07 21:44:05 -08:00
|
|
|
statics = map[string][]byte{
|
|
|
|
"/": staticIndexHtml,
|
|
|
|
"/jquery.js": staticJqueryJs,
|
|
|
|
"/math.css": staticMathCss,
|
|
|
|
"/math.js": staticMathJs,
|
|
|
|
"/addsub/": staticAddsubHtml,
|
|
|
|
"/mul/": staticMulHtml,
|
|
|
|
}
|
2013-05-09 00:48:56 -07:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2013-05-08 22:43:18 -07:00
|
|
|
flag.Parse()
|
2014-12-07 21:23:31 -08:00
|
|
|
http.HandleFunc("/api/v0/addsub/problem/", addsub)
|
|
|
|
http.HandleFunc("/api/v0/mul/problem/", mul)
|
|
|
|
http.HandleFunc("/api/v0/attempt/", attempt)
|
2014-12-07 21:44:05 -08:00
|
|
|
http.HandleFunc(
|
|
|
|
"/",
|
|
|
|
static,
|
|
|
|
)
|
2014-12-07 21:57:41 -08:00
|
|
|
http.HandleFunc(
|
|
|
|
"/reset/",
|
|
|
|
reset,
|
|
|
|
)
|
2015-01-12 20:01:38 -08:00
|
|
|
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("problem getting hostname:", err)
|
|
|
|
}
|
|
|
|
log.Printf("serving at: http://%s:%d/", hostname, *port)
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
|
2013-05-08 22:43:18 -07:00
|
|
|
log.Fatal("ListenAndServe:", err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-07 21:44:05 -08:00
|
|
|
|
|
|
|
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 {
|
2014-12-07 21:48:19 -08:00
|
|
|
if req.URL.Path == "/math.css" {
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
}
|
2014-12-07 21:44:05 -08:00
|
|
|
_, err := w.Write(content)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "problem writing response", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|