-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
91 lines (83 loc) · 2.23 KB
/
handler.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
package handler
import (
"fmt"
"encoding/json"
"log"
"net/url"
"github.com/valyala/fasthttp"
"../storage"
)
func Handler(ctx *fasthttp.RequestCtx) {}
func CreateUrlHandler(h fasthttp.RequestHandler, s *storage.Storage, c Configuration) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
if !CheckAuthentication(c, ctx) {
ctx.SetStatusCode(401)
fmt.Fprintf(ctx, "Unauthorized")
} else {
var urlAddress string
if ctx.Request.Header.IsPost() {
params := ctx.Request.PostArgs()
urlAddress = string(params.Peek("url"))
} else {
query := ctx.URI().QueryArgs()
urlAddress = string(query.Peek("url"))
}
_, err := url.ParseRequestURI(urlAddress)
if err != nil {
ctx.SetStatusCode(500)
fmt.Fprintf(ctx, "Invalid URL")
} else {
url := storage.CreateUrl(s, urlAddress)
fmt.Fprintf(ctx, "%s", ConvertUrlToJson(url))
ctx.SetContentType("application/json")
}
}
h(ctx)
})
}
func GetUrlHandler(h fasthttp.RequestHandler, s *storage.Storage, c Configuration) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
if !CheckAuthentication(c, ctx) {
ctx.SetStatusCode(401)
} else {
uid := ctx.UserValue("uid").(string)
url := storage.GetUrl(s, uid)
if url.Url != "" {
fmt.Fprintf(ctx, "%s", ConvertUrlToJson(*url))
ctx.SetContentType("application/json")
} else {
ctx.SetStatusCode(404)
fmt.Fprintf(ctx, "Not Found")
}
}
h(ctx)
})
}
func RedirectHandler(h fasthttp.RequestHandler, s *storage.Storage) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
uid := ctx.UserValue("uid").(string)
url := storage.GetUrl(s, uid)
if url.Url != "" {
storage.UpdateStatistics(s, uid)
ctx.Redirect(url.Url, 301)
} else {
ctx.SetStatusCode(404)
fmt.Fprintf(ctx, "Not Found")
}
h(ctx)
})
}
func CheckAuthentication(c Configuration, ctx *fasthttp.RequestCtx) bool {
if c.Authentication {
token := ctx.Request.Header.Peek(c.AuthenticationParameter)
return string(token) == c.AuthenticationToken
}
return true
}
func ConvertUrlToJson(url storage.Url) string {
j, err := json.Marshal(url)
if err != nil {
log.Println(err)
}
return string(j);
}