Rest server (continue)

This commit is contained in:
fasiondog 2021-02-28 01:05:03 +08:00
parent bbcde7aedb
commit 90989e1479
6 changed files with 183 additions and 1 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-02-28
* Author: fasiondog
*/
#pragma once
#include <hikyuu/Log.h>
#include <nng/nng.h>
#include <nng/supplemental/http/http.h>
namespace hku {
#define HTTP_FATAL_CHECK(rv, msg) \
{ \
if (rv != 0) { \
HKU_FATAL("[HTTP_FATAL] {} err: {}", msg, nng_strerror(rv)); \
nng_fini(); \
exit(0); \
} \
}
#define HTTP_ERROR_CHECK(rv, msg) \
{ \
if (rv != 0) { \
HKU_ERROR("[HTTP_FATAL] {} err: {}", msg, nng_strerror(rv)); \
} \
}
} // namespace hku

View File

@ -0,0 +1,30 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-02-28
* Author: fasiondog
*/
#include "HttpServer.h"
namespace hku {
HttpServer::HttpServer(const char* host, uint16_t port)
: m_host(host), m_port(port), m_server(nullptr) {
m_root_url = fmt::format("{}:{}", m_host, m_port);
nng_url* url{nullptr};
HTTP_FATAL_CHECK(nng_url_parse(&url, m_root_url.c_str()), "Failed nng_url_parse!");
HTTP_FATAL_CHECK(nng_http_server_hold(&m_server, url), "Failed nng_http_server_hold!");
nng_url_free(url);
}
HttpServer::~HttpServer() {
HKU_INFO("Quit http server({})", m_root_url);
nng_http_server_release(m_server);
}
void HttpServer::start() {
HTTP_FATAL_CHECK(nng_http_server_start(m_server), "Failed nng_http_server_start!");
}
} // namespace hku

View File

@ -0,0 +1,30 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-02-28
* Author: fasiondog
*/
#pragma once
#include <string>
#include "HttpMicro.h"
namespace hku {
class HttpServer {
public:
HttpServer(const char *host, uint16_t port);
virtual ~HttpServer();
void start();
private:
std::string m_host;
uint16_t m_port;
std::string m_root_url;
nng_http_server *m_server = nullptr;
};
} // namespace hku

View File

@ -0,0 +1,30 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-02-26
* Author: fasiondog
*/
#include <hikyuu/hikyuu.h>
#if defined(_WIN32)
#include <Windows.h>
#endif
#include "http/HttpServer.h"
using namespace hku;
int main(int argc, char* argv[]) {
#if defined(_WIN32)
// Windows 下设置控制台程序输出代码页为 UTF8
auto old_cp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
#endif
HttpServer server("http://*", 8080);
server.start();
SetConsoleOutputCP(old_cp);
return 0;
}

View File

@ -0,0 +1,59 @@
target("hikyuu_server")
set_kind("binary")
add_packages("fmt", "spdlog", "flatbuffers", "nng")
add_deps("hikyuu")
add_includedirs("..")
if is_plat("windows") then
add_cxflags("-wd4819")
add_cxflags("-wd4251") --template dll export warning
add_cxflags("-wd4267")
add_cxflags("-wd4834") --C++17 discarding return value of function with 'nodiscard' attribute
add_cxflags("-wd4996")
add_cxflags("-wd4244") --discable double to int
add_cxflags("-wd4566")
else
add_rpathdirs("$ORIGIN")
add_cxflags("-Wno-sign-compare", "-Wno-missing-braces")
end
if is_plat("windows") then
add_defines("SQLITE_API=__declspec(dllimport)")
add_defines("HKU_API=__declspec(dllimport)")
add_includedirs("../../hikyuu_extern_libs/src/sqlite3")
add_deps("sqlite3")
add_packages("mysql")
end
if is_plat("linux") then
if is_arch("x86_64") then
if os.exists("/usr/lib64/mysql") then
add_linkdirs("/usr/lib64/mysql")
end
add_linkdirs("/usr/lib/x86_64-linux-gnu")
end
end
if is_plat("macosx") then
--add_linkdirs("/usr/local/opt/libiconv/lib")
add_links("iconv")
add_includedirs("/usr/local/opt/mysql-client/include")
add_linkdirs("/usr/local/opt/mysql-client/lib")
end
if is_plat("windows") then
-- nng 静态链接需要的系统库
add_syslinks("ws2_32", "advapi32")
end
if is_plat("linux") or is_plat("macosx") then
add_links("sqlite3")
add_links("mysqlclient")
end
-- add files
add_files("./**.cpp")
target_end()

View File

@ -4,7 +4,7 @@ set_xmakever("2.2.5")
set_project("hikyuu")
-- version
set_version("1.1.7", {build="%Y%m%d%H%M"})
set_version("1.1.8", {build="%Y%m%d%H%M"})
set_configvar("LOG_ACTIVE_LEVEL", 0) -- 激活的日志级别
--if is_mode("debug") then
-- set_configvar("LOG_ACTIVE_LEVEL", 0) -- 激活的日志级别
@ -108,6 +108,7 @@ add_subdirs("./hikyuu_cpp/hikyuu")
add_subdirs("./hikyuu_pywrap")
add_subdirs("./hikyuu_cpp/unit_test")
add_subdirs("./hikyuu_cpp/demo")
add_subdirs("./hikyuu_cpp/hikyuu_server")
before_install("scripts.before_install")
on_install("scripts.on_install")