-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.go
268 lines (234 loc) · 7.3 KB
/
app.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"strings"
"time"
)
type Settings struct {
RedisUrl string
RedisPrefix string
RestrictDomain string
Redirect404 string
UrlLength int
}
type ApiAddRequest struct {
LongUrl string
}
func ApiAddHandler(resp http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body);
if err != nil {
RenderJsonError(resp, req, err.Error(), http.StatusInternalServerError)
return
}
var message ApiAddRequest
dec := json.NewDecoder(strings.NewReader(string(body)))
for {
if err := dec.Decode(&message); err == io.EOF {
break
} else if err != nil {
RenderJsonError(resp, req, err.Error(), http.StatusBadRequest)
return
}
}
if message.LongUrl == "" {
RenderJsonError(resp, req, "No URL to shorten", http.StatusBadRequest)
return
}
gosUrl, err := NewUrl(message.LongUrl)
if err != nil {
RenderJsonError(resp, req, err.Error(), http.StatusBadRequest)
return
}
shortUrl, err := router.Get("redirect").URL("id", gosUrl.Id)
if err != nil {
RenderJsonError(resp, req, err.Error(), http.StatusBadRequest)
return
}
json := fmt.Sprintf("{\"id\":\"http://%s%s\",\"longUrl\":\"%s\"}", req.Host, shortUrl, gosUrl.Destination)
resp.Write([]byte(json))
}
func AddHandler(resp http.ResponseWriter, req *http.Request) {
gosUrl, err := NewUrl(req.FormValue("url"))
if err != nil {
Render(resp, req, "home", map[string]string{"error": err.Error()})
return
}
statsUrl, err := router.Get("stats").URL("id", gosUrl.Id)
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(resp, req, statsUrl.String(), http.StatusFound)
}
func RedirectHandler(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
gosUrl, err := GetUrl(vars["id"])
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
} else if gosUrl == nil {
if settings.Redirect404 != "" {
originalUrl, err := router.Get("redirect").URL("id", vars["id"])
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
}
url404 := strings.Replace(settings.Redirect404, "$gosURL", url.QueryEscape(fmt.Sprintf("http://%s%s", req.Host, originalUrl.String())), 1)
http.Redirect(resp, req, url404, http.StatusTemporaryRedirect)
return
}
RenderError(resp, req, "No URL was found with that goshorty code", http.StatusNotFound)
return
}
request, _ := requestParser.Parse(req)
go gosUrl.Hit(request)
http.Redirect(resp, req, gosUrl.Destination, http.StatusMovedPermanently)
}
func StatHandler(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
if req.Header.Get("X-Requested-With") == "" {
statsUrl, err := router.Get("stats").URL("id", vars["id"])
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(resp, req, statsUrl.String(), http.StatusFound)
return
}
gosUrl, err := GetUrl(vars["id"])
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
} else if gosUrl == nil {
RenderError(resp, req, "No URL was found with that goshorty code", http.StatusNotFound)
return
}
var body []byte
switch {
case vars["what"] == "sources":
stats, err := gosUrl.Sources(false)
if err == nil {
body, err = json.Marshal(stats)
}
default:
stats, err := gosUrl.Stats(vars["what"])
if err == nil {
body, err = json.Marshal(stats)
}
}
if err != nil {
body = []byte(fmt.Sprintf("{\"error\":\"%s\"}", err.Error()))
}
resp.Header().Set("Content-Type", "application/json")
resp.Write(body)
}
func StatsHandler(resp http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
gosUrl, err := GetUrl(vars["id"])
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
} else if gosUrl == nil {
RenderError(resp, req, "No URL was found with that goshorty code", http.StatusNotFound)
return
}
hits, err := gosUrl.Hits()
if err != nil {
RenderError(resp, req, err.Error(), http.StatusInternalServerError)
return
}
Render(resp, req, "stats", map[string]string{
"id": gosUrl.Id,
"url": gosUrl.Destination,
"when": relativeTime(time.Now().Sub(gosUrl.Created)),
"hits": fmt.Sprintf("%d", hits),
})
}
func HomeHandler(resp http.ResponseWriter, req *http.Request) {
Render(resp, req, "home", nil)
}
func relativeTime(duration time.Duration) string {
hours := int64(math.Abs(duration.Hours()))
minutes := int64(math.Abs(duration.Minutes()))
when := ""
switch {
case hours >= (365 * 24):
when = "Over an year ago"
case hours > (30 * 24):
when = fmt.Sprintf("%d months ago", int64(hours/(30*24)))
case hours == (30 * 24):
when = "a month ago"
case hours > 24:
when = fmt.Sprintf("%d days ago", int64(hours/24))
case hours == 24:
when = "yesterday"
case hours >= 2:
when = fmt.Sprintf("%d hours ago", hours)
case hours > 1:
when = "over an hour ago"
case hours == 1:
when = "an hour ago"
case minutes >= 2:
when = fmt.Sprintf("%d minutes ago", minutes)
case minutes > 1:
when = "a minute ago"
default:
when = "just now"
}
return when
}
var (
router = mux.NewRouter()
settings = new(Settings)
requestParser *RequestParser
)
func main() {
var (
geoDb string
redisHost string
redisPort int
redisPrefix string
regex string
port int
)
flag.StringVar(&redisHost, "redis_host", "", "Redis host (leave empty for localhost)")
flag.IntVar(&redisPort, "redis_port", 6379, "Redis port")
flag.StringVar(&redisPrefix, "redis_prefix", "goshorty:", "Redis prefix to use")
flag.StringVar(&settings.RestrictDomain, "domain", "", "Restrict destination URLs to a single domain")
flag.StringVar(&settings.Redirect404, "redirect_404", "", "Restrict destination URLs to a single domain")
flag.IntVar(&settings.UrlLength, "length", 5, "How many characters should the short code have")
flag.StringVar(®ex, "regex", "[A-Za-z0-9]{%d}", "Regular expression to match route for accessing a short code. %d is replaced with <length> setting")
flag.IntVar(&port, "port", 8080, "Port where server is listening on")
flag.StringVar(&geoDb, "geo_db", "./GeoIP.dat", "Location to the MaxMind GeoIP country database file")
flag.Parse()
var err error
requestParser, err = NewRequestParser(geoDb)
if err != nil {
panic(err)
}
regex = fmt.Sprintf(regex, settings.UrlLength)
settings.RedisUrl = fmt.Sprintf("%s:%d", redisHost, redisPort)
settings.RedisPrefix = redisPrefix
router.HandleFunc("/api/v1/url", ApiAddHandler).Methods("POST").Name("add")
router.HandleFunc("/add", AddHandler).Methods("POST").Name("add")
router.HandleFunc("/{id:"+regex+"}+/{what:(hour|day|week|month|year|all|sources)}", StatHandler).Name("stat")
router.HandleFunc("/{id:"+regex+"}+", StatsHandler).Name("stats")
router.HandleFunc("/{id:"+regex+"}", RedirectHandler).Name("redirect")
router.HandleFunc("/", HomeHandler).Name("home")
for _, dir := range []string{"css", "js", "img"} {
router.PathPrefix("/" + dir + "/").Handler(http.StripPrefix("/"+dir+"/", http.FileServer(http.Dir("assets/"+dir))))
}
fmt.Println(fmt.Sprintf("Server is listening on port %d", port))
err = http.ListenAndServe(fmt.Sprintf(":%d", port), router)
if err != nil {
panic(err)
}
}