Giter VIP home page Giter VIP logo

http_server_cpp's Introduction

http_server_cpp

  • C++的http服务器,简单好用

执行方式

cd http_server_cpp
make run -j6

案例代码

#include "http_server.hpp"

using namespace std;

// 定义控制器,处理路由对应的请求,MVC模式里边的C部分,
class LogicalController : public Controller{
    SetupController(LogicalController);
 
public: 
    // 轻易请求的映射,这里的名称就是路由的地址
    DefRequestMapping(getReturn);
};

// 定义路由的处理函数,类java spring。这里使用返回值作为response。实际上支持多种,请看具体代码
Json::Value LogicalController::getReturn(const Json::Value& param){

    Json::Value data;
    data["alpha"] = 3.15;
    data["name"] = "张三";
    return success(data);
}

int main(int argc, char** argv) {

    // 创建http服务器,具有32个线程
    auto server = createHttpServer("0.0.0.0:8090", 32);
    if(server == nullptr) return -1;
    
    // 添加控制器对于路由v1
    server->add_controller("/v1", make_shared<LogicalController>());

    // 添加静态文件访问,对于路由/static,绑定到路径./上
    server->add_controller("/static", create_file_access_controller("./"));
    return while_loop();
}

效果

关于我们

http_server_cpp's People

Contributors

shouxieai avatar

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.