This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 799
/
requesthandler.go
74 lines (66 loc) · 1.7 KB
/
requesthandler.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
package weblog
import (
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/deis/deis/logger/syslogish"
)
var getRegex *regexp.Regexp
var deleteRegex *regexp.Regexp
func init() {
getRegex = regexp.MustCompile(`^/([-a-z0-9]+)/?(?:\?log_lines=([0-9]+))?$`)
deleteRegex = regexp.MustCompile(`^/([-a-z0-9]+)/?$`)
}
type requestHandler struct {
syslogishServer *syslogish.Server
}
func (h requestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
h.serveGet(w, r)
} else if r.Method == "DELETE" {
h.serveDelete(w, r)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func (h requestHandler) serveGet(w http.ResponseWriter, r *http.Request) {
match := getRegex.FindStringSubmatch(r.RequestURI)
if match == nil {
w.WriteHeader(http.StatusNotFound)
return
}
app := match[1]
logLines, err := strconv.Atoi(match[2])
if err != nil || logLines < 1 {
log.Printf("Invalid number of log lines specified by request for `%s`; defaulting to 100 lines.", r.RequestURI)
logLines = 100
}
logs, err := h.syslogishServer.ReadLogs(app, logLines)
if err != nil {
log.Println(err)
if strings.HasPrefix(err.Error(), "Could not find logs for") {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
for _, line := range logs {
fmt.Fprintf(w, "%s\n", line)
}
}
func (h requestHandler) serveDelete(w http.ResponseWriter, r *http.Request) {
match := deleteRegex.FindStringSubmatch(r.RequestURI)
if match == nil {
w.WriteHeader(http.StatusNotFound)
return
}
app := match[1]
if err := h.syslogishServer.DestroyLogs(app); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
}