-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (123 loc) · 3.08 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
package main
import (
"fmt"
"github.com/go-chi/chi"
"github.com/go-redis/redis"
"html/template"
"log"
"math/rand"
"net/http"
"os"
"path"
"regexp"
"strings"
"time"
)
var redisAddress = "localhost:6379"
var redisPwd = os.Getenv("REDIS_PWD")
//var redisPwd = ""
var minUrlLength = 5
var maxUrlLength = 13
var client = redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: redisPwd,
DB: 10,
})
func main() {
// Test db connection
pong, err := client.Ping().Result()
if err != nil {
log.Fatal(err)
return
}
if pong != "" {
fmt.Println("Redis say pong!")
}
r := chi.NewRouter()
r.Get("/", homeHandler)
r.Get("/{linkId}", linkHandler)
fmt.Println("Server started at port 8080")
http.ListenAndServe(":8080", r)
}
func validateUrl(url string) bool {
var regex, err = regexp.Compile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`)
if err != nil {
fmt.Println(err.Error())
}
var isMatch = regex.MatchString(url)
return isMatch
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
var filepath = path.Join("template", "index.html")
var tmpl, err = template.ParseFiles(filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// check first if url parameter exists
url := r.FormValue("url")
var shortKey = ""
if validateUrl(url) {
// give shortened url
for true {
shortKey = getString(minUrlLength, maxUrlLength)
_, err := client.Get(shortKey).Result()
if err == redis.Nil {
// If the key does not already exist, use the key
break
}
}
err := client.Set(shortKey, url, 0).Err()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var dataRender = map[string]interface{}{
"url": shortKey,
}
err = tmpl.Execute(w, dataRender)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func linkHandler(w http.ResponseWriter, r *http.Request) {
link := chi.URLParam(r, "linkId")
trueLink, err := client.Get(link).Result()
if err == redis.Nil {
http.Error(w, "URL not found", http.StatusNotFound)
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Redirect(w, r, trueLink, http.StatusFound)
}
}
func getString(minLength, maxLength int) string {
const seedBytes = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ023579_-"
var src = rand.NewSource(time.Now().UnixNano())
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
if minLength >= maxLength {
log.Fatal("Invalid length")
}
if minLength < 1 {
log.Fatal("Invalid minlength")
}
// https://stackoverflow.com/a/31832326
n := (src.Int63() % int64(maxLength-minLength)) + int64(minLength)
sb := strings.Builder{}
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(seedBytes) {
sb.WriteByte(seedBytes[idx])
i--
}
cache >>= letterIdxBits
remain--
}
return sb.String()
}