-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdierolsaas.go
41 lines (37 loc) · 928 Bytes
/
dierolsaas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// derolsaas.go
package main
import (
"fmt"
"gorilla/mux"
"mtemplate"
"net/http"
"roller"
)
func init() {
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler)
r.HandleFunc("/index", IndexHandler)
r.HandleFunc("/roll/{roll}", RollHandler)
http.Handle("/", r)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
mtemplate.RenderFile("html/index.html", w, nil)
}
func RollHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
rollRequest := vars["roll"]
if len(rollRequest) == 0 {
w.WriteHeader(400)
fmt.Fprintf(w, "Missing roll specification")
return
}
spec, specErr := roller.Parse(rollRequest)
if specErr != nil {
w.WriteHeader(400)
fmt.Fprintf(w, "Roll specification in incorrect format: %s", specErr)
return
}
results := roller.DoRolls(*spec)
w.Header().Set("Content-Type", "application/json")
w.Write(results.ToJSON())
}