5综合调试cr模块|测试json|测试输出代码|测试cr模块|清理临时文件|接入cpp-httplib|测试httplib(C++)

综合调试

compile_server.cc

测试json串
#include "compile_run.hpp"
  
using namespace ns_compile_and_run;
  
//编译服务随时可能被多个人请求,必须保证传递上来的code,形成源文件名称的时候,要具有唯一性,要不然多个用户之间会互相影响
int main()
{
    // in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
    // out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
    //通过http,让client给上传一个json string
    //下面的工作,充当客户端请求的字符串
    std::string in_json;
  
    Json::Value in_value;
    in_value["code"] = "";
    in_value["input"] = "";
    in_value["cpu_limit"] = 1;
    in_value["mem_limit"] = 10240 * 3;
  
    Json::FastWriter writer;
    in_json = writer.write(in_value);
  
    std::cout << in_json << std::endl;
    //CompileAndRun::Start();
    return 0;
}

![[Pasted image 20250222161952.png]]

测试输出代码
#include "compile_run.hpp"
  
using namespace ns_compile_and_run;
  
//编译服务随时可能被多个人请求,必须保证传递上来的code,形成源文件名称的时候,要具有唯一性,要不然多个用户之间会互相影响
int main()
{
    // in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
    // out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
    //通过http,让client给上传一个json string
    //下面的工作,充当客户端请求的字符串
    std::string in_json;
  
    Json::Value in_value;
    in_value["code"] = R"(#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        return 0;
    })";
    in_value["input"] = "";
    in_value["cpu_limit"] = 1;
    in_value["mem_limit"] = 10240 * 3;
  
    Json::FastWriter writer;
    in_json = writer.write(in_value);
  
    std::cout << in_json << std::endl;
    //CompileAndRun::Start();
    return 0;
}

![[Pasted image 20250222162616.png]]

测试cr模块
#include "compile_run.hpp"
  
using namespace ns_compile_and_run;
  
//编译服务随时可能被多个人请求,必须保证传递上来的code,形成源文件名称的时候,要具有唯一性,要不然多个用户之间会互相影响
int main()
{
    //in_json: {"code": "#include...", "input": "","cpu_limit":1, "mem_limit":10240}
    //out_json: {"status":"0", "reason":"","stdout":"","stderr":"",}
    //通过http 让client 给我们 上传一个json string
    //下面的工作,充当客户端请求的json串
    std::string in_json;
    Json::Value in_value;
    //R"()", raw string
    in_value["code"] = R"(#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        return 0;
    })";
    in_value["input"] = "";
    in_value["cpu_limit"] = 1;
    in_value["mem_limit"] = 10240*3;
    Json::FastWriter writer;
    in_json = writer.write(in_value);
    std::cout << in_json << std::endl;
    //这个是将来给客户端返回的json串
    std::string out_json;
    CompileAndRun::Start(in_json, &out_json);
  
    std::cout << out_json << std::endl;
    return 0;
}

![[Pasted image 20250222170629.png]]

![[Pasted image 20250222170640.png]]

#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        while(1);
        return 0;
    }

![[Pasted image 20250222200628.png]]

#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        int *p = new int[1024*1024*20];
        return 0;
    }

![[Pasted image 20250222201026.png]]

#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        int a = 0;
        a /= 0;
        return 0;
   }

![[Pasted image 20250222201216.png]]

#include<iostream>
    int main(){
        std::cout << "你可以看见我了" << std::endl;
        aaaaa
        return 0;
    }

![[Pasted image 20250222201350.png]]

清理临时文件

compile_run.hpp

    class CompileAndRun
    {
    public:
        static void RemoveTempFile(const std::string &file_name)
        {
            //清理文件的个数是不确定的,但是有哪些我们是知道的
            std::string _src = PathUtil::Src(file_name);
            if(FileUtil::IsFileExists(_src)) unlink(_src.c_str());
  
            std::string _compiler_error = PathUtil::CompilerError(file_name);
            if(FileUtil::IsFileExists(_compiler_error)) unlink(_compiler_error.c_str());
  
            std::string _execute = PathUtil::Exe(file_name);
            if(FileUtil::IsFileExists(_execute)) unlink(_execute.c_str());
  
            std::string _stdin = PathUtil::Stdin(file_name);
            if(FileUtil::IsFileExists(_stdin)) unlink(_stdin.c_str());
  
            std::string _stdout = PathUtil::Stdout(file_name);
            if(FileUtil::IsFileExists(_stdout)) unlink(_stdout.c_str());
  
            std::string _stderr = PathUtil::Stderr(file_name);
            if(FileUtil::IsFileExists(_stderr)) unlink(_stderr.c_str());
        }

        static void Start(const std::string &in_json, std::string *out_json)
        {
            Json::Value in_value;
            Json::Reader reader;
            reader.parse(in_json, in_value); //最后在处理差错问题
//...
            if (status_code == 0)

            {
                // 整个过程全部成功
                std::string _stdout;
               FileUtil::ReadFile(PathUtil::Stdout(file_name), &_stdout, true);
                out_value["stdout"] = _stdout;
                std::string _stderr;
                FileUtil::ReadFile(PathUtil::Stderr(file_name), &_stderr, true);
                out_value["stderr"] = _stderr;
            }

            Json::StyledWriter writer;
            *out_json = writer.write(out_value);
  
            RemoveTempFile(file_name);
        }
	};

这样测试之后,temp不会有临时文件,如果后期想调试可以注释掉这些代码
![[Pasted image 20250223014253.png]]

接入cpp-httplib

将导入的httplib.h拷贝到comm文件夹底下

cp ./cpphttplib/cpp-httplib-0.7.15/httplib.h ./comm/ -rf

![[Pasted image 20250223020541.png]]

测试

compile_server.cc

#include "../comm/httplib.h"
using namespace httplib;

int main()
{

    Server svr;

    svr.listen("0.0.0.0", 8080); //启动http服务
}

查看服务

netstat -nltp

![[Pasted image 20250223022036.png]]

8080的就是

#include "../comm/httplib.h"

using namespace httplib;

int main()

{
    Server svr;
    
    svr.Get("/hello",[](const Request &req, Response &resp){

        // 用来进行基本测试

        resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    });
    
    svr.listen("0.0.0.0", 8080); //启动http服务
}

![[Pasted image 20250223024143.png]]

创建一个wwwroot目录和index.html文件用于测试
![[Pasted image 20250223024711.png]]

#include "../comm/httplib.h"
using namespace httplib;

int main()
{
    Server svr;
    
    svr.Get("/hello",[](const Request &req, Response &resp){
        // 用来进行基本测试
        resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080); //启动http服务
}

![[Pasted image 20250223024934.png]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值