acl/app/wizard_demo/httpd_static/main.cpp

150 lines
3.6 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "master_service.h"
#include "http_service.h"
#if __cplusplus >= 201103L // Support c++11 ?
# define USE_LAMBDA
#endif
static bool load_html(const char* path, HttpResponse& res)
{
acl::string path_buf(path);
path_buf.strip("..");
char* end = path_buf.buf_end();
if (end && *(end - 1) == '/') {
path_buf.set_offset(path_buf.size() - 1);
}
const char* slash = strrchr(path_buf, '/');
if (slash) {
slash++;
if (strrchr(slash, '.') == NULL) {
path_buf << "/index.html";
}
}
//printf("%s\n", path_buf.c_str());
acl::string ctype("text/html");
#define EQ !strcasecmp
const char* ext = strrchr(path_buf.c_str(), '.');
if (ext && *++ext) {
if (EQ(ext, "js")) {
ctype = "text/js";
} else if (EQ(ext, "zip")) {
ctype = "application/zip";
} else if (EQ(ext, "css")) {
ctype = "text/css";
} else if (EQ(ext, "png")) {
ctype = "image/png";
} else if (EQ(ext, "jpg")) {
ctype = "image/jpg";
}
}
acl::string filepath;
filepath << var_cfg_html_path << "/" << path_buf;
acl::string buf;
if (!acl::ifstream::load(filepath, buf)) {
logger_error("load %s error %s",
filepath.c_str(), acl::last_serror());
buf.format("404 %s not found\r\n", path);
res.setStatus(404).setContentLength(buf.size());
return res.write(buf);
}
res.setContentType(ctype);
res.setContentLength(buf.size());
return res.write(buf);
}
static bool http_get_default(const char* path, HttpRequest&, HttpResponse& res)
{
return load_html(path, res);
}
static bool http_get_root(HttpRequest&, HttpResponse& res)
{
return load_html("/index.html", res);
}
int main(int argc, char* argv[])
{
// <20><>ʼ<EFBFBD><CABC> acl <20><>
acl::acl_cpp_init();
master_service ms;
http_service& service = ms.get_service();;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
ms.set_cfg_int(var_conf_int_tab)
.set_cfg_int64(var_conf_int64_tab)
.set_cfg_str(var_conf_str_tab)
.set_cfg_bool(var_conf_bool_tab);
// Register http handlers according different url path
service.Get("/", http_get_root)
.Default(http_get_default);
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
if (argc == 1 || (argc >= 2 && strcmp(argv[1], "alone") == 0)) {
// <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>
acl::log::stdout_open(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ip|port1,ip|port2,...
const char* addrs = "|8888";
printf("listen on: %s\r\n", addrs);
// <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>
unsigned int count = 0;
// <20><><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD≯߳<DFB3><CCB8><EFBFBD>
unsigned int max_threads = 100;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
if (argc >= 3) {
ms.run_alone(addrs, argv[2], count, max_threads);
} else {
ms.run_alone(addrs, NULL, count, max_threads);
}
printf("Enter any key to exit now\r\n");
getchar();
} else {
#if defined(_WIN32) || defined(_WIN64)
// <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>
acl::log::stdout_open(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>ip:port1,ip:port2,...
const char* addrs = "127.0.0.1:8888";
printf("listen on: %s\r\n", addrs);
// <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>
unsigned int count = 0;
// <20><><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD≯߳<DFB3><CCB8><EFBFBD>
unsigned int max_threads = 100;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
ms.run_alone(addrs, NULL, count, max_threads);
printf("Enter any key to exit now\r\n");
getchar();
#else
// acl_master <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
ms.run_daemon(argc, argv);
#endif
}
return 0;
}