/
main.go
72 lines (59 loc) · 1.36 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
69
70
71
72
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/tedsuo/rata"
)
const viewsDir = "public"
func newTestsHandler(c *Collector) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(c.CollectResults())
})
}
func newServer(args []string) (*http.Server, error) {
c, err := NewCollector(args[0])
if err != nil {
fmt.Errorf("Error creating test collector: %s\n", err)
return nil, err
}
log.Printf("Watching reports dir [%s]\n", c.ReportsDir)
routes := rata.Routes{
{Name: "get_index", Method: "GET", Path: "/"},
{Name: "get_tests", Method: "GET", Path: "/tests"},
}
handlers := map[string]http.Handler{
"get_index": http.FileServer(http.Dir(viewsDir)),
"get_tests": newTestsHandler(c),
}
router, err := rata.NewRouter(routes, handlers)
if err != nil {
return nil, err
}
server := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return server, nil
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "usage: gawker [reports dir]\n")
os.Exit(1)
}
s, err := newServer(args)
if err != nil {
fmt.Errorf("Error creating server: ", err)
os.Exit(1)
}
log.Fatal(s.ListenAndServe())
}