在线oj之server模块

oj_server.cpp模块是整个在线oj项目与外界沟通的桥梁,负责项目后台与外界的通信。在这个模块中使用了github上一个http开源库(上一篇博客我提到过),模板填充技术以及Json数据格式,里面有些可能看不懂,后面的博客我会详细介绍。

Server:httplib中的一个Server类
Get:Server中的Get请求方法
Post:Server中的Post方法
set_content:httplib中定义的响应接口
listen:Server中的监听方法

    //有兴趣可以自己看源代码
  Server &Get(const char *pattern, Handler handler);
  Server &Post(const char *pattern, Handler handler);
  using Handler = std::function<void(const Request &, Response &)>;
  void set_content(std::string s, const char *content_type);
  bool listen(const char *host, int port, int socket_flags = 0);

OjModel:试题信息模块的一个类,里面封装了获取试题信息的方法
OjView:页面渲染模块,里面封装了用于将获取到的信息渲染到网页的方法
LOG:日志模块下一个打印消息的方法
Compiler:编译模块中的一个类

主页面效果,很low
在这里插入图片描述

#include "httplib.h"
#include "oj_model.hpp"
#include "oj_view.hpp"
#include "oj_log.hpp"
#include "compile.hpp"


int main()
{
    //httplib的时候,需要使用httplib使用的命名空间
    using namespace httplib;

    Server svr;

    OjModel oj_model;
    //获取试题信息,信息来源于文件
    svr.Get("/all_questions", [&oj_model](const Request& req, Response& resp){
            std::vector<Question> ques;
            oj_model.GetAllQuestions(&ques);
            std::string html;
            //想使用模板技术去填充html页面
            OjView::ExpandAllQuestionsHtml(&html, ques);
            resp.set_content(html, "text/html; charset=UTF-8");
            });

    //正则表达式
    // \b 单词的分界
    // *:匹配任意字符串
    // \d:匹配一个数字
    // (): 分组应用
    // 源码转义:特殊字符按照特殊字符字面源码编译 R"(str)“
  svr.Get(R"(/question/(\d+))", [&oj_model](const Request& req, Response& resp){
      //1.去试题模块查找对应题号的具体题目信息
      //  map当中(序号 名称 题目地址 难度)

      std::string desc;
      std::string header;
      //从querystr中获取id
      LOG(INFO, "req.matches") << req.matches[0] << ":" << req.matches[1] << std::endl;
      //  2. 在题目地址的路径下去加载单个题目的描述信息
      struct Question ques;
      oj_model.GetOneQuestion(req.matches[1].str(), &desc, &header, &ques);
      //3.组织,返回给浏览器
      std::string html;
      OjView::ExpandOneQuestion(ques, desc, header, &html);
      resp.set_content(html, "text/html; charset=UTF-8");
  });

  svr.Post(R"(/question/(\d+))", [&oj_model](const Request& req, Response& resp){
          //1.从正文当中提取出来提交的内容,主要是提取code字段的内容
          // 提交的内容当中有url编码,需要对提交内容解码
          std::unordered_map<std::string, std::string> pram;
          UrlUtil::PraseBody(req.body, &pram);
          //  2. 编译&&运行
          std::string code;
          oj_model.SplitingCode(pram["code"], req.matches[1].str(), &code);
          Json::Value req_json;
          req_json["code"] = code;
          Json::Value resp_json;
          Compiler::CompilerAndRun(req_json, &resp_json);
          //  3.构造响应,json
          const std::string errorno = resp_json["errorno"].asString();
          const std::string reason = resp_json["reason"].asString();
          const std::string stdout_reason = resp_json["stdout"].asString();
          std::string html;
          OjView::ExpandReason(errorno, reason, stdout_reason, &html);
          resp.set_content(html, "text/html; charset=UTF-8");
          });
  LOG(INFO, "Listen in 0.0.0.0:20000");
  LOG(INFO, "Server ready");
  svr.listen("0.0.0.0", 20000);
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值