https://github.com/songquanpeng/http-server
Yet another HTTP server implemented in C++.
https://github.com/songquanpeng/http-server
Last synced: 5 months ago
JSON representation
Yet another HTTP server implemented in C++.
- Host: GitHub
- URL: https://github.com/songquanpeng/http-server
- Owner: songquanpeng
- Created: 2022-07-18T11:43:37.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-23T15:08:51.000Z (about 3 years ago)
- Last Synced: 2025-02-26T09:38:39.069Z (7 months ago)
- Language: C++
- Homepage:
- Size: 54.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HTTP Server
> Yet another HTTP server implemented in C++## Running Steps
1. Prepare environments:
```shell
$ uname -r
5.10.16.3-microsoft-standard-WSL2
$ sudo apt install build-essential cmake
$ cmake --version
cmake version 3.22.1
$ gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
```
2. Build executables:
```shell
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
# or
cmake .. -DCMAKE_BUILD_TYPE=Release
make
```
3. Start the server:
```shell
./server
```## Basic Usages
Please refer [main.cpp](./main.cpp) for details.
1. Initialize server & event loop_:
```C++
EventLoop loop_;
HTTPServer server(&loop_, 3000, 4);
```
2. Define controllers:
```C++
void getEchoPage(const HTTPRequestPtr &req, const HTTPResponsePtr &res) {
res->send("
}
```
3. Register routes with controllers:
```C++
server.setRoute("/", getIndexPage);
server.setRoute("/echo", getEchoPage);
server.serveStatic("/public");
```
4. Start serving:
```C++
server.start();
loop_.loop_();
```
## Others
Benchmark:
```shell
wget https://github.com/justsong-lab/WebBench/releases/download/v0.1.0/webbench
chmod u+x webbench
./webbench -c 100 -t 5 http://localhost:3000/
```