|
| 1 | +# Vanilla |
| 2 | + |
| 3 | +Vanilla is a raw V server. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +This project is a simple server written in the V programming language. |
| 8 | +It aims to provide a minimalistic and efficient server implementation. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Lightweight and fast |
| 13 | +- Minimal dependencies |
| 14 | +- Easy to understand and extend |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +To install Vanilla, you need to have the V compiler installed. |
| 19 | +You can download it from the [official V website](https://vlang.io). |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +To run the server, use the following command: |
| 24 | + |
| 25 | +```sh |
| 26 | +v -prod crun . |
| 27 | +``` |
| 28 | + |
| 29 | +This will start the server, and you can access it at `http://localhost:3000`. |
| 30 | + |
| 31 | +## Code Overview |
| 32 | + |
| 33 | +### Main Server |
| 34 | + |
| 35 | +The main server logic is implemented in [src/main.v](v/vanilla/src/main.v). |
| 36 | +The server is initialized and started in the `main` function: |
| 37 | + |
| 38 | +```v ignore |
| 39 | +module main |
| 40 | +
|
| 41 | +const port = 3000 |
| 42 | +
|
| 43 | +fn main() { |
| 44 | + mut server := Server{ |
| 45 | + router: setup_router() |
| 46 | + } |
| 47 | +
|
| 48 | + server.socket_fd = create_server_socket(port) |
| 49 | + if server.socket_fd < 0 { |
| 50 | + return |
| 51 | + } |
| 52 | + server.epoll_fd = C.epoll_create1(0) |
| 53 | + if server.epoll_fd < 0 { |
| 54 | + C.perror('epoll_create1 failed'.str) |
| 55 | + C.close(server.socket_fd) |
| 56 | + return |
| 57 | + } |
| 58 | +
|
| 59 | + server.lock_flag.lock() |
| 60 | + if add_fd_to_epoll(server.epoll_fd, server.socket_fd, u32(C.EPOLLIN)) == -1 { |
| 61 | + C.close(server.socket_fd) |
| 62 | + C.close(server.epoll_fd) |
| 63 | +
|
| 64 | + server.lock_flag.unlock() |
| 65 | + return |
| 66 | + } |
| 67 | +
|
| 68 | + server.lock_flag.unlock() |
| 69 | +
|
| 70 | + server.lock_flag.init() |
| 71 | + for i := 0; i < 16; i++ { |
| 72 | + server.threads[i] = spawn process_events(&server) |
| 73 | + } |
| 74 | + println('listening on http://localhost:${port}/') |
| 75 | + event_loop(&server) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Test |
| 80 | + |
| 81 | +### CURL |
| 82 | + |
| 83 | +```sh |
| 84 | +curl -X GET --verbose http://localhost:3000/ && |
| 85 | +curl -X POST --verbose http://localhost:3000/user && |
| 86 | +curl -X GET --verbose http://localhost:3000/user/1 |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +### WRK |
| 91 | + |
| 92 | +```sh |
| 93 | +wrk --connection 512 --threads 16 --duration 10s http://localhost:3000 |
| 94 | +``` |
| 95 | + |
| 96 | +### Valgrind |
| 97 | +```sh |
| 98 | +# Race condition check |
| 99 | +v -prod -gc none . |
| 100 | +valgrind --tool=helgrind ./vanilla_http_server |
| 101 | +``` |
0 commit comments