-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
275 lines (241 loc) · 5.29 KB
/
routes.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
"strings"
)
const domain = "http://localhost:9595/"
const indexPage = `
<!DOCTYPE html>
<html>
<head>
<title>URL Shortener</title>
<meta charset="UTF-8">
<style type="text/css">
body {
color: #555;
}
.container {
width: 960px;
margin: 20px auto;
text-align: center;
}
.sub-text {
font-size: 0.85em;
margin-top: -15px;
}
.box {
width: 500px;
background-color: #f9f3ec;
padding: 20px;
border-radius: 2px;
margin: 15px auto;
position: relative;
z-index: 100;
}
.box input[type="text"] {
width: 400px;
height: 35px;
margin: 0 auto;
border: 1px solid #bbb;
font-size: 1.1em;
padding: 0 10px 0 10px;
border-radius: 2px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.box input[type="submit"] {
width: 300px;
height: 35px;
border: none;
background-color: #2da7ee;
color: #fff;
font-size: 1em;
margin: 10px auto;
border-radius: 2px;
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.box input[type="submit"]:hover {
background-color: #1398e6;
}
.footer {
font-size: 0.85em;
}
.footer a {
color: #555;
text-decoration: none;
font-weight: bold;
}
.footer a:hover {
color: #5cbaf2;
}
.extension {
width: 500px;
margin: 0 auto;
font-size: 0.75em;
padding: 25px 20px 20px 20px;
background-color: #f3ebdf;
border-radius: 2px;
position: relative;
top: -20px;
}
</style>
</head>
<body>
<div class="container">
<h1>URL Shortener</h1>
<div class="sub-text">A simple URL shortener written in Go</div>
<div class="box">
<p>Enter or paste your long URL in the box below.</p>
<form action="/" method="post">
<input type="text" name="url" placeholder="http://kiwi.io">
<input type="submit" value="Shorten URL">
</form>
</div>
<div class="footer">
Created by Earl Balai<br />
Powered by the <a href="http://golang.org">Go</a> language
</div>
</div>
</body>
</html>
`
func RegisterRoutes() {
router := mux.NewRouter()
router.HandleFunc("/", mainView)
router.HandleFunc("/ext", extView).Methods("POST")
router.HandleFunc("/l/{code}", navigate)
http.Handle("/", router)
}
func WriteContent(w http.ResponseWriter, s string) {
fmt.Fprintf(w, s)
}
func mainView(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
switch r.Method {
case "GET":
WriteContent(w, indexPage)
case "POST":
createShortlink(w, r.FormValue("url"), false)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func createShortlink(w http.ResponseWriter, url string, extension bool) {
w.Header().Set("Content-Type", "text/html")
if !strings.Contains(url, ".") {
http.Error(w, "Invalid url...", http.StatusForbidden)
return
}
db := OpenDB()
var count int64
row := db.QueryRow("SELECT COUNT(*) FROM storage")
err := row.Scan(&count)
if err != nil {
log.Fatal(err)
}
count++
short := strconv.FormatInt(count, 36)
var db_code string
q := db.QueryRow("SELECT code FROM storage WHERE url = $1", url)
e := q.Scan(&db_code)
if e != nil {
db.Exec("INSERT INTO storage(id, url, code) VALUES($1, $2, $3)", count, url, short)
fmt.Printf("New URL stored to DB. URL: %s with CODE: %s\n", url, short)
} else {
short = db_code
fmt.Printf("URL already exists. Code: %s\n", db_code)
}
defer db.Close()
short_url := domain + short
result := `
<!DOCTYPE html>
<html>
<head>
<title>URL Shortener</title>
<meta charset="UTF-8">
<style type="text/css">
body {
color: #555;
}
.container {
width: 960px;
margin: 20px auto;
text-align: center;
}
.box {
width: 500px;
background-color: #f9f3ec;
padding: 20px;
border-radius: 2px;
margin: 0 auto;
}
.box a {
color: #5cbaf2;
font-size: 1.3em;
font-weight: bold;
text-decoration: none;
}
.footer {
margin-top: 15px;
font-size: 0.85em;
}
.footer a{
color: #555;
text-decoration: none;
font-weight: bold;
}
.footer a:hover {
color: #5cbaf2;
}
</style>
</head>
<body>
<div class="container">
<h1>URL Shortener</h1>
<p>Your short URL has been generated!</p>
<div class="box">
<a href="` + short_url + `" id="url">` + short_url + `</a>
</div>
<div class="footer">
Created by Earl Balai<br />
Powered by the <a href="http://golang.org">Go</a> language
</div>
</div>
</body>
</html>
`
if extension {
WriteContent(w, short_url)
} else {
WriteContent(w, result)
}
}
func navigate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["code"]
//Check db for ID
db := OpenDB()
var url string
row := db.QueryRow("SELECT url FROM storage WHERE code = $1", id)
err := row.Scan(&url)
if err != nil {
http.Error(w, "404 url not found", http.StatusNotFound)
}
defer db.Close()
if !strings.Contains(url, "http://") {
url = "http://" + url
}
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
//Extension
func extView(w http.ResponseWriter, r *http.Request) {
createShortlink(w, r.FormValue("url"), true)
}