added static asset go files
This commit is contained in:
@@ -14,19 +14,42 @@ import (
|
||||
const MAX = 12
|
||||
|
||||
var addr = flag.String("addr", ":8000", "address I'll listen on.")
|
||||
var static_files = flag.String("static", "./static", "location of static files")
|
||||
|
||||
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.Handle("/",
|
||||
http.FileServer(http.Dir(*static_files)))
|
||||
http.HandleFunc("/api/v0/addsub/problem/", addsub)
|
||||
http.HandleFunc("/api/v0/mul/problem/", mul)
|
||||
http.HandleFunc("/api/v0/attempt/", attempt)
|
||||
http.HandleFunc(
|
||||
"/",
|
||||
static,
|
||||
)
|
||||
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 {
|
||||
_, err := w.Write(content)
|
||||
if err != nil {
|
||||
http.Error(w, "problem writing response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user