This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
234 lines (186 loc) · 5.19 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/droundy/goopt"
)
var (
wsReplacer = strings.NewReplacer("__", "_", "_", " ")
revWsReplacer = strings.NewReplacer(" ", "_", "_", "__", "-", "--")
// set last modifed to server startup. close enough to release.
lastModified = time.Now()
lastModifiedStr = lastModified.UTC().Format(http.TimeFormat)
oneYear = time.Duration(8700) * time.Hour
staticPath, _ = resourcePaths()
)
func shift(s []string) ([]string, string) {
return s[1:], s[0]
}
func invalidRequest(w http.ResponseWriter, r *http.Request) {
log.Println("bad request", r.URL.String())
http.Error(w, "bad request", 400)
}
func parseFileName(name string) (d Data, err error) {
imageName := wsReplacer.Replace(name)
imageParts := strings.Split(imageName, "-")
newParts := []string{}
for len(imageParts) > 0 {
var head, right string
imageParts, head = shift(imageParts)
// if starts with - append to previous
if len(head) == 0 && len(newParts) > 0 {
left := ""
if len(newParts) > 0 {
left = newParts[len(newParts)-1]
newParts = newParts[:len(newParts)-1]
}
// trailing -- is going to break color anyways so don't worry
imageParts, right = shift(imageParts)
head = strings.Join([]string{left, right}, "-")
}
newParts = append(newParts, head)
}
if len(newParts) != 3 {
err = errors.New("Invalid file name")
return
}
if !strings.HasSuffix(newParts[2], ".png") {
err = errors.New("Unknown file type")
return
}
cp := newParts[2][0 : len(newParts[2])-4]
c, err := getColor(cp)
if err != nil {
return
}
d = Data{newParts[0], newParts[1], c}
return
}
func buckle(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 {
invalidRequest(w, r)
return
}
d, err := parseFileName(parts[2])
if err != nil {
invalidRequest(w, r)
return
}
t, err := time.Parse(time.RFC1123, r.Header.Get("if-modified-since"))
if err == nil && lastModified.Before(t.Add(1*time.Second)) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Add("content-type", "image/png")
w.Header().Add("expires", time.Now().Add(oneYear).Format(time.RFC1123))
w.Header().Add("cache-control", "public")
w.Header().Add("last-modified", lastModifiedStr)
makePngShield(w, d)
}
const basePkg = "github.com/badges/buckler"
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(staticPath, "index.html"))
}
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(staticPath, "favicon.png"))
}
func fatal(msg string) {
fmt.Println(msg)
os.Exit(1)
}
func cliMode(vendor string, status string, color string, args []string) {
// if any of vendor, status or color is given, all must be
if (vendor != "" || status != "" || color != "") &&
!(vendor != "" && status != "" && color != "") {
fatal("You must specify all of vendor, status, and color")
}
if vendor != "" {
c, err := getColor(color)
if err != nil {
fatal("Invalid color: " + color)
}
d := Data{vendor, status, c}
name := fmt.Sprintf("%s-%s-%s.png", revWsReplacer.Replace(vendor),
revWsReplacer.Replace(status), color)
if len(args) > 1 {
fatal("You can only specify one output file name")
}
if len(args) == 1 {
name = args[0]
}
// default to standard out
f := os.Stdout
if name != "-" {
f, err = os.Create(name)
if err != nil {
fatal("Unable to create file: " + name)
}
}
makePngShield(f, d)
return
}
// generate based on command line file names
for i := range args {
name := args[i]
d, err := parseFileName(name)
if err != nil {
fatal(err.Error())
}
f, err := os.Create(name)
if err != nil {
fatal(err.Error())
}
makePngShield(f, d)
}
}
func usage() string {
u := `Usage: %s [-h HOST] [-p PORT]
%s [-v VENDOR -s STATUS -c COLOR] <FILENAME>
%s`
return fmt.Sprintf(u, os.Args[0], os.Args[0], goopt.Help())
}
func main() {
hostEnv := os.Getenv("HOST")
portEnv := os.Getenv("PORT")
// default to environment variable values (changes the help string :( )
if hostEnv == "" {
hostEnv = "*"
}
p := 8080
if portEnv != "" {
p, _ = strconv.Atoi(portEnv)
}
goopt.Usage = usage
// server mode options
host := goopt.String([]string{"-h", "--host"}, hostEnv, "host ip address to bind to")
port := goopt.Int([]string{"-p", "--port"}, p, "port to listen on")
// cli mode
vendor := goopt.String([]string{"-v", "--vendor"}, "", "vendor for cli generation")
status := goopt.String([]string{"-s", "--status"}, "", "status for cli generation")
color := goopt.String([]string{"-c", "--color", "--colour"}, "", "color for cli generation")
goopt.Parse(nil)
args := goopt.Args
// if any of the cli args are given, or positional args remain, assume cli
// mode.
if len(args) > 0 || *vendor != "" || *status != "" || *color != "" {
cliMode(*vendor, *status, *color, args)
return
}
// normalize for http serving
if *host == "*" {
*host = ""
}
http.HandleFunc("/v1/", buckle)
http.HandleFunc("/favicon.png", favicon)
http.HandleFunc("/", index)
log.Println("Listening on port", *port)
http.ListenAndServe(*host+":"+strconv.Itoa(*port), nil)
}