An open API service indexing awesome lists of open source software.

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++.

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("

  • Method: " + req->method + "
  • URL: " + req->url + "
  • ");
    }
    ```
    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/
    ```