| 1 |
#include "hv.h" |
| 2 |
#include "hssl.h" |
| 3 |
#include "hmain.h" |
| 4 |
#include "iniparser.h" |
| 5 |
|
| 6 |
#include "HttpServer.h" |
| 7 |
#include "hasync.h" // import hv::async |
| 8 |
|
| 9 |
#include "router.h" |
| 10 |
|
| 11 |
hv::HttpServer g_http_server; |
| 12 |
hv::HttpService g_http_service; |
| 13 |
|
| 14 |
static void print_version(); |
| 15 |
static void print_help(); |
| 16 |
|
| 17 |
static int parse_confile(const char* confile); |
| 18 |
|
| 19 |
// long options |
| 20 |
static const option_t long_options[] = { |
| 21 |
{'h', "help", NO_ARGUMENT, "Print this information"}, |
| 22 |
{'v', "version", NO_ARGUMENT, "Print version"}, |
| 23 |
{'c', "confile", REQUIRED_ARGUMENT, "Set configure file, default etc/{program}.conf"}, |
| 24 |
{'t', "test", NO_ARGUMENT, "Test configure file and exit"}, |
| 25 |
{'s', "signal", REQUIRED_ARGUMENT, "send signal to process, signal=[start,stop,restart,status,reload]"}, |
| 26 |
{'d', "daemon", NO_ARGUMENT, "Daemonize"}, |
| 27 |
{'p', "port", REQUIRED_ARGUMENT, "Set listen port"} |
| 28 |
}; |
| 29 |
|
| 30 |
void print_version() { |
| 31 |
printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version()); |
| 32 |
} |
| 33 |
|
| 34 |
void print_help() { |
| 35 |
char detail_options[1024] = {0}; |
| 36 |
dump_opt_long(long_options, ARRAY_SIZE(long_options), detail_options, sizeof(detail_options)); |
| 37 |
printf("%s\n", detail_options); |
| 38 |
} |
| 39 |
|
| 40 |
int parse_confile(const char* confile) { |
| 41 |
IniParser ini; |
| 42 |
int ret = ini.LoadFromFile(confile); |
| 43 |
if (ret != 0) { |
| 44 |
printf("Load confile [%s] failed: %d\n", confile, ret); |
| 45 |
exit(-40); |
| 46 |
} |
| 47 |
|
| 48 |
// logfile |
| 49 |
std::string str = ini.GetValue("logfile"); |
| 50 |
if (!str.empty()) { |
| 51 |
strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile)); |
| 52 |
} |
| 53 |
hlog_set_file(g_main_ctx.logfile); |
| 54 |
// loglevel |
| 55 |
str = ini.GetValue("loglevel"); |
| 56 |
if (!str.empty()) { |
| 57 |
hlog_set_level_by_str(str.c_str()); |
| 58 |
} |
| 59 |
// log_filesize |
| 60 |
str = ini.GetValue("log_filesize"); |
| 61 |
if (!str.empty()) { |
| 62 |
hlog_set_max_filesize_by_str(str.c_str()); |
| 63 |
} |
| 64 |
// log_remain_days |
| 65 |
str = ini.GetValue("log_remain_days"); |
| 66 |
if (!str.empty()) { |
| 67 |
hlog_set_remain_days(atoi(str.c_str())); |
| 68 |
} |
| 69 |
// log_fsync |
| 70 |
str = ini.GetValue("log_fsync"); |
| 71 |
if (!str.empty()) { |
| 72 |
logger_enable_fsync(hlog, hv_getboolean(str.c_str())); |
| 73 |
} |
| 74 |
hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version()); |
| 75 |
hlog_fsync(); |
| 76 |
|
| 77 |
// worker_processes |
| 78 |
int worker_processes = 0; |
| 79 |
#ifdef DEBUG |
| 80 |
// Disable multi-processes mode for debugging |
| 81 |
worker_processes = 0; |
| 82 |
#else |
| 83 |
str = ini.GetValue("worker_processes"); |
| 84 |
if (str.size() != 0) { |
| 85 |
if (strcmp(str.c_str(), "auto") == 0) { |
| 86 |
worker_processes = get_ncpu(); |
| 87 |
hlogd("worker_processes=ncpu=%d", worker_processes); |
| 88 |
} |
| 89 |
else { |
| 90 |
worker_processes = atoi(str.c_str()); |
| 91 |
} |
| 92 |
} |
| 93 |
#endif |
| 94 |
g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES); |
| 95 |
// worker_threads |
| 96 |
int worker_threads = 0; |
| 97 |
str = ini.GetValue("worker_threads"); |
| 98 |
if (str.size() != 0) { |
| 99 |
if (strcmp(str.c_str(), "auto") == 0) { |
| 100 |
worker_threads = get_ncpu(); |
| 101 |
hlogd("worker_threads=ncpu=%d", worker_threads); |
| 102 |
} |
| 103 |
else { |
| 104 |
worker_threads = atoi(str.c_str()); |
| 105 |
} |
| 106 |
} |
| 107 |
g_http_server.worker_threads = LIMIT(0, worker_threads, 64); |
| 108 |
|
| 109 |
// worker_connections |
| 110 |
str = ini.GetValue("worker_connections"); |
| 111 |
if (str.size() != 0) { |
| 112 |
g_http_server.worker_connections = atoi(str.c_str()); |
| 113 |
} |
| 114 |
|
| 115 |
// http_port |
| 116 |
int port = 0; |
| 117 |
const char* szPort = get_arg("p"); |
| 118 |
if (szPort) { |
| 119 |
port = atoi(szPort); |
| 120 |
} |
| 121 |
if (port == 0) { |
| 122 |
port = ini.Get<int>("port"); |
| 123 |
} |
| 124 |
if (port == 0) { |
| 125 |
port = ini.Get<int>("http_port"); |
| 126 |
} |
| 127 |
g_http_server.port = port; |
| 128 |
// https_port |
| 129 |
if (HV_WITH_SSL) { |
| 130 |
g_http_server.https_port = ini.Get<int>("https_port"); |
| 131 |
} |
| 132 |
if (g_http_server.port == 0 && g_http_server.https_port == 0) { |
| 133 |
printf("Please config listen port!\n"); |
| 134 |
exit(-10); |
| 135 |
} |
| 136 |
|
| 137 |
// base_url |
| 138 |
str = ini.GetValue("base_url"); |
| 139 |
if (str.size() != 0) { |
| 140 |
g_http_service.base_url = str; |
| 141 |
} |
| 142 |
// document_root |
| 143 |
str = ini.GetValue("document_root"); |
| 144 |
if (str.size() != 0) { |
| 145 |
g_http_service.document_root = str; |
| 146 |
} |
| 147 |
// home_page |
| 148 |
str = ini.GetValue("home_page"); |
| 149 |
if (str.size() != 0) { |
| 150 |
g_http_service.home_page = str; |
| 151 |
} |
| 152 |
// error_page |
| 153 |
str = ini.GetValue("error_page"); |
| 154 |
if (str.size() != 0) { |
| 155 |
g_http_service.error_page = str; |
| 156 |
} |
| 157 |
// index_of |
| 158 |
str = ini.GetValue("index_of"); |
| 159 |
if (str.size() != 0) { |
| 160 |
g_http_service.index_of = str; |
| 161 |
} |
| 162 |
// keepalive_timeout |
| 163 |
str = ini.GetValue("keepalive_timeout"); |
| 164 |
if (str.size() != 0) { |
| 165 |
g_http_service.keepalive_timeout = atoi(str.c_str()); |
| 166 |
} |
| 167 |
// limit_rate |
| 168 |
str = ini.GetValue("limit_rate"); |
| 169 |
if (str.size() != 0) { |
| 170 |
g_http_service.limit_rate = atoi(str.c_str()); |
| 171 |
} |
| 172 |
// access_log |
| 173 |
str = ini.GetValue("access_log"); |
| 174 |
if (str.size() != 0) { |
| 175 |
g_http_service.enable_access_log = hv_getboolean(str.c_str()); |
| 176 |
} |
| 177 |
// cors |
| 178 |
if (ini.Get<bool>("cors")) { |
| 179 |
g_http_service.AllowCORS(); |
| 180 |
} |
| 181 |
// ssl |
| 182 |
if (g_http_server.https_port > 0) { |
| 183 |
std::string crt_file = ini.GetValue("ssl_certificate"); |
| 184 |
std::string key_file = ini.GetValue("ssl_privatekey"); |
| 185 |
std::string ca_file = ini.GetValue("ssl_ca_certificate"); |
| 186 |
hlogi("SSL backend is %s", hssl_backend()); |
| 187 |
hssl_ctx_opt_t param; |
| 188 |
memset(¶m, 0, sizeof(param)); |
| 189 |
param.crt_file = crt_file.c_str(); |
| 190 |
param.key_file = key_file.c_str(); |
| 191 |
param.ca_file = ca_file.c_str(); |
| 192 |
param.endpoint = HSSL_SERVER; |
| 193 |
if (g_http_server.newSslCtx(¶m) != 0) { |
| 194 |
#ifdef OS_WIN |
| 195 |
if (strcmp(hssl_backend(), "schannel") == 0) { |
| 196 |
hlogw("schannel needs pkcs12 formatted certificate file."); |
| 197 |
g_http_server.https_port = 0; |
| 198 |
} |
| 199 |
#else |
| 200 |
hloge("SSL certificate verify failed!"); |
| 201 |
exit(0); |
| 202 |
#endif |
| 203 |
} |
| 204 |
else { |
| 205 |
hlogi("SSL certificate verify ok!"); |
| 206 |
} |
| 207 |
} |
| 208 |
// proxy |
| 209 |
auto proxy_keys = ini.GetKeys("proxy"); |
| 210 |
for (const auto& proxy_key : proxy_keys) { |
| 211 |
str = ini.GetValue(proxy_key, "proxy"); |
| 212 |
if (str.empty()) continue; |
| 213 |
if (proxy_key[0] == '/') { |
| 214 |
// reverse proxy |
| 215 |
const std::string& path = proxy_key; |
| 216 |
std::string proxy_url = hv::ltrim(str, "> "); |
| 217 |
hlogi("reverse_proxy %s => %s", path.c_str(), proxy_url.c_str()); |
| 218 |
g_http_service.Proxy(path.c_str(), proxy_url.c_str()); |
| 219 |
} |
| 220 |
else if (strcmp(proxy_key.c_str(), "proxy_connect_timeout") == 0) { |
| 221 |
g_http_service.proxy_connect_timeout = atoi(str.c_str()); |
| 222 |
} |
| 223 |
else if (strcmp(proxy_key.c_str(), "proxy_read_timeout") == 0) { |
| 224 |
g_http_service.proxy_read_timeout = atoi(str.c_str()); |
| 225 |
} |
| 226 |
else if (strcmp(proxy_key.c_str(), "proxy_write_timeout") == 0) { |
| 227 |
g_http_service.proxy_write_timeout = atoi(str.c_str()); |
| 228 |
} |
| 229 |
else if (strcmp(proxy_key.c_str(), "forward_proxy") == 0) { |
| 230 |
hlogi("forward_proxy = %s", str.c_str()); |
| 231 |
if (hv_getboolean(str.c_str())) { |
| 232 |
g_http_service.EnableForwardProxy(); |
| 233 |
} |
| 234 |
} |
| 235 |
else if (strcmp(proxy_key.c_str(), "trust_proxies") == 0) { |
| 236 |
auto trust_proxies = hv::split(str, ';'); |
| 237 |
for (auto trust_proxy : trust_proxies) { |
| 238 |
trust_proxy = hv::trim(trust_proxy); |
| 239 |
if (trust_proxy.empty()) continue; |
| 240 |
hlogi("trust_proxy %s", trust_proxy.c_str()); |
| 241 |
g_http_service.AddTrustProxy(trust_proxy.c_str()); |
| 242 |
} |
| 243 |
} |
| 244 |
else if (strcmp(proxy_key.c_str(), "no_proxies") == 0) { |
| 245 |
auto no_proxies = hv::split(str, ';'); |
| 246 |
for (auto no_proxy : no_proxies) { |
| 247 |
no_proxy = hv::trim(no_proxy); |
| 248 |
if (no_proxy.empty()) continue; |
| 249 |
hlogi("no_proxy %s", no_proxy.c_str()); |
| 250 |
g_http_service.AddNoProxy(no_proxy.c_str()); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
hlogi("parse_confile('%s') OK", confile); |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
static void on_reload(void* userdata) { |
| 260 |
hlogi("reload confile [%s]", g_main_ctx.confile); |
| 261 |
parse_confile(g_main_ctx.confile); |
| 262 |
} |
| 263 |
|
| 264 |
int main(int argc, char** argv) { |
| 265 |
// g_main_ctx |
| 266 |
main_ctx_init(argc, argv); |
| 267 |
int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options)); |
| 268 |
if (ret != 0) { |
| 269 |
print_help(); |
| 270 |
exit(ret); |
| 271 |
} |
| 272 |
|
| 273 |
// help |
| 274 |
if (get_arg("h")) { |
| 275 |
print_help(); |
| 276 |
exit(0); |
| 277 |
} |
| 278 |
|
| 279 |
// version |
| 280 |
if (get_arg("v")) { |
| 281 |
print_version(); |
| 282 |
exit(0); |
| 283 |
} |
| 284 |
|
| 285 |
// parse_confile |
| 286 |
const char* confile = get_arg("c"); |
| 287 |
if (confile) { |
| 288 |
strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile)); |
| 289 |
} |
| 290 |
parse_confile(g_main_ctx.confile); |
| 291 |
|
| 292 |
// test |
| 293 |
if (get_arg("t")) { |
| 294 |
printf("Test confile [%s] OK!\n", g_main_ctx.confile); |
| 295 |
exit(0); |
| 296 |
} |
| 297 |
|
| 298 |
// signal |
| 299 |
signal_init(on_reload); |
| 300 |
const char* signal = get_arg("s"); |
| 301 |
if (signal) { |
| 302 |
signal_handle(signal); |
| 303 |
} |
| 304 |
|
| 305 |
#ifdef OS_UNIX |
| 306 |
// daemon |
| 307 |
if (get_arg("d")) { |
| 308 |
// nochdir, noclose |
| 309 |
int ret = daemon(1, 1); |
| 310 |
if (ret != 0) { |
| 311 |
printf("daemon error: %d\n", ret); |
| 312 |
exit(-10); |
| 313 |
} |
| 314 |
} |
| 315 |
#endif |
| 316 |
|
| 317 |
// pidfile |
| 318 |
create_pidfile(); |
| 319 |
|
| 320 |
// http_server |
| 321 |
Router::Register(g_http_service); |
| 322 |
g_http_server.registerHttpService(&g_http_service); |
| 323 |
|
| 324 |
#if 0 |
| 325 |
std::atomic_flag init_flag = ATOMIC_FLAG_INIT; |
| 326 |
g_http_server.onWorkerStart = [&init_flag](){ |
| 327 |
if (!init_flag.test_and_set()) { |
| 328 |
hv::async::startup(); |
| 329 |
} |
| 330 |
}; |
| 331 |
g_http_server.onWorkerStop = [&init_flag](){ |
| 332 |
if (init_flag.test_and_set()) { |
| 333 |
hv::async::cleanup(); |
| 334 |
} |
| 335 |
}; |
| 336 |
#endif |
| 337 |
|
| 338 |
g_http_server.run(); |
| 339 |
return ret; |
| 340 |
} |