1 | module main |
2 | |
3 | import net.http { CommonHeader, Request, Response, Server } |
4 | |
5 | struct ExampleHandler {} |
6 | |
7 | fn (h ExampleHandler) handle(req Request) Response { |
8 | mut res := Response{ |
9 | header: http.new_header_from_map({ |
10 | CommonHeader.content_type: 'text/plain' |
11 | }) |
12 | } |
13 | mut status_code := 200 |
14 | res.body = match req.url { |
15 | '/foo' { |
16 | 'bar\n' |
17 | } |
18 | '/hello' { |
19 | 'world\n' |
20 | } |
21 | '/' { |
22 | 'foo\nhello\n' |
23 | } |
24 | else { |
25 | status_code = 404 |
26 | 'Not found\n' |
27 | } |
28 | } |
29 | res.status_code = status_code |
30 | return res |
31 | } |
32 | |
33 | fn main() { |
34 | mut server := Server{ |
35 | handler: ExampleHandler{} |
36 | } |
37 | server.listen_and_serve() |
38 | } |