-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemogen.go
181 lines (150 loc) · 4.09 KB
/
emogen.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
// Copyright 2019, Joren Van Onder ([email protected])
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"log"
"math"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
func randomIndex(length uint) int {
index := math.Floor(rand.Float64() * float64(length))
return int(index)
}
func getShortLink(redisConn redis.Conn, shortlink string) (string, error) {
return redis.String(redisConn.Do("GET", "shortlink:"+shortlink))
}
func randomEmojiString(length uint) string {
return fmt.Sprintf(
"%s%s%s",
emojis[randomIndex(length)],
emojis[randomIndex(length)],
emojis[randomIndex(length)],
)
}
func getEmojis(redisPool *redis.Pool, length uint) string {
redisConn := redisPool.Get()
defer redisConn.Close()
for {
newEmojis := randomEmojiString(length)
link, _ := getShortLink(redisConn, newEmojis)
if link != "" {
log.Printf("collision for %s, retrying...\n", newEmojis)
} else {
return newEmojis
}
}
}
type toShorten struct {
Link string `json:"link" binding:"required"`
}
func setupRouter(router *gin.Engine, redisPool *redis.Pool) {
router.GET("/:link", func(c *gin.Context) {
redisConn := redisPool.Get()
defer redisConn.Close()
shortlink := c.Param("link")
link, err := getShortLink(redisConn, shortlink)
if err != nil {
link = "/"
}
log.Printf("resolving %s to %s\n", shortlink, link)
c.Redirect(http.StatusMovedPermanently, link)
})
router.POST("/", func(c *gin.Context) {
redisConn := redisPool.Get()
defer redisConn.Close()
var json toShorten
if err := c.ShouldBindJSON(&json); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
link := json.Link
log.Printf("shortening %s\n", link)
shortLink, err := redis.String(redisConn.Do("GET", "link:"+link))
if err == nil {
c.JSON(200, gin.H{
"link": "/" + shortLink,
})
return
}
if !strings.HasPrefix(link, "http") {
log.Printf("link %s did not specify http\n", link)
link = "http://" + link
}
shortLink = getEmojis(redisPool, uint(len(emojis)))
_, err = redisConn.Do("SET", "shortlink:"+shortLink, link)
if err != nil {
log.Printf("Error while storing link %s -> %s (%s)\n", shortLink, link, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed connecting to db.",
})
return
}
_, err = redisConn.Do("SET", "link:"+link, shortLink)
if err != nil {
log.Printf("Error while storing link %s -> %s (%s)\n", link, shortLink, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed connecting to db.",
})
return
}
c.JSON(200, gin.H{
"link": "/" + shortLink,
})
})
}
func setup() {
rand.Seed(time.Now().UnixNano())
log.SetPrefix("[EMOGEN] ")
log.Printf("Starting up...")
log.Printf("Prepared %d emojis\n", len(emojis))
}
func setupRedis() redis.Pool {
return redis.Pool{
MaxIdle: 8,
MaxActive: 16,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatalf("Could not connect to redis (%s)\n", err)
}
return c, err
},
}
}
func addr() string {
ip := os.Getenv("EMOGEN_LOCALHOST_IP")
if ip == "" {
ip = "127.0.0.1"
}
port := os.Getenv("EMOGEN_LOCALHOST_PORT")
if port == "" {
port = "80"
}
return ip + ":" + port
}
func main() {
setup()
redisPool := setupRedis()
defer redisPool.Close()
router := gin.Default()
router.Use(cors.Default())
setupRouter(router, &redisPool)
router.Run(addr())
}