|
| 1 | +# fasthttp |
| 2 | + |
| 3 | +The `fasthttp` module is a high-performance HTTP server library for V that provides low-level socket management and non-blocking I/O. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **High Performance**: Uses platform-specific I/O multiplexing: |
| 8 | + - `epoll` on Linux for efficient connection handling |
| 9 | + - `kqueue` on macOS for high-performance event notification |
| 10 | +- **Non-blocking I/O**: Handles multiple concurrent connections efficiently |
| 11 | +- **Simple API**: Easy-to-use request handler pattern |
| 12 | +- **Cross-platform**: Supports Linux and macOS |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +The module is part of the standard V library. Import it in your V code: |
| 17 | + |
| 18 | +```v |
| 19 | +import fasthttp |
| 20 | +``` |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +Here's a minimal HTTP server example: |
| 25 | + |
| 26 | +```v |
| 27 | +import fasthttp |
| 28 | +
|
| 29 | +fn handle_request(req fasthttp.HttpRequest) ![]u8 { |
| 30 | + path := req.buffer[req.path.start..req.path.start + req.path.len].bytestr() |
| 31 | +
|
| 32 | + if path == '/' { |
| 33 | + return 'Hello, World!'.bytes() |
| 34 | + } |
| 35 | +
|
| 36 | + return '404 Not Found'.bytes() |
| 37 | +} |
| 38 | +
|
| 39 | +fn main() { |
| 40 | + mut server := fasthttp.new_server(fasthttp.ServerConfig{ |
| 41 | + port: 3000 |
| 42 | + handler: handle_request |
| 43 | + }) or { |
| 44 | + eprintln('Failed to create server: ${err}') |
| 45 | + return |
| 46 | + } |
| 47 | +
|
| 48 | + println('Server listening on http://localhost:3000') |
| 49 | + server.run() or { eprintln('error: ${err}') } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## API Reference |
| 54 | + |
| 55 | +### `HttpRequest` Struct |
| 56 | + |
| 57 | +Represents an incoming HTTP request. |
| 58 | + |
| 59 | +**Fields:** |
| 60 | + |
| 61 | +- `buffer: []u8` - The raw request buffer containing the complete HTTP request |
| 62 | +- `method: Slice` - The HTTP method (GET, POST, etc.) |
| 63 | +- `path: Slice` - The request path |
| 64 | +- `version: Slice` - The HTTP version (e.g., "HTTP/1.1") |
| 65 | +- `client_conn_fd: int` - Internal socket file descriptor |
| 66 | + |
| 67 | +### `Slice` Struct |
| 68 | + |
| 69 | +Represents a slice of the request buffer. |
| 70 | + |
| 71 | +**Fields:** |
| 72 | + |
| 73 | +- `start: int` - Starting index in the buffer |
| 74 | +- `len: int` - Length of the slice |
| 75 | + |
| 76 | +**Usage:** |
| 77 | + |
| 78 | +```v ignore |
| 79 | +method := req.buffer[req.method.start..req.method.start + req.method.len].bytestr() |
| 80 | +path := req.buffer[req.path.start..req.path.start + req.path.len].bytestr() |
| 81 | +``` |
| 82 | + |
| 83 | +## Request Handler Pattern |
| 84 | + |
| 85 | +The handler function receives an `HttpRequest` and must return either: |
| 86 | + |
| 87 | +- `[]u8` - A byte array containing the HTTP response body |
| 88 | +- An error if processing failed |
| 89 | + |
| 90 | +The handler should extract method and path information from the request and route accordingly. |
| 91 | + |
| 92 | +**Example:** |
| 93 | + |
| 94 | +```v ignore |
| 95 | +fn my_handler(req fasthttp.HttpRequest) ![]u8 { |
| 96 | + method := req.buffer[req.method.start..req.method.start + req.method.len].bytestr() |
| 97 | + path := req.buffer[req.path.start..req.path.start + req.path.len].bytestr() |
| 98 | +
|
| 99 | + match method { |
| 100 | + 'GET' { |
| 101 | + if path == '/' { |
| 102 | + return 'Home page'.bytes() |
| 103 | + } |
| 104 | + } |
| 105 | + 'POST' { |
| 106 | + if path == '/api/data' { |
| 107 | + return 'Data received'.bytes() |
| 108 | + } |
| 109 | + } |
| 110 | + else {} |
| 111 | + } |
| 112 | +
|
| 113 | + return '404 Not Found'.bytes() |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Response Format |
| 118 | + |
| 119 | +Responses should be returned as byte arrays. |
| 120 | +The server will send them directly to the client as HTTP response bodies. |
| 121 | + |
| 122 | +```v ignore |
| 123 | +// Simple text response |
| 124 | +return 'Hello, World!'.bytes() |
| 125 | +
|
| 126 | +// HTML response |
| 127 | +return '<html><body>Hello</body></html>'.bytes() |
| 128 | +
|
| 129 | +// JSON response |
| 130 | +return '{"message": "success"}'.bytes() |
| 131 | +``` |
| 132 | + |
| 133 | +## Example |
| 134 | + |
| 135 | +See the complete example in `examples/fasthttp/` for a more |
| 136 | +detailed server implementation with multiple routes and controllers. |
| 137 | + |
| 138 | +```sh |
| 139 | +./v examples/fasthttp |
| 140 | +./examples/fasthttp/fasthttp |
| 141 | +``` |
| 142 | + |
| 143 | +## Platform Support |
| 144 | + |
| 145 | +- **Linux**: Uses `epoll` for high-performance I/O multiplexing |
| 146 | +- **macOS**: Uses `kqueue` for event notification |
| 147 | +- **Windows**: Currently not supported |
| 148 | + |
| 149 | +## Performance Considerations |
| 150 | + |
| 151 | +- The `fasthttp` module is designed for high throughput and low latency |
| 152 | +- Handler functions should be efficient; blocking operations will affect other connections |
| 153 | +- Use goroutines within handlers if you need to perform long-running operations without |
| 154 | + blocking the I/O loop |
| 155 | + |
| 156 | +## Notes |
| 157 | + |
| 158 | +- HTTP headers are currently not parsed; the entire request is available in the buffer |
| 159 | +- Only the request method, path, and version are parsed automatically |
| 160 | +- Response status codes and headers must be manually constructed if needed |
| 161 | +- The module provides low-level access for maximum control and performance |
0 commit comments