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 pathpaste.v
96 lines (79 loc) · 2.58 KB
/
paste.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
96
import vweb
import json
import vredis
import encoding.base64 as bsf
import compress.zlib
struct PostPaste {
text string
expire int
}
/*
{
"text":"lorem\nipsome",
"expire":300
}
*/
[post;'/api/p/']
fn (mut app App) set_paste() vweb.Result {
println("got POST for text")
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
paste := json.decode(PostPaste,app.req.data) or {
app.set_status(418,"")
return app.text("Cannot parse JSON!")
} //? cannot parse
if paste.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
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()
data := bsf.encode(zlib.compress(paste.text.bytes()) or {
app.set_status(500,"")
return app.text("Internal server error, contact me!")
})
if !redis.set("p:"+pointer, data) {
app.set_status(500,"")
eprintln("REDIS ERROR - COULD NOT SET DATA - POST ")
return app.text("Internal server error, contact me!")
} //? cannot set data
if paste.expire != -1 {
redis.expire("p:"+pointer,paste.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/p/$pointer')
//? s.l-m.dev/p/$pointer
}
[get;"/p/:id"]
fn (mut app App) get_paste(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)
data := bsf.decode(redis.get("p:"+id) or {
return app.not_found()
})
final := zlib.decompress(data) or {
app.set_status(500,"")
return app.text("Internal server error, contact me!")
}
return app.text(final.bytestr())
}