/
main.go
68 lines (51 loc) · 1.21 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"log"
"net/http"
"os"
"runtime/pprof"
"strings"
"github.com/gorilla/mux"
"github.com/tscott0/countdown/letters"
"github.com/tscott0/countdown/numbers"
)
const (
dictionaryFile string = "words-en-gb"
)
func main() {
f, err := os.Create("test.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
if err := letters.ReadDict(dictionaryFile); err != nil {
log.Fatalf("couldn't read dictionary file %q: %v", dictionaryFile, err)
}
router := newRouter()
if err := http.ListenAndServe(":8080", router); err != nil {
log.Fatal(err)
}
}
func lettersHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
l := vars["letters"]
resp, err := letters.SolveJSON(l)
if err != nil {
log.Fatalf("failed to solve %q: %v", l, err)
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, resp)
}
func numbersHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
n := vars["numbers"]
a := strings.Split(n, ",")
resp, err := numbers.SolveJSON(a)
if err != nil {
log.Fatalf("failed to solve %q: %v", n, err)
}
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, resp)
}