This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.v
95 lines (79 loc) · 2.62 KB
/
links.v
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
import vweb
import vredis
import json
import rand
import net.urllib as ulib
fn randthis()string{
set := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$'()*+,-_~"
//? set.len = 81
return rand.string_from_set(set,_idlength)
}
struct PostLink {
url string
expire int
}
/*
{
"url":"https://example.com",
"expire":300
}
*/
[post;'/api/l/']
fn (mut app App) set_link() vweb.Result {
println("got POST for link")
if app.req.header.get(.content_type) or {
app.set_status(400,"")
return app.text("Content type not specified!")
} != "application/json" {
app.set_status(400,"")
return app.text("JSON content type not specified! (application/json)")
} //? bad headers
link := json.decode(PostLink,app.req.data) or {
app.set_status(418,"")
return app.text("Cannot parse JSON!")
} //? cannot parse
if link.expire == 0 {
app.set_status(418,"")
return app.text("Cannot use a string value or zero for expire time!\nUse -1 to never expire.")
} //? edge case on JSON parsing, string numbers get mapped to 0
ulib.parse(link.url) or {
app.set_status(418,"")
return app.text("Incorrect URL format! Must contain https:// or http://")
} //? no sneaky inward URLs
mut redis := vredis.connect(vredis.ConnOpts{}) or {
app.set_status(500,"")
eprintln("REDIS ERROR - COULD NOT CONNECT - POST ")
eprintln(err)
return app.text("Internal server error, contact me!")
} //* connect to redis
pointer := randthis()
if !redis.set("l:"+pointer, link.url) {
app.set_status(500,"")
eprintln("REDIS ERROR - COULD NOT SET DATA - POST ")
return app.text("Internal server error, contact me!")
} //? cannot set data
if link.expire != -1 {
redis.expire("l:"+pointer,link.expire) or {
app.set_status(500,"")
eprintln("REDIS ERROR - COULD NOT SET EXPIRY - POST ")
eprintln(err)
return app.text("Internal server error, contact me!")
} //? cannot set expiry
}
app.set_status(200,"")
return app.text('s.l-m.dev/l/$pointer')
//? s.l-m.dev/l/$pointer
}
[get;"/l/:id"]
fn (mut app App) get_link(id string) vweb.Result {
mut redis := vredis.connect(vredis.ConnOpts{}) or {
app.set_status(500,"")
eprintln("REDIS ERROR - COULD NOT CONNECT - POST ")
return app.text("Internal server error, contact me!")
} //* connect to redis
println("got GET")
println(id)
return app.redirect(redis.get("l:"+id) or {
return app.not_found()
}) //? cannot get data
}