You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OJproject/src/my-online-judge-master/MYOJ/OJ_server/OJ_server.cc

75 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <fstream>
#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "OJ_Control.hpp"
static ns_control::Control *ctrl_ptr = nullptr;
void recovery(int signo)
{
ctrl_ptr->recoveryMachine();
}
int main(int argc, char *argv[])
{
// 0、修改指定信号的默认行为把ctrl + \产生的SIGQUIT信号重定向为一键上线所有主机交由运维使用
signal(SIGQUIT, recovery);
// 1、守护进程化
if (argc == 2)
{
std::string option(argv[1]);
if (option == "-d")
{
// 1、守护进程化
if (daemon(1, 0) == -1)
{
perror("OJ_server 守护进程化失败!\n");
exit(1);
}
}
}
// 2、用户请求的服务路由功能用户不同的请求前往不同的区域
httplib::Server svr; // httplib中的Server类
ns_control::Control ctrl; // 核心业务逻辑的控制器(初始化的同时,加载题库)
ctrl_ptr = &ctrl;
// 3、搭建首页告诉httplib服务器使用指定的目录作为静态文件服务的根目录在找不到任何对应路由请求的情况下使用这个目录下的文件作为响应即首页文件
svr.set_base_dir("/home/zzy1/2024/my-online-judge/MYOJ/OJ_server/wwwroot");
// 3.1 获取所有题目的列表
svr.Get("/all_questions", [&ctrl](const httplib::Request &req, httplib::Response &resp)
{
std::string html;
ctrl.allQuestions(&html);
resp.set_content(html, "text/html; charset=utf-8"); });
// 3.2 用户提供题目编号来获取题目内容
svr.Get(R"(/question/(\d+))", [&ctrl](const httplib::Request &req, httplib::Response &resp)
{
std::string number = req.matches[1]; //matches[1]中储存着正则匹配到的字符
std::string html;
ctrl.oneQuestion(number, &html);
resp.set_content(html, "text/html; charset=utf-8"); });
// 3.3 用户提交代码,使用我们的判题功能
// 尽管POST请求的参数是在正文当中的但是调用函数的时候还是要根据URL路径来调用因为HTTP 服务器的核心功能之一是根据客户端请求的 URL 路径来决定如何处理请求。路由机制确保了不同类型的请求可以被正确地路由到相应的处理函数。)
svr.Post(R"(/judge/(\d+))", [&ctrl](const httplib::Request &req, httplib::Response &resp)
{
std::string number = req.matches[1];
std::string result_json;
ctrl.Judge(number, req.body /* POST请求的参数存放在正文当中 */, &result_json);
resp.set_content(result_json, "application/json;charset=utf-8"); });
svr.listen("0.0.0.0", 8081);
return 0;
}