2021-08-24 18:21:24 +02:00
|
|
|
module main
|
|
|
|
|
|
|
|
import net.http { CommonHeader, Request, Response, Server }
|
|
|
|
|
|
|
|
struct ExampleHandler {}
|
|
|
|
|
|
|
|
fn (h ExampleHandler) handle(req Request) Response {
|
|
|
|
mut res := Response{
|
|
|
|
header: http.new_header_from_map({
|
|
|
|
CommonHeader.content_type: 'text/plain'
|
|
|
|
})
|
|
|
|
}
|
2021-08-31 11:13:58 +02:00
|
|
|
mut status_code := 200
|
2022-05-29 19:27:18 +02:00
|
|
|
res.body = match req.url {
|
2021-08-31 11:13:58 +02:00
|
|
|
'/foo' {
|
|
|
|
'bar\n'
|
|
|
|
}
|
|
|
|
'/hello' {
|
|
|
|
'world\n'
|
|
|
|
}
|
|
|
|
'/' {
|
|
|
|
'foo\nhello\n'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status_code = 404
|
|
|
|
'Not found\n'
|
|
|
|
}
|
2021-08-24 18:21:24 +02:00
|
|
|
}
|
2021-08-31 11:13:58 +02:00
|
|
|
res.status_code = status_code
|
2021-08-24 18:21:24 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
mut server := Server{
|
|
|
|
handler: ExampleHandler{}
|
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
server.listen_and_serve()?
|
2021-08-24 18:21:24 +02:00
|
|
|
}
|