2021-08-24 19:21:24 +03: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 12:13:58 +03:00
|
|
|
mut status_code := 200
|
2021-08-24 19:21:24 +03:00
|
|
|
res.text = match req.url {
|
2021-08-31 12:13:58 +03:00
|
|
|
'/foo' {
|
|
|
|
'bar\n'
|
|
|
|
}
|
|
|
|
'/hello' {
|
|
|
|
'world\n'
|
|
|
|
}
|
|
|
|
'/' {
|
|
|
|
'foo\nhello\n'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status_code = 404
|
|
|
|
'Not found\n'
|
|
|
|
}
|
2021-08-24 19:21:24 +03:00
|
|
|
}
|
2021-08-31 12:13:58 +03:00
|
|
|
res.status_code = status_code
|
2021-08-24 19:21:24 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
mut server := Server{
|
|
|
|
handler: ExampleHandler{}
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
server.listen_and_serve()?
|
2021-08-24 19:21:24 +03:00
|
|
|
}
|