Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,71 @@
|
||||
# Fasthttp Example
|
||||
|
||||
A simple HTTP server example using the `fasthttp` module from `vlib/fasthttp`.
|
||||
|
||||
## Features
|
||||
|
||||
- Handles GET and POST requests
|
||||
- Routes requests to different controllers based on HTTP method and path
|
||||
- Returns appropriate HTTP responses with status codes and content
|
||||
|
||||
## Building
|
||||
|
||||
```sh
|
||||
./v examples/fasthttp
|
||||
```
|
||||
|
||||
## Running
|
||||
|
||||
```sh
|
||||
./examples/fasthttp/fasthttp
|
||||
```
|
||||
|
||||
The server will listen on `http://localhost:3000`
|
||||
|
||||
## Testing
|
||||
|
||||
### Home endpoint
|
||||
|
||||
```sh
|
||||
curl http://localhost:3000/
|
||||
```
|
||||
|
||||
### Get user by ID
|
||||
|
||||
```sh
|
||||
curl http://localhost:3000/user/123
|
||||
```
|
||||
|
||||
### Create user
|
||||
|
||||
```sh
|
||||
curl -X POST http://localhost:3000/user
|
||||
```
|
||||
|
||||
### 404 response
|
||||
|
||||
```sh
|
||||
curl http://localhost:3000/notfound
|
||||
```
|
||||
|
||||
## File Structure
|
||||
|
||||
- `main.v` - Entry point and request router
|
||||
- `controllers.v` - Request handlers for different routes
|
||||
- `v.mod` - Module metadata
|
||||
|
||||
## Architecture
|
||||
|
||||
The example demonstrates:
|
||||
|
||||
1. **Request Routing**: The `handle_request()` function routes incoming HTTP requests based on
|
||||
method and path
|
||||
2. **Response Handling**: Controllers return HTTP responses with proper headers and status codes
|
||||
3. **Content Type**: All responses are returned as `[]u8` (byte arrays)
|
||||
|
||||
The fasthttp module handles:
|
||||
|
||||
- Low-level socket management
|
||||
- Request parsing
|
||||
- Connection handling
|
||||
- Non-blocking I/O with epoll (Linux) or kqueue (macOS)
|
||||
@@ -0,0 +1,27 @@
|
||||
module main
|
||||
|
||||
fn home_controller() ![]u8 {
|
||||
response := 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, World!'
|
||||
return response.bytes()
|
||||
}
|
||||
|
||||
fn get_user_controller(id string) ![]u8 {
|
||||
body := 'User ID: ${id}'
|
||||
content_length := body.len
|
||||
response := 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
|
||||
return response.bytes()
|
||||
}
|
||||
|
||||
fn create_user_controller() ![]u8 {
|
||||
body := 'User created successfully'
|
||||
content_length := body.len
|
||||
response := 'HTTP/1.1 201 Created\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
|
||||
return response.bytes()
|
||||
}
|
||||
|
||||
fn not_found_response() ![]u8 {
|
||||
body := '404 Not Found'
|
||||
content_length := body.len
|
||||
response := 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: ${content_length}\r\n\r\n${body}'
|
||||
return response.bytes()
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
module main
|
||||
|
||||
import fasthttp
|
||||
|
||||
fn handle_request(req fasthttp.HttpRequest) !fasthttp.HttpResponse {
|
||||
method := req.buffer[req.method.start..req.method.start + req.method.len].bytestr()
|
||||
path := req.buffer[req.path.start..req.path.start + req.path.len].bytestr()
|
||||
|
||||
if method == 'GET' {
|
||||
if path == '/' {
|
||||
return fasthttp.HttpResponse{
|
||||
content: home_controller()!
|
||||
}
|
||||
} else if path.starts_with('/user/') {
|
||||
id := path[6..]
|
||||
return fasthttp.HttpResponse{
|
||||
content: get_user_controller(id)!
|
||||
}
|
||||
}
|
||||
} else if method == 'POST' {
|
||||
if path == '/user' {
|
||||
return fasthttp.HttpResponse{
|
||||
content: create_user_controller()!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fasthttp.HttpResponse{
|
||||
content: not_found_response()!
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut server := fasthttp.new_server(fasthttp.ServerConfig{
|
||||
port: 3000
|
||||
handler: handle_request
|
||||
}) or {
|
||||
eprintln('Failed to create server: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
println('Starting fasthttp server on port http://localhost:3000...')
|
||||
|
||||
server.run() or { eprintln('error: ${err}') }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
Module {
|
||||
name: 'fasthttp_example'
|
||||
description: 'A simple HTTP server example using the fasthttp module'
|
||||
version: '0.0.1'
|
||||
license: 'MIT'
|
||||
}
|
||||
Reference in new issue
Block a user