-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (49 loc) · 1.91 KB
/
Copy pathmain.cpp
File metadata and controls
61 lines (49 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "./log/log.h"
#include "./muduo/include/EventLoop.h"
#include "./muduo/include/InetAddress.h"
#include "./utils/EnvConfig.h"
#include "config.h"
#include "webserver_muduo.h"
#include <unistd.h>
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
// ✓ 1. 从环境变量读取数据库配置(安全加固)
std::string user = EnvConfig::getEnv("DB_USER", "webuser");
std::string passwd = EnvConfig::getEnv("DB_PASS");
std::string databasename = EnvConfig::getEnv("DB_NAME", "tinyweb");
// 验证必需配置
if (passwd.empty()) {
std::cerr << "Error: DB_PASS environment variable not set" << std::endl;
return 1;
}
// 2. 命令行解析:获取用户自定义运行参数
Config config;
config.parse_arg(argc, argv);
// 3. 初始化日志系统(原项目文件日志)
// 使用异步日志提升性能:max_queue_size > 0 时启用异步模式
if (config.close_log == 0) {
int async_queue_size = (config.LOGWrite == 1) ? 800000 : 1000; // 同步模式也使用小队列
Log::get_instance()->init("./ServerLog", config.close_log, 8192, 800000,
async_queue_size);
LOG_INFO("=== Muduo WebServer Starting ===");
}
// 4. 创建 EventLoop(One Loop Per Thread 核心)
EventLoop loop;
// 5. 配置监听地址
InetAddress listenAddr(config.PORT);
// 6. 实例化 Muduo 风格 WebServer
WebServerMuduo server(&loop, listenAddr, "TinyWebServer-Muduo");
// 7. 初始化服务器配置
server.init(user, passwd, databasename, config.LOGWrite, config.close_log,
config.sql_num, config.thread_num);
// 8. 设置工作线程数(IO线程池)
server.setThreadNum(config.thread_num);
// 9. 启动服务器
LOG_INFO("Server starting on port %d", config.PORT);
server.start();
// 10. 启动事件循环(阻塞在此,等待连接)
LOG_INFO("EventLoop started");
loop.loop();
return 0;
}