-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoopurl.go
284 lines (233 loc) · 5.55 KB
/
coopurl.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// coopurl is an url shortener library using badger.
package coopurl
import (
"crypto/sha256"
"fmt"
"net/http"
"net/url"
"path"
"sync"
"time"
"github.com/dgraph-io/badger/v3"
)
const (
DefaultDbPath = "/tmp/badger"
DefaultLength = 8
)
// Handler is the handler for our library.
// It should be created with the New() function.
type Handler struct {
db *badger.DB
mu sync.Mutex
path string // will only affect the database if it's set before the database is initialized.
logger Logger
TTL time.Duration
Length int
Scheme string
}
// New creates a new Handler.
// It implements the option pattern to change the default value of our handler.
func New(opts ...Options) (*Handler, error) {
var h Handler
h.logger = NilLogger{}
for _, opt := range opts {
opt(&h)
}
if err := h.open(); err != nil {
return nil, err
}
return &h, nil
}
type Options func(*Handler)
func WithDbPath(path string) Options {
return func(h *Handler) {
h.path = path
}
}
func WithDefaultTTL(ttl time.Duration) Options {
return func(h *Handler) {
h.TTL = ttl
}
}
func WithDefaultLength(length int) Options {
return func(h *Handler) {
h.Length = length
}
}
func WithLogger(logger badger.Logger) Options {
return func(h *Handler) {
h.logger = logger
}
}
func (h *Handler) init() error {
if h.logger == nil {
h.logger = NilLogger{}
}
if h.db == nil {
return h.open()
}
return nil
}
func (h *Handler) getPath() string {
if h.path == "" {
return DefaultDbPath
}
return h.path
}
func (h *Handler) getLength(r req) int {
if r.length > 0 {
return r.length
}
if h.Length > 0 {
return h.Length
}
return DefaultLength
}
func (h *Handler) open() error {
h.mu.Lock()
defer h.mu.Unlock()
var err error
opt := badger.DefaultOptions(h.getPath())
opt = opt.WithLogger(h.logger)
h.db, err = badger.Open(opt)
return err
}
// Close stops the database connection.
func (h *Handler) Close() {
h.logger.Infof("Closing handler")
h.db.Close()
}
// ServeHTTP is an http.HandleFunc that will redirect the client to the url linked to the id given in the request url.
// This id is the last part of request url path. eg: "domain.com/r/{id}"
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := h.init(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// Maybe check only for get methods
_, id := path.Split(r.URL.Path)
u, err := h.Get(id)
if err != nil {
// TODO: differentiate between not found and other errors
w.WriteHeader(http.StatusInternalServerError)
return
}
h.logger.Infof("Redirect from %s to %s", id, u)
if err := redirect(w, r, u); err != nil {
h.logger.Errorf("Couldn't redirect to %s", u)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func redirect(w http.ResponseWriter, r *http.Request, s string) error {
u, err := url.Parse(s)
if err != nil {
return err
}
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
return nil
}
// Get search the store for the url linked to the given id.
func (h *Handler) Get(id string) (string, error) {
if err := h.init(); err != nil {
return "", err // Maybe wrap err with custom error
}
return h.get(id)
}
func (h *Handler) get(id string) (string, error) {
var url string
err := h.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(id))
if err != nil {
return err
}
b, err := item.ValueCopy(nil)
if err != nil {
return err
}
url = string(b)
return nil
})
if err != nil {
return "", err
}
h.logger.Infof("Get entry: %s - %s", id, url)
return url, nil
}
// Post will take a url, store it and return an id linked to it.
func (h *Handler) Post(url string, opts ...ReqOptions) (string, error) {
if err := h.init(); err != nil {
return "", err // Maybe wrap err with custom error
}
return h.post(url)
}
func (h *Handler) post(s string, opts ...ReqOptions) (string, error) {
// Check that the given url is valid
u, err := url.Parse(s)
if err != nil {
return "", err
}
// We set the scheme to http if it's missing to redirect to google.fr and not domain/r/google.fr
if u.Scheme == "" {
h.logger.Infof("Setting missing scheme to http: %s", u.String())
u.Scheme = "http"
}
r := req{}
for _, opt := range opts {
opt(&r)
}
// Generate Id
id := generateId(u.String(), h.getLength(r))
ttl := r.ttl
if ttl == 0 {
ttl = h.TTL
}
// Put in db
err = h.db.Update(func(txn *badger.Txn) error {
if ttl != 0 {
e := badger.NewEntry([]byte(id), []byte(u.String())).WithTTL(ttl)
return txn.SetEntry(e)
}
return txn.Set([]byte(id), []byte(u.String()))
})
if err != nil {
return "", err
}
if ttl != 0 {
h.logger.Infof("New entry: %s - %s (ttl: %s)", id, u.String(), ttl)
} else {
h.logger.Infof("New entry: %s - %s", id, u.String())
}
return id, err
}
type ReqOptions func(*req)
func WithTTL(ttl time.Duration) ReqOptions {
return func(r *req) {
r.ttl = ttl
}
}
func WithLength(length int) ReqOptions {
return func(r *req) {
r.length = length
}
}
type req struct {
ttl time.Duration
length int
}
// generateId generates an id from url of size n
func generateId(url string, n int) string {
s := fmt.Sprintf("%s-%s", url, time.Now())
sha := fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
if n >= len(sha) {
return sha
}
return sha[:n]
}
type Logger badger.Logger
// NilLogger is a logger that doesn't log
type NilLogger struct{}
var _ Logger = (*NilLogger)(nil)
func (NilLogger) Errorf(string, ...interface{}) {}
func (NilLogger) Warningf(string, ...interface{}) {}
func (NilLogger) Infof(string, ...interface{}) {}
func (NilLogger) Debugf(string, ...interface{}) {}