v2 / examples / pico / pico.v
56 lines · 50 sloc · 1001 bytes · efc8f7fb3ef7e90a088db64ad2c68dd0698b90d5
Raw
1// vtest build: !solaris
2import json
3import picoev
4import picohttpparser
5
6const port = 8089
7
8struct Message {
9 message string
10}
11
12@[inline]
13fn json_response() string {
14 msg := Message{
15 message: 'Hello, World!'
16 }
17 return json.encode(msg)
18}
19
20@[inline]
21fn hello_response() string {
22 return 'Hello, World!'
23}
24
25fn callback(_data voidptr, req picohttpparser.Request, mut res picohttpparser.Response) {
26 if req.method == 'GET' {
27 if req.path == '/t' {
28 res.http_ok()
29 res.header_server()
30 res.header_date()
31 res.plain()
32 res.body(hello_response())
33 } else if req.path == '/j' {
34 res.http_ok()
35 res.header_server()
36 res.header_date()
37 res.json()
38 res.body(json_response())
39 } else {
40 res.http_ok()
41 res.header_server()
42 res.header_date()
43 res.html()
44 res.body('Hello Picoev!\n')
45 }
46 } else {
47 res.http_405()
48 }
49 res.end()
50}
51
52fn main() {
53 println('Starting webserver on http://localhost:${port}/ ...')
54 mut server := picoev.new(port: port, cb: callback)!
55 server.serve()
56}
57