This commit is contained in:
Ryan6981 2024-10-17 01:08:56 +08:00
commit 290a77729e
9 changed files with 236 additions and 0 deletions

23
docker-compose.yml Normal file
View File

@ -0,0 +1,23 @@
version: '3.9'
services:
rust-compiler:
build:
context: ./rust-compiler
container_name: rust-compiler
networks:
- rust-net
web-server:
build:
context: ./web-server
container_name: web-server
ports:
- "80:80"
networks:
- rust-net
depends_on:
- rust-compiler
networks:
rust-net:

40
redme.txt Normal file
View File

@ -0,0 +1,40 @@
rust-online-compiler
├── docker-compose.yml
├── rust-compiler
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── web-server
├── Dockerfile
├── nginx.conf
├── lua_scripts
│ └── compile.lua
└── static
└── index.html
架构是将Rust编译环境和Web服务器包括前端UI分为两个独立的Docker镜像并且这两个镜像之间可以相互通信。
4. 构建和运行
在项目的根目录下运行以下命令来构建和启动所有服务:
bash
深色版本
docker-compose up --build
这将会构建所有的Docker镜像并启动相应的容器。你可以通过访问http://localhost来查看前端界面并尝试编译Rust代码。
#查看端口占用和结束端口进程
sudo lsof -i :80
sudo kill -9 <PID>
#删除镜像重建镜像
重新构建和运行
在项目的根目录下运行以下命令来构建和启动所有服务:
docker-compose down
docker-compose up --build

12
rust-compiler/Cargo.toml Normal file
View File

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

38
rust-compiler/Dockerfile Normal file
View File

@ -0,0 +1,38 @@
# 使用官方的Rust镜像作为基础镜像
FROM rust:latest as builder
# 设置工作目录
WORKDIR /app
# 拷贝Cargo.toml
COPY Cargo.toml .
# 拷贝Rust源代码
COPY 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/*
# 设置工作目录
WORKDIR /app
# 从builder阶段拷贝可执行文件
COPY --from=builder /app/rust_compiler/target/release/rust_compiler .
# 暴露一个端口用于接收编译请求
EXPOSE 8080
# 运行Rust程序
CMD ["./rust_compiler"]

28
rust-compiler/src/main.rs Normal file
View File

@ -0,0 +1,28 @@
use std::io::{self, Read, Write};
use std::process::Command;
fn main() -> io::Result<()> {
loop {
// 读取一行输入
let mut input = String::new();
println!("Waiting for input...");
io::stdin().read_line(&mut input)?;
// 去掉行尾的换行符
let code = input.trim_end();
// 编译用户提交的代码
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))?;
}
}
}
}

21
web-server/Dockerfile Normal file
View File

@ -0,0 +1,21 @@
# 使用官方的OpenResty Alpine镜像作为基础镜像
FROM openresty/openresty:alpine
# 更新包列表并安装必要的工具
RUN apk add --no-cache curl
# 设置工作目录
WORKDIR /usr/local/openresty/nginx/html
# 复制Nginx配置文件到容器中
COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
# 如果有自定义的Lua脚本或静态资源复制它们
COPY lua_scripts/ /usr/local/openresty/nginx/html/lua_scripts/
COPY static/ /usr/local/openresty/nginx/html/static/
# 暴露80端口
EXPOSE 80
# 启动OpenResty
CMD ["openresty", "-g", "daemon off;"]

View File

@ -0,0 +1,22 @@
local function compile_rust_code(code)
local rust_compiler = io.popen("rust-compiler", "w+")
if not rust_compiler then
return "Error: Failed to start rust-compiler"
end
-- 发送代码到Rust编译器
rust_compiler:write(code .. "\n")
rust_compiler:flush()
-- 读取编译结果
local output = {}
for line in rust_compiler:lines() do
table.insert(output, line)
end
rust_compiler:close()
return table.concat(output, "\n")
end
ngx.header["Content-Type"] = "application/json; charset=utf-8"
ngx.say(cjson.encode({ result = compile_rust_code(ngx.var.arg_code) }))

28
web-server/nginx.conf Normal file
View File

@ -0,0 +1,28 @@
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html/static;
index index.html;
}
location /compile {
content_by_lua_file /usr/local/openresty/nginx/html/lua_scripts/compile.lua;
}
}
}

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rust Online Compiler</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>Rust Online Compiler</h1>
<textarea id="code" rows="10" cols="50"></textarea><br/>
<button onclick="compile()">Compile</button>
<pre id="output"></pre>
<script>
function compile() {
const code = document.getElementById('code').value;
axios.post('/compile', { code })
.then(response => {
document.getElementById('output').textContent = response.data.result;
});
}
</script>
</body>
</html>