-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (89 loc) · 2.85 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
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
/*
* Copyright 2014–2018 Kullo GmbH
*
* This source code is licensed under the 3-clause BSD license. See LICENSE.txt
* in the root directory of this source tree for details.
*/
package main
import (
"flag"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"net/http"
"os"
"runtime/debug"
)
var listen = flag.String("listen", ":8080", "where to listen (address and port)")
var errorLogFile = flag.String("error-logfile", "", "file name of the error log")
var dumpDirectory = flag.String("dump-directory", "/tmp", "location of dumps and corresponding data")
var symbolsDirectory = flag.String("symbols-directory", "/opt/breakpad-symbols", "location of the symbols repository")
func writeServerError(err error, rw http.ResponseWriter) {
log.Print(err.Error() + "\n" + string(debug.Stack()))
rw.WriteHeader(http.StatusInternalServerError)
io.WriteString(rw, "Internal Server Error. We're sorry.")
}
func statusHandler(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
io.WriteString(rw, "HTTP 200\n\nUp and running")
}
func uploadHandler(rw http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Bad request: This endpoint only allows POST requests")
return
}
contentType, params, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Bad request: Content-Type cannot be parsed")
return
}
if contentType != "multipart/form-data" {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Bad request: Content-Type must be multipart/form-data")
return
}
mr := multipart.NewReader(req.Body, params["boundary"])
form, err := mr.ReadForm(10 * 1024 * 1024)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Bad request: Multi-part message cannot be parsed")
return
}
minidumpHeader := form.File["upload_file_minidump"]
if minidumpHeader == nil {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Bad request: Body must contain upload_file_minidump")
return
}
uploadId, err := writeToDisk(dumpDirectory, minidumpHeader, form.Value)
if err != nil {
writeServerError(err, rw)
return
}
ProcessCrashreport(uploadId)
rw.WriteHeader(http.StatusOK)
io.WriteString(rw, uploadId)
}
func main() {
log.SetFlags(log.Flags() | log.Lshortfile)
flag.Parse()
if *errorLogFile != "" {
openedErrorLogFile, err := os.OpenFile(
*errorLogFile,
os.O_CREATE|os.O_WRONLY|os.O_APPEND,
0640)
if err != nil {
log.Fatal("Couldn't open error log: " + err.Error())
}
log.SetOutput(openedErrorLogFile)
}
StartCrashreportProcessorWorker()
http.HandleFunc("/status", statusHandler)
http.HandleFunc("/upload", uploadHandler)
log.Print(fmt.Sprintf("Starting HTTP server on '%s' ...", *listen))
log.Fatal(http.ListenAndServe(*listen, nil))
}