This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
78 lines (66 loc) · 1.94 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
package main
import (
"fmt"
"github.com/datatogether/core"
"io"
"net/http"
"strconv"
)
func reqParamInt(key string, r *http.Request) (int, error) {
i, err := strconv.ParseInt(r.FormValue(key), 10, 0)
return int(i), err
}
func reqParamBool(key string, r *http.Request) (bool, error) {
return strconv.ParseBool(r.FormValue(key))
}
func DownloadUrlHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
DownloadUrl(w, r)
default:
NotFoundHandler(w, r)
}
}
func DownloadUrl(w http.ResponseWriter, r *http.Request) {
u := &core.Url{Id: r.URL.Path[len("/urls/"):]}
if err := u.Read(store); err != nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, err.Error())
return
}
f, err := u.File()
if err != nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, err.Error())
return
}
if err := f.GetS3(); err != nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, err.Error())
return
}
w.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, u.FileName))
w.Write(f.Data)
}
// HealthCheckHandler is a basic "hey I'm fine" for load balancers & co
// TODO - add Database connection & proper configuration checks here for more accurate
// health reporting
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "status" : 200 }`))
}
// EmptyOkHandler is an empty 200 response, often used
// for OPTIONS requests that responds with headers set in addCorsHeaders
func EmptyOkHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
// CertbotHandler pipes the certbot response for manual certificate generation
func CertbotHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, cfg.CertbotResponse)
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{ "status" : "not found" }`))
}