Code Monkey home page Code Monkey logo

evhttpserver's Introduction

EVHttpServer logo

Introduction

build and test badge badge

EVHttpServer is just an http server implemented by encapsulating libevent using c++, It provides:

  • Simpler api

  • Use thread pool to handle http requests

  • Support regular matching path

Examples

See examples here.

Here is a simple example of using EVHttpServer:

#include "EVHttpServer.h"
#include <iostream>
#include <thread>
#include <signal.h>
static volatile bool g_runFlag = true;

void sighandler(int signum)
{
    g_runFlag = false;
}

void func(const EVHttpServer::HttpReq & req, EVHttpServer::HttpRes & res, void * arg)
{
    std::cout << req.methodStr() << " " << req.path() << std::endl;
    std::cout << req.body() << std::endl;

    res.setBody(R"({"status":"OK"})");
    res.setCode(200);
}

int main(int argc, const char *argv[])
{
    EVHttpServer server;

    server.addHandler({EVHttpServer::REQ_POST, "/api/fun"}, func);
    server.init(9999);
    server.start();

    signal(SIGINT, sighandler); 

    while(g_runFlag)
    {
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    return 0;
}

API documentation

Click here to jump to the api documentation generated by doxygen.

Use in your project

The first way is to include the source code in the src directory into your project, and then give libevent's header file path, library path and rpath when compiling.

The second way is to use EVHttpServer compiled as a library.

Build

  1. Clone the repository
git clone https://github.com/quanqixian/EVHttpServer.git
  1. Generate the necessary build files

    In this step, the third-party library will be cloned.

cd EVHttpServer
cmake -B build -S . -DCMAKE_INSTALL_PREFIX=/path/to/install -DCMAKE_BUILD_TYPE=Release
  1. Compile the source code. In this step, third-party libraries, EVHttpServer library, samples, tests will be compiled.
cmake --build build
  1. Install to system
cmake --install build

Now you can use the EVHttpServer library, include the header file "EVHttpServer.h" in the code, link the libevent library and the EVHttpServer library when compiling.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.