#include #include #include #include #include "auth/AuthManager.h" #include "controllers/AuthController.h" #include "controllers/WindPowerController.h" #include "utils/ResponseUtil.h" using namespace drogon; int main() { // 保留任务耗时日志,便于定位上传、清洗与回传瓶颈。 trantor::Logger::setLogLevel(trantor::Logger::kInfo); // 加载 Drogon 配置(监听端口 / CORS / 静态资源根目录) LOG_INFO << "Loading server configuration..."; app().loadConfigFile("config/server_config.json"); if (!AuthManager::Instance().Initialize()) { LOG_ERROR << "Failed to initialize account database"; return 1; } app().registerController(std::make_shared()); app().registerController(std::make_shared()); app().registerPreRoutingAdvice( [](const HttpRequestPtr& req, AdviceCallback&& stop, AdviceChainCallback&& next) { const std::string path = req->path(); const bool public_api = path == "/api/auth/login" || path == "/api/auth/logout" || path == "/api/auth/me" || path == "/api/system/health" || path == "/api/system/version"; if (req->method() == Options || path.rfind("/api/", 0) != 0 || public_api) { next(); return; } const auto auth = AuthManager::Instance().Authenticate( req->getCookie(AuthManager::kSessionCookie)); if (auth.status == AuthStatus::kAuthenticated) { next(); return; } const bool account_abnormal = auth.status == AuthStatus::kExpired || auth.status == AuthStatus::kDisabled; const auto message = account_abnormal ? "账号异常请联系管理员" : "请先登录"; const auto code = account_abnormal ? 4 : 2; auto response = HttpResponse::newHttpResponse(); response->setContentTypeCode(CT_APPLICATION_JSON); response->setBody( ResponseUtil::GenerateErrorResponse(code, message).dump()); response->setStatusCode(k401Unauthorized); stop(response); }); // SPA 前端路由回退:未匹配路径统一返回 index.html,交由前端路由处理 app().setCustom404Page( HttpResponse::newFileResponse("./web/index.html", "", CT_TEXT_HTML), false); LOG_INFO << "wind_server starting..."; // 阻塞运行,直到收到 SIGINT/SIGTERM app().run(); LOG_INFO << "wind_server stopped"; return 0; }