-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
136 lines (114 loc) · 3.34 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
const (
rateLimitSecs = 10
rateLimitRequests = 10
redisPortEnv = "REDIS_PORT"
hostnameEnv = "LINKTO_HOSTNAME"
wordfilesEnv = "LINKTO_WORDFILES"
passwordEnv = "LINKTO_PASSWORD"
linktoPort = ":9091"
)
type response struct {
Longurl string `json:"longurl"`
Shorturl string `json:"shorturl"`
}
type handler struct {
long StringStore
short StringStore
custom StringStore
hostname string
shortener shortener
}
func newHandler(hostname string, s shortener) handler {
long := newStringStore(longmapName)
short := newStringStore(shortmapName)
custom := newStringStore(custommapName)
return handler{long, short, custom, hostname, s}
}
func (h handler) shorten(w http.ResponseWriter, r *http.Request) {
longurl := r.Form.Get("longurl")
if !strings.HasPrefix(longurl, "http") {
longurl = "http://" + longurl
}
existing, err := h.long.Get(longurl)
if err == nil {
w.Header().Set("Content-Type", "text/json")
w.Write(respJSON(longurl, h.hostname, existing))
return
}
shorturl := h.shortener.getShortURL()
h.long.Set(longurl, shorturl)
h.short.Set(shorturl, longurl)
w.Header().Set("Content-Type", "text/json")
w.WriteHeader(http.StatusCreated)
w.Write(respJSON(longurl, h.hostname, shorturl))
}
func (h handler) customShorten(w http.ResponseWriter, r *http.Request) {
longurl := r.Form.Get("longurl")
if !strings.HasPrefix(longurl, "http") {
longurl = "http://" + longurl
}
customurl := r.Form.Get("customurl")
_, errcu := h.custom.Get(customurl)
_, errsh := h.short.Get(customurl)
if errcu == nil || errsh == nil {
fmt.Fprintf(w, "Custom URL %s/%s is already taken\n", h.hostname, customurl)
return
}
h.custom.Set(customurl, longurl)
w.Header().Set("Content-Type", "text/json")
w.WriteHeader(http.StatusCreated)
w.Write(respJSON(longurl, h.hostname, customurl))
}
func respJSON(longurl, hostname, shorturl string) []byte {
s := fmt.Sprintf("%s/%s", hostname, shorturl)
r := response{Longurl: longurl, Shorturl: s}
body, _ := json.Marshal(r)
return body
}
func (h handler) redirect(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:len(r.URL.Path)]
if path == "" {
w.Write([]byte(mainpage))
return
}
longurl, err := h.short.Get(path)
if err == nil {
http.Redirect(w, r, longurl, http.StatusMovedPermanently)
return
}
longurl, err = h.custom.Get(path)
if err == nil {
http.Redirect(w, r, longurl, http.StatusMovedPermanently)
return
}
http.Error(w, fmt.Sprintf("No link found for %s", path), http.StatusNotFound)
}
func main() {
redisPort := os.Getenv(redisPortEnv)
err := setupRedis(redisPort[strings.LastIndex(redisPort, "/")+1:])
if err != nil {
log.Fatalln(err)
}
files := strings.Split(os.Getenv(wordfilesEnv), " ")
shortener, err := newShortener(newStringStore(shortmapName), files)
if err != nil {
log.Fatalln(err)
}
hostname := os.Getenv(hostnameEnv)
h := newHandler(hostname, shortener)
shorten := MustParams(RateLimit(h.shorten, rateLimitSecs, rateLimitRequests, newExpireStore()), "longurl")
http.HandleFunc("/shorten", LogResp(CORS(shorten)))
customShorten := MustParams(SimpleAuth(h.customShorten), "longurl", "customurl")
http.HandleFunc("/customshorten", LogResp(CORS(customShorten)))
http.HandleFunc("/", LogResp(h.redirect))
log.Fatalln(http.ListenAndServe(linktoPort, nil))
}