forked from the-benchmarker/web-frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.v
More file actions
37 lines (30 loc) · 925 Bytes
/
Copy pathmain.v
File metadata and controls
37 lines (30 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module main
import vanilla.http_server
import vanilla.http_server.http1_1.response
import vanilla.http_server.http1_1.request_parser
fn handle_request(req_buffer []u8, client_conn_fd int) ![]u8 {
req := request_parser.decode_http_request(req_buffer)!
method := unsafe { tos(&req.buffer[req.method.start], req.method.len) }
path := unsafe { tos(&req.buffer[req.path.start], req.path.len) }
if method == 'GET' {
if path == '/' {
return home_controller([])
} else if path.starts_with('/user/') {
id := path[6..]
return get_user_controller([id])
}
} else if method == 'POST' {
if path == '/user' {
return create_user_controller([])
}
}
return response.tiny_bad_request_response
}
fn main() {
mut server := http_server.new_server(http_server.ServerConfig{
port: 3000
request_handler: handle_request
io_multiplexing: unsafe { http_server.IOBackend.epoll }
})!
server.run()
}