-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (127 loc) · 3.83 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
package main
import (
"fmt"
"io/ioutil"
"encoding/json"
"net/url"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
"github.com/pilu/go-base62"
)
type Configuration struct {
Redis RedisConfig `json:"redis"`
Bind string `json:"bind"`
}
type RedisConfig struct {
Addr string `json:"addr"`
Password string `json:"password"`
DB int `json:"db"`
}
func LoadConfig() (config Configuration) {
file, err := ioutil.ReadFile("./config.json")
if err != nil {
panic("Unable to read config file.")
}
var cfg Configuration
err = json.Unmarshal(file, &cfg)
if err != nil {
panic("Unable to parse config file.")
}
return cfg
}
func main() {
config := LoadConfig()
r := gin.Default()
red := redis.NewClient(&redis.Options{
Addr: config.Redis.Addr,
Password: config.Redis.Password,
DB: config.Redis.DB,
})
_, err := red.Ping().Result()
if err != nil {
panic(err)
}
r.GET("/", func (ctx *gin.Context) {
ctx.Header("Content-Type", "text/html")
ctx.String(200, `<!DOCTYPE html>
<html>
<head>
<title>Kogara - Link Shortening</title>
</head>
<body>
<form action="/" method="POST">
<input type="text" name="url"><br>
<input type="submit" value="Shorten">
</form>
</body>
</html>`);
})
r.POST("/", func (ctx *gin.Context) {
if ctx.PostForm("url") == "" {
ctx.String(400, "URL cannot be empty.")
return
}
link, err := url.Parse(ctx.PostForm("url"))
if err != nil {
ctx.String(500, "Something went wrong parsing the URL.")
return
} else if (link.Scheme != "http" && link.Scheme != "https") {
ctx.String(400, "This URL shortener currently only supports http and https links.")
return
} else if link.Host == "" {
ctx.String(400, "No host was found")
return
}
count, err := red.Incr("kogara:counter").Result()
if err != nil {
ctx.String(500, "Unable to create link.")
return
}
id := base62.Encode(int(count));
_, err = red.Set("kogara:links:" + id, ctx.PostForm("url"), 0).Result()
if err != nil {
ctx.String(500, "Unable to create link.")
} else {
ctx.Header("Content-Type", "text/html")
ctx.String(200,fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title>Kogara - Link Created</title>
</head>
<body>
Created <a href="/r/%s">/r/%s</a>
</body>
</html>`, id, id))
}
})
r.GET("/r/:id", func (ctx *gin.Context) {
link, err := red.Get("kogara:links:" + ctx.Params.ByName("id")).Result()
if err == redis.Nil {
ctx.String(404, "This link does not seem to exist.")
} else if err != nil {
ctx.String(500, "Sorry, something went wrong while trying to fetch this link.")
} else {
red.Incr("kogara:analytics:" + ctx.Params.ByName("id"))
ctx.Redirect(301, link)
}
})
r.GET("/+/:id", func (ctx *gin.Context) {
count, err := red.Get("kogara:analytics:" + ctx.Params.ByName("id")).Result()
if err == redis.Nil {
ctx.String(404, "Not found")
} else if err != nil {
ctx.String(500, "Something went wrong.")
} else {
ctx.String(200, count)
}
})
r.GET("/check/:id", func (ctx *gin.Context) {
exists, err := red.Exists("kogara:links:" + ctx.Params.ByName("id")).Result()
if err != nil {
ctx.JSON(500, gin.H{"error": true, "available": nil})
} else {
ctx.JSON(200, gin.H{"error": nil, "available": exists != 1})
}
})
r.Run(config.Bind)
}