use go generate

brought in jteeuwen/go-bindata and friends.

I honestly like their interface much, much better than the previous one.
This commit is contained in:
Stephen McQuay 2015-01-12 23:29:14 -08:00
parent 5c7addafb9
commit c6419060ff
3 changed files with 332 additions and 10992 deletions

View File

@ -1,3 +0,0 @@
static.go: static/*
go get -v github.com/chsc/bin2go
bin2go -p main -s static.go -c -a static/*

38
main.go
View File

@ -9,33 +9,33 @@ import (
"os" "os"
"time" "time"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
) )
//go:generate go-bindata -o static.go static/
const MAX = 12 const MAX = 12
var port = flag.Int("port", 8000, "listen on this port") var port = flag.Int("port", 8000, "listen on this port")
var store = sessions.NewCookieStore([]byte(os.Getenv("MMG_SECRET_KEY"))) var store = sessions.NewCookieStore([]byte(os.Getenv("MMG_SECRET_KEY")))
var statics map[string][]byte
func main() { 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()) rand.Seed(time.Now().UTC().UnixNano())
flag.Parse() flag.Parse()
http.HandleFunc("/api/v0/addsub/problem/", addsub) http.HandleFunc("/api/v0/addsub/problem/", addsub)
http.HandleFunc("/api/v0/mul/problem/", mul) http.HandleFunc("/api/v0/mul/problem/", mul)
http.HandleFunc("/api/v0/attempt/", attempt) http.HandleFunc("/api/v0/attempt/", attempt)
http.HandleFunc( http.Handle(
"/", "/",
static, http.FileServer(
&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
Prefix: "static",
},
),
) )
http.HandleFunc( http.HandleFunc(
"/reset/", "/reset/",
@ -52,19 +52,3 @@ func main() {
log.Fatal("ListenAndServe:", err) 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
}
}
}

11283
static.go

File diff suppressed because one or more lines are too long