| 1 | module main |
| 2 | |
| 3 | import net.http { 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 | .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 | |
| 30 | res.status_code = status_code |
| 31 | return res |
| 32 | } |
| 33 | |
| 34 | fn main() { |
| 35 | mut server := Server{ |
| 36 | handler: ExampleHandler{} |
| 37 | } |
| 38 | server.listen_and_serve() |
| 39 | } |
| 40 | |