---
title: "simple_server--一个方便c++快速开发接口的http server"
author: "finallygo"
date: "2014-10-27T16:02:42Z"
tags: []
categories: ["开源软件"]
slug: "simple-server-c-http-server"
draft: false
lastmod: "2014-10-29T11:25:23Z"
id: 2148369
url: "https://finallygo.iteye.com/blog/2148369"
---

# simple\_server

此组件是为了使用c++方便快速的构建http server,编写基于http协议json格式的接口,和nginx等传统服务器相比,更加重视开发的便捷性,项目参考[restbed](https://bitbucket.org/Corvusoft/restbed/overview) 实现, 重新实现的原因是restbed只支持g++4.9以上

 

## [](https://github.com/hongliuliao/simple_server#%E7%89%B9%E7%82%B9)特点

- 只支持linux
- 单进程+单线程模型
- g++3.4 以上编译器都支持
- 强调简洁实用

## [](https://github.com/hongliuliao/simple_server#%E4%BE%9D%E8%B5%96)依赖

- [simple\_log](https://github.com/hongliuliao/simple_log) 日志组件
- [jsoncpp](https://github.com/open-source-parsers/jsoncpp) json序列化组件

## [](https://github.com/hongliuliao/simple_server#%E6%80%A7%E8%83%BD)性能

- qps 3000 (vmware虚拟机上ubuntu系统测试结果)

## [](https://github.com/hongliuliao/simple_server#%E6%9E%84%E5%BB%BA--%E6%B5%8B%E8%AF%95)构建 && 测试

```
 make && make test && ./bin/http_server_test 
 curl "localhost:3490/hello"
```

## [](https://github.com/hongliuliao/simple_server#%E5%8A%9F%E8%83%BD%E5%88%97%E8%A1%A8)功能列表

- http 1.0/1.1(keep-alive 支持) GET/POST请求
- 便捷的开发形式
- Json格式的数据返回

## [](https://github.com/hongliuliao/simple_server#%E4%BE%8B%E5%AD%90)例子

```
#include <sstream>
#include <cstdlib>
#include "simple_log.h"
#include "http_server.h"

Response hello(Request &request) {
    Json::Value root;
    root["hello"] = "world";
    return Response(STATUS_OK, root);
}

Response sayhello(Request &request) {
    std::string name = request.get_param("name");
    std::string age = request.get_param("age");

    Json::Value root;
    root["name"] = name;
    root["age"] = atoi(age.c_str());
    return Response(STATUS_OK, root);
}

Response login(Request &request) {
    std::string name = request.get_param("name");
    std::string pwd = request.get_param("pwd");

    LOG_DEBUG("login user which name:%s, pwd:%s", name.c_str(), pwd.c_str());
    Json::Value root;
    root["code"] = 0;
    root["msg"] = "login success!";
    return Response(STATUS_OK, root);
}

int main() {
    HttpServer http_server;

    http_server.add_mapping("/hello", hello);
    http_server.add_mapping("/sayhello", sayhello);
    http_server.add_mapping("/login", login, POST_METHOD);

    http_server.start(3490);
    return 0;
}


```

## [](https://github.com/hongliuliao/simple_server#%E7%BC%96%E8%AF%91)编译

```
g++ -I dependency/simple_log/include/ -I dependency/json-cpp/include/ -I bin/include test/http_server_test.cpp dependency/simple_log/lib/libsimplelog.a dependency/json-cpp/lib/libjson_libmt.a bin/lib/libsimpleserver.a -o bin/http_server_test

```

## [](https://github.com/hongliuliao/simple_server#%E8%BF%90%E8%A1%8C)运行

```
liao@ubuntu:~/workspace/simple_server$ curl "localhost:3490/login" -d "name=tom&pwd=3"
{"code":0,"msg":"login success!"}
```

项目地址:[https://github.com/hongliuliao/simple\_server](https://github.com/hongliuliao/simple_server)