-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
191 lines (156 loc) · 4.79 KB
/
handlers.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"html/template"
"log"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/ligurio/recidive/formats"
"github.com/matoous/go-nanoid"
)
// 2MB
const maxSize = 2 * 1024 * 1024
func viewHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP request %s -> %s %s\n", r.RemoteAddr, r.Method, r.URL)
id := mux.Vars(r)["id"]
db := initDb(dbpath)
defer db.Close()
report := new(formats.Report)
if err := db.Where("uid = ?", id).First(&report).Error; err != nil {
log.Println(err.Error())
errorHandler(w, r, http.StatusInternalServerError)
return
}
db.Preload("Suites.Tests").Find(&report)
db.Preload("Suites").Preload("Suites.Tests").Find(&report)
report.Hits = report.Hits + 1
db.Save(&report)
err := templates.ExecuteTemplate(w, "view", report)
if err != nil {
log.Println(err)
errorHandler(w, r, http.StatusInternalServerError)
return
}
}
func opensearchHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP request %s -> %s %s\n", r.RemoteAddr, r.Method, r.URL)
var tmpl = template.Must(template.ParseFiles("static/templates/opensearch.xml"))
err := tmpl.Execute(w, r.Host)
if err != nil {
log.Println("DEBUG: ", err)
errorHandler(w, r, http.StatusInternalServerError)
}
}
func ProcessQuery(r *http.Request) ([]formats.Report, error) {
/*
Process HTTP query and return a list of reports
*/
duration := r.URL.Query().Get("duration")
if duration != "" {
log.Println("URL parameter 'duration' is: ", string(duration))
}
period := 0
period, _ = strconv.Atoi(duration)
db := initDb(dbpath)
defer db.Close()
var reports []formats.Report
if period > 0 {
now := time.Now()
then := now.AddDate(0, 0, 0-period)
db.Where("created_at BETWEEN ? AND ?", then, now).Find(&reports)
} else {
db.Find(&reports)
}
return reports, nil
}
func listHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP request %s -> %s %s\n", r.RemoteAddr, r.Method, r.URL)
log.Println("GET params were:", r.URL.Query())
// TODO: pass r.URL.RequestURI() to chart URL in HTML template
reports, err := ProcessQuery(r)
err = templates.ExecuteTemplate(w, "list", reports)
if err != nil {
errorHandler(w, r, http.StatusInternalServerError)
}
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP request %s -> %s %s\n", r.RemoteAddr, r.Method, r.URL)
if r.Method == "GET" {
err := templates.ExecuteTemplate(w, "upload", nil)
if err != nil {
log.Println("DEBUG: ", err)
errorHandler(w, r, http.StatusInternalServerError)
}
return
}
if err := r.ParseMultipartForm(maxSize); err != nil {
log.Printf("DEBUG: Max size is exceeded in ParseMultipartForm: %s\n", err)
errorHandler(w, r, http.StatusInternalServerError)
return
}
db := initDb(dbpath)
defer db.Close()
for _, fileHeaders := range r.MultipartForm.File {
for _, fileHeader := range fileHeaders {
file, err := fileHeader.Open()
if err != nil {
log.Println("Failed to open file")
}
log.Printf("DEBUG: File: %s\n", fileHeader.Filename)
report, err := formats.ReadReport(file, fileHeader.Filename)
report.UID = makeID()
log.Println("Report ID is", report.UID)
if err == nil && report != nil {
log.Println("DEBUG: successful upload")
db.Debug().Create(&report)
errors := db.GetErrors()
if len(errors) != 0 {
for err := range errors {
log.Println("DEBUG: Insert failed", err)
}
errorHandler(w, r, http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte(report.UID + "\n"))
}
} else {
log.Println("DEBUG: ReadReport failed", err)
errorHandler(w, r, http.StatusInternalServerError)
}
}
}
}
func makeID() string {
id, err := gonanoid.Nanoid(10)
if err != nil {
panic(err)
}
return id
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
switch status {
case http.StatusNotFound:
renderTemplate(w, "notfound")
case http.StatusInternalServerError:
renderTemplate(w, "notfound")
}
}
func StartServer(listenAddr string, staticDir *string) error {
r := mux.NewRouter()
r.StrictSlash(true)
r.HandleFunc("/", listHandler).Methods("GET")
r.HandleFunc("/view/{id}/", viewHandler).Methods("GET")
r.HandleFunc("/upload", uploadHandler).Methods("GET", "POST")
r.HandleFunc("/chart/pie", drawPieChart).Methods("GET")
r.HandleFunc("/chart/bar", drawBarChart).Methods("GET")
r.HandleFunc("/chart/stackedbar", drawStackedBarChart).Methods("GET")
r.HandleFunc("/chart/twoaxes", drawTwoAxesChart).Methods("GET")
r.HandleFunc("/opensearch.xml", opensearchHandler).Methods("GET")
s := http.StripPrefix("/static/", http.FileServer(http.Dir(*staticDir)))
r.PathPrefix("/static/").Handler(s)
http.Handle("/", r)
log.Printf("Start on %s\n", *httpAddr)
return http.ListenAndServe(*httpAddr, r)
}