mirror of
https://github.com/vlang/v.git
synced 2025-09-15 15:32:27 +03:00
examples: add vanilla_http_server
- a fast, multi-threaded, non-blocking, port and host reuse, thread-safe, epoll server (#23094)
This commit is contained in:
parent
6623ac21c7
commit
f787e0317e
11 changed files with 576 additions and 0 deletions
42
examples/vanilla_http_server/src/controllers.v
Normal file
42
examples/vanilla_http_server/src/controllers.v
Normal file
|
@ -0,0 +1,42 @@
|
|||
module main
|
||||
|
||||
import strings
|
||||
|
||||
const http_ok_response = 'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n'.bytes()
|
||||
|
||||
const http_created_response = 'HTTP/1.1 201 Created\r\nContent-Type: application/json\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n'.bytes()
|
||||
|
||||
fn home_controller(params []string) ![]u8 {
|
||||
return http_ok_response
|
||||
}
|
||||
|
||||
fn get_users_controller(params []string) ![]u8 {
|
||||
return http_ok_response
|
||||
}
|
||||
|
||||
@[direct_array_access; manualfree]
|
||||
fn get_user_controller(params []string) ![]u8 {
|
||||
if params.len == 0 {
|
||||
return tiny_bad_request_response
|
||||
}
|
||||
id := params[0]
|
||||
response_body := id
|
||||
|
||||
mut sb := strings.new_builder(200)
|
||||
sb.write_string('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ')
|
||||
sb.write_string(response_body.len.str())
|
||||
sb.write_string('\r\nConnection: keep-alive\r\n\r\n')
|
||||
sb.write_string(response_body)
|
||||
|
||||
defer {
|
||||
unsafe {
|
||||
response_body.free()
|
||||
params.free()
|
||||
}
|
||||
}
|
||||
return sb
|
||||
}
|
||||
|
||||
fn create_user_controller(params []string) ![]u8 {
|
||||
return http_created_response
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue