-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (70 loc) · 1.91 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
package main
import (
"net/http"
"os"
"github.com/2enhance/placeholdr/config"
"github.com/2enhance/placeholdr/svg"
"github.com/apex/log"
"github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
"github.com/gorilla/pat"
)
// use JSON logging when run by Up (including `up start`)
func init() {
if os.Getenv("UP_STAGE") == "" {
log.SetHandler(text.Default)
} else {
log.SetHandler(json.Default)
}
}
func main() {
addr := ":" + os.Getenv("PORT")
app := pat.New()
app.Get("/avatar/{dimensions}", AvatarHandler)
app.Get("/avatar", AvatarHandler)
app.Get("/logo/{dimensions}", LogoHandler)
app.Get("/logo", LogoHandler)
app.Get("/{dimensions}", PlaceholderHandler)
app.Get("/", PlaceholderHandler)
if err := http.ListenAndServe(addr, app); err != nil {
log.WithError(err).Fatal("error listening")
}
}
// LogoHandler responds to GET @ /logo/{?dimensions}
func LogoHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
query := req.URL.Query()
options := config.NewLogoOptions(
query.Get(":dimensions"),
query.Get("id"),
)
svg.NewLogo(w, options)
}
// AvatarHandler responds to GET @ /avatar/{?dimensions}
func AvatarHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
query := req.URL.Query()
options := config.NewAvatarOptions(
query.Get(":dimensions"),
query.Get("id"),
)
svg.NewAvatar(w, options)
}
// PlaceholderHandler responds to GET @ /{dimensions}
func PlaceholderHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
w.WriteHeader(http.StatusOK)
query := req.URL.Query()
isGrey := false
if _, ok := query["grey"]; ok {
isGrey = true
}
options := config.NewPlaceholderOptions(
query.Get(":dimensions"),
query.Get("id"),
isGrey,
)
svg.NewPlaceholder(w, options)
}