This commit is contained in:
Ryan6981 2024-10-17 02:42:44 +08:00
parent 290a77729e
commit cd72100d95
6 changed files with 58 additions and 40 deletions

View File

@ -7,6 +7,9 @@ services:
container_name: rust-compiler
networks:
- rust-net
# 确保Rust编译器作为一个长期运行的服务
tty: true
stdin_open: true
web-server:
build:

View File

@ -3,10 +3,8 @@ name = "rust_compiler"
version = "0.1.0"
edition = "2021"
# 指定二进制目标
[[bin]]
name = "rust_compiler"
path = "src/main.rs"
# 添加依赖项
[dependencies]

View File

@ -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"]

View File

@ -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(())
}

View File

@ -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;"]

View File

@ -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