2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2020-06-29 17:17:56 +08:00
|
|
|
|
#include "master_service.h"
|
2020-06-29 14:34:16 +08:00
|
|
|
|
#include "http_service.h"
|
|
|
|
|
|
2020-09-07 14:09:24 +08:00
|
|
|
|
#if __cplusplus >= 201103L // Support c++11 ?
|
|
|
|
|
# define USE_LAMBDA
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef USE_LAMBDA
|
|
|
|
|
static bool http_get_default(const char* path, HttpRequest&, HttpResponse& res)
|
2020-06-29 14:34:16 +08:00
|
|
|
|
{
|
2020-09-07 14:09:24 +08:00
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("%s: hello world!\r\n", path);
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static bool http_get_root(HttpRequest&, HttpResponse& res)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf("hello world!\r\n");
|
2020-06-29 14:34:16 +08:00
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool http_get_ok(HttpRequest&, HttpResponse& res)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf("ok: hello world!\r\n");
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool http_post_ok(HttpRequest& req, HttpResponse& res)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf;
|
|
|
|
|
if (!req.getBody(buf)) {
|
|
|
|
|
printf("getBody error!\r\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("body: %s\r\n", buf.c_str());
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool http_get_json(HttpRequest&, HttpResponse& res)
|
|
|
|
|
{
|
|
|
|
|
acl::json json;
|
|
|
|
|
acl::json_node& root = json.get_root();
|
|
|
|
|
root.add_number("code", 200)
|
|
|
|
|
.add_text("status", "+ok")
|
|
|
|
|
.add_child("data",
|
|
|
|
|
json.create_node()
|
|
|
|
|
.add_text("name", "value")
|
|
|
|
|
.add_bool("success", true)
|
|
|
|
|
.add_number("number", 100));
|
|
|
|
|
return res.write(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool http_get_xml(HttpRequest&, HttpResponse& res)
|
|
|
|
|
{
|
|
|
|
|
acl::xml1 xml;
|
|
|
|
|
acl::xml_node& root = xml.get_root();
|
|
|
|
|
root.add_child("test6", true, "text6")
|
|
|
|
|
.add_child("test61", true, "text61")
|
|
|
|
|
.add_attr("name61_1", "value61_1")
|
|
|
|
|
.add_attr("name61_2", "value61_2")
|
|
|
|
|
.add_attr("name61_3", "value61_3")
|
|
|
|
|
.add_child("test611", true, "text611")
|
|
|
|
|
.add_attr("name611_1", "value611_1")
|
|
|
|
|
.add_attr("name611_2", "value611_2")
|
|
|
|
|
.add_attr("name611_3", "value611_3")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("test62", true, "text62")
|
|
|
|
|
.add_attr("name62_1", "value62_1")
|
|
|
|
|
.add_attr("name62_2", "value62_2")
|
|
|
|
|
.add_attr("name62_3", "value62_3")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_attr("name6_1", "value6_1")
|
|
|
|
|
.add_attr("name6_2", "value6_2")
|
|
|
|
|
.add_attr("name6_3", "value6_3");
|
|
|
|
|
|
|
|
|
|
return res.write(xml);
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC> acl <20><>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
master_service ms;
|
|
|
|
|
http_service& service = ms.get_service();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
|
2020-06-29 17:17:56 +08:00
|
|
|
|
ms.set_cfg_int(var_conf_int_tab)
|
2020-06-29 14:34:16 +08:00
|
|
|
|
.set_cfg_int64(var_conf_int64_tab)
|
|
|
|
|
.set_cfg_str(var_conf_str_tab)
|
|
|
|
|
.set_cfg_bool(var_conf_bool_tab);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
|
// Register http handlers according different url path
|
2020-09-07 14:09:24 +08:00
|
|
|
|
service.Get("/", http_get_root)
|
2020-06-29 14:34:16 +08:00
|
|
|
|
.Get("/ok", http_get_ok)
|
|
|
|
|
.Post("/ok", http_post_ok)
|
|
|
|
|
.Get("/json", http_get_json)
|
2020-06-30 15:55:52 +08:00
|
|
|
|
.Get("/xml", http_get_xml)
|
2020-09-07 14:09:24 +08:00
|
|
|
|
#ifdef USE_LAMBDA
|
2020-06-30 15:55:52 +08:00
|
|
|
|
.Get("/test1", [](HttpRequest&, HttpResponse& res) {
|
|
|
|
|
acl::string buf("test1: hello world!\r\n");
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
2021-01-17 18:57:37 +08:00
|
|
|
|
}).Get("/test2", [&](HttpRequest&, HttpResponse& res) {
|
2020-06-30 15:55:52 +08:00
|
|
|
|
acl::string buf("test2: hello world!\r\n");
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
2021-01-17 18:57:37 +08:00
|
|
|
|
}).Default([](const char* path, HttpRequest&, HttpResponse& res) {
|
2020-09-07 14:09:24 +08:00
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("Default(%s): hello world!\r\n", path);
|
2020-08-21 14:51:09 +08:00
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf);
|
2020-06-30 15:55:52 +08:00
|
|
|
|
});
|
|
|
|
|
#else
|
2020-08-21 14:51:09 +08:00
|
|
|
|
.Default(http_get_default);
|
2020-06-30 15:55:52 +08:00
|
|
|
|
#endif
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
2021-01-17 18:57:37 +08:00
|
|
|
|
if (argc == 1 || (argc >= 2 && strcmp(argv[1], "alone") == 0)) {
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
2015-06-22 17:32:33 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
2022-06-12 20:32:08 +08:00
|
|
|
|
// <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD> acl_master.log <20><>־
|
|
|
|
|
acl::master_log_enable(false);
|
2015-06-22 17:32:33 +08:00
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ip|port1,ip|port2,...
|
2019-07-27 19:32:56 +08:00
|
|
|
|
const char* addrs = "|8888";
|
2015-06-22 17:32:33 +08:00
|
|
|
|
printf("listen on: %s\r\n", addrs);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ø<EFBFBD>ֵ > 0 <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9>̵<EFBFBD>
|
|
|
|
|
// <20>Ự<EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӵӽ<D3B4><D3BD>յ<EFBFBD><D5B5>رճ<D8B1>֮Ϊһ<CEAA><D2BB><EFBFBD>Ự<EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻỰ<D3BB><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|
|
|
|
// Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-06-22 17:32:33 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
|
2019-07-20 16:41:23 +08:00
|
|
|
|
if (argc >= 3) {
|
2020-06-29 17:17:56 +08:00
|
|
|
|
ms.run_alone(addrs, argv[2], count);
|
2019-07-20 16:41:23 +08:00
|
|
|
|
} else {
|
2020-06-29 17:17:56 +08:00
|
|
|
|
ms.run_alone(addrs, NULL, count);
|
2019-07-20 16:41:23 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
printf("Enter any key to exit now\r\n");
|
|
|
|
|
getchar();
|
2019-07-20 16:41:23 +08:00
|
|
|
|
} else {
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
2015-06-22 17:32:33 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ip:port1,ip:port2,...
|
2015-06-22 17:32:33 +08:00
|
|
|
|
const char* addrs = "127.0.0.1:8888";
|
|
|
|
|
printf("listen on: %s\r\n", addrs);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ø<EFBFBD>ֵ > 0 <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӹ<EFBFBD><D3B9>̵<EFBFBD>
|
|
|
|
|
// <20>Ự<EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ӵӽ<D3B4><D3BD>յ<EFBFBD><D5B5>رճ<D8B1>֮Ϊһ<CEAA><D2BB><EFBFBD>Ự<EFBFBD><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻỰ<D3BB><E1BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|
|
|
|
// Ϊ 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-06-22 17:32:33 +08:00
|
|
|
|
int count = 0;
|
|
|
|
|
|
2020-06-29 17:17:56 +08:00
|
|
|
|
ms.run_alone(addrs, NULL, count); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
|
2015-06-22 17:32:33 +08:00
|
|
|
|
printf("Enter any key to exit now\r\n");
|
|
|
|
|
getchar();
|
|
|
|
|
|
|
|
|
|
#else
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// acl_master <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
2020-06-29 17:17:56 +08:00
|
|
|
|
ms.run_daemon(argc, argv);
|
2015-06-22 17:32:33 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|