From cd72100d950e6106ea2f9ef19ea4580294abd71a Mon Sep 17 00:00:00 2001 From: Ryan6981 <995559618@qq.com> Date: Thu, 17 Oct 2024 02:42:44 +0800 Subject: [PATCH] up --- docker-compose.yml | 3 ++ rust-compiler/Cargo.toml | 2 -- rust-compiler/Dockerfile | 13 ++------- rust-compiler/src/main.rs | 47 ++++++++++++++++++++---------- web-server/Dockerfile | 31 ++++++++++++-------- web-server/lua_scripts/compile.lua | 2 ++ 6 files changed, 58 insertions(+), 40 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7b1d298..0452e1f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,9 @@ services: container_name: rust-compiler networks: - rust-net + # 确保Rust编译器作为一个长期运行的服务 + tty: true + stdin_open: true web-server: build: diff --git a/rust-compiler/Cargo.toml b/rust-compiler/Cargo.toml index 78c8d58..9614b02 100644 --- a/rust-compiler/Cargo.toml +++ b/rust-compiler/Cargo.toml @@ -3,10 +3,8 @@ name = "rust_compiler" version = "0.1.0" edition = "2021" -# 指定二进制目标 [[bin]] name = "rust_compiler" path = "src/main.rs" -# 添加依赖项 [dependencies] diff --git a/rust-compiler/Dockerfile b/rust-compiler/Dockerfile index 628e11b..5d1ce65 100644 --- a/rust-compiler/Dockerfile +++ b/rust-compiler/Dockerfile @@ -5,22 +5,18 @@ FROM rust:latest as builder WORKDIR /app # 拷贝Cargo.toml -COPY Cargo.toml . +COPY ./Cargo.toml . # 拷贝Rust源代码 -COPY src/ . +COPY ./src/ src/ # 构建Rust程序 -RUN cargo new --bin rust_compiler -WORKDIR /app/rust_compiler -RUN cp /app/main.rs . RUN cargo build --release # 最终镜像 FROM debian:stable-slim # 安装必要的依赖 -#RUN sed -i 's|http://deb.debian.org/debian|https://mirrors.tuna.tsinghua.edu.cn/debian|g' /etc/apt/sources.list && RUN apt-get update && \ apt-get install -y ca-certificates && \ rm -rf /var/lib/apt/lists/* @@ -29,10 +25,7 @@ RUN apt-get update && \ WORKDIR /app # 从builder阶段拷贝可执行文件 -COPY --from=builder /app/rust_compiler/target/release/rust_compiler . - -# 暴露一个端口用于接收编译请求 -EXPOSE 8080 +COPY --from=builder /app/target/release/rust_compiler . # 运行Rust程序 CMD ["./rust_compiler"] diff --git a/rust-compiler/src/main.rs b/rust-compiler/src/main.rs index 4b741d8..b107277 100644 --- a/rust-compiler/src/main.rs +++ b/rust-compiler/src/main.rs @@ -1,4 +1,4 @@ -use std::io::{self, Read, Write}; +use std::io::{self, Write}; use std::process::Command; fn main() -> io::Result<()> { @@ -6,23 +6,38 @@ fn main() -> io::Result<()> { // 读取一行输入 let mut input = String::new(); println!("Waiting for input..."); - io::stdin().read_line(&mut input)?; + match io::stdin().read_line(&mut input) { + Ok(0) => { + // 如果读取到EOF(例如管道关闭),退出循环 + break; + } + Ok(_) => { + // 去掉行尾的换行符 + let code = input.trim_end(); - // 去掉行尾的换行符 - let code = input.trim_end(); + // 编译用户提交的代码 + let output = Command::new("rustc") + .arg("-") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("Failed to start rustc") + .wait_with_output() + .expect("Failed to wait on rustc"); - // 编译用户提交的代码 - let output = Command::new("rustc") - .arg("-") - .input(code.as_bytes()) - .output()?; - - // 输出编译结果 - if output.status.success() { - writeln!(io::stdout(), "Compiled successfully")?; - } else { - writeln!(io::stderr(), "Compilation failed:\n{}", String::from_utf8_lossy(&output.stderr))?; + // 输出编译结果 + if output.status.success() { + writeln!(io::stdout(), "Compiled successfully")?; + } else { + writeln!(io::stderr(), "Compilation failed:\n{}", String::from_utf8_lossy(&output.stderr))?; + } + } + Err(e) => { + eprintln!("Error reading input: {}", e); + break; + } } } -} + Ok(()) } diff --git a/web-server/Dockerfile b/web-server/Dockerfile index 43ed9ab..7d7c0d5 100644 --- a/web-server/Dockerfile +++ b/web-server/Dockerfile @@ -1,21 +1,28 @@ -# 使用官方的OpenResty Alpine镜像作为基础镜像 FROM openresty/openresty:alpine -# 更新包列表并安装必要的工具 -RUN apk add --no-cache curl +# 安装依赖 +RUN apk add --no-cache build-base git + +# 下载并安装 luarocks +RUN git clone https://github.com/luarocks/luarocks.git /tmp/luarocks && \ + cd /tmp/luarocks && \ + ./configure --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1 && \ + make build && \ + make install + +# 添加 luarocks 到 PATH +ENV PATH="/usr/local/openresty/luajit/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" + +# 安装 lua-cjson +RUN luarocks install lua-cjson # 设置工作目录 WORKDIR /usr/local/openresty/nginx/html -# 复制Nginx配置文件到容器中 +# 拷贝配置文件 COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf +COPY lua_scripts/compile.lua lua_scripts/compile.lua +COPY static/index.html static/index.html -# 如果有自定义的Lua脚本或静态资源,复制它们 -COPY lua_scripts/ /usr/local/openresty/nginx/html/lua_scripts/ -COPY static/ /usr/local/openresty/nginx/html/static/ - -# 暴露80端口 -EXPOSE 80 - -# 启动OpenResty +# 启动 Nginx CMD ["openresty", "-g", "daemon off;"] diff --git a/web-server/lua_scripts/compile.lua b/web-server/lua_scripts/compile.lua index 83263d6..76fedef 100644 --- a/web-server/lua_scripts/compile.lua +++ b/web-server/lua_scripts/compile.lua @@ -1,3 +1,5 @@ +local cjson = require "cjson" + local function compile_rust_code(code) local rust_compiler = io.popen("rust-compiler", "w+") if not rust_compiler then