mmg/main.go

59 lines
1.2 KiB
Go
Raw Normal View History

2013-05-08 22:43:18 -07:00
package main
import (
"flag"
"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
2013-05-08 22:43:18 -07:00
var addr = flag.String("addr", ":8000", "address I'll listen on.")
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,
)
2013-05-08 22:43:18 -07:00
if err := http.ListenAndServe(*addr, nil); err != nil {
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
}
}
}