v / examples / fasthttp / controllers.v
27 lines · 23 sloc · 882 bytes · 645e94ea994bc2e5d689092ae5aa65eb99d4511d
Raw
1module main
2
3fn home_controller() ![]u8 {
4 response := 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!'
5 return response.bytes()
6}
7
8fn get_user_controller(id string) ![]u8 {
9 body := 'User ID: ${id}'
10 content_length := body.len
11 response := 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
12 return response.bytes()
13}
14
15fn create_user_controller() ![]u8 {
16 body := 'User created successfully'
17 content_length := body.len
18 response := 'HTTP/1.1 201 Created\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
19 return response.bytes()
20}
21
22fn not_found_response() ![]u8 {
23 body := '404 Not Found'
24 content_length := body.len
25 response := 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
26 return response.bytes()
27}
28