v / examples / fasthttp / main.v
45 lines · 38 sloc · 1021 bytes · 8f72ea1a8fb2f83dc990c3c4f51c6584eca0cf8f
Raw
1module main
2
3import fasthttp
4
5fn handle_request(req fasthttp.HttpRequest) !fasthttp.HttpResponse {
6 method := req.buffer[req.method.start..req.method.start + req.method.len].bytestr()
7 path := req.buffer[req.path.start..req.path.start + req.path.len].bytestr()
8
9 if method == 'GET' {
10 if path == '/' {
11 return fasthttp.HttpResponse{
12 content: home_controller()!
13 }
14 } else if path.starts_with('/user/') {
15 id := path[6..]
16 return fasthttp.HttpResponse{
17 content: get_user_controller(id)!
18 }
19 }
20 } else if method == 'POST' {
21 if path == '/user' {
22 return fasthttp.HttpResponse{
23 content: create_user_controller()!
24 }
25 }
26 }
27
28 return fasthttp.HttpResponse{
29 content: not_found_response()!
30 }
31}
32
33fn main() {
34 mut server := fasthttp.new_server(fasthttp.ServerConfig{
35 port: 3000
36 handler: handle_request
37 }) or {
38 eprintln('Failed to create server: ${err}')
39 return
40 }
41
42 println('Starting fasthttp server on port http://localhost:3000...')
43
44 server.run() or { eprintln('error: ${err}') }
45}
46