From 562fa553b77a96dd98e670d362f12d425144e0d6 Mon Sep 17 00:00:00 2001 From: Ryan6981 <995559618@qq.com> Date: Wed, 16 Oct 2024 17:08:07 +0800 Subject: [PATCH] up --- docker-compose.yml | 46 ++++++++++++++++++++++++++++++++ redme.txt | 42 ++++++++++++++++++++++++++++++ rust-compiler/Dockerfile | 28 ++++++++++++++++++++ rust-compiler/main.rs | 52 +++++++++++++++++++++++++++++++++++++ web-server/Dockerfile | 26 +++++++++++++++++++ web-server/app.py | 28 ++++++++++++++++++++ web-server/requirements.txt | 2 ++ web-ui/Dockerfile | 19 ++++++++++++++ web-ui/index.html | 33 +++++++++++++++++++++++ 9 files changed, 276 insertions(+) create mode 100644 docker-compose.yml create mode 100644 redme.txt create mode 100644 rust-compiler/Dockerfile create mode 100644 rust-compiler/main.rs create mode 100644 web-server/Dockerfile create mode 100644 web-server/app.py create mode 100644 web-server/requirements.txt create mode 100644 web-ui/Dockerfile create mode 100644 web-ui/index.html diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d96bb2a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,46 @@ +version: '3' + +services: + rabbitmq: + image: rabbitmq:3-management + container_name: rabbitmq + networks: + - rustnet + + rust-compiler: + build: + context: ./rust-compiler + dockerfile: Dockerfile + container_name: rust-compiler + networks: + - rustnet + environment: + - RABBITMQ_HOST=rabbitmq + + web-server: + build: + context: ./web-server + dockerfile: Dockerfile + container_name: web-server + ports: + - "5000:5000" + depends_on: + - rust-compiler + - rabbitmq + networks: + - rustnet + environment: + - RABBITMQ_HOST=rabbitmq + + web-ui: + build: + context: ./web-ui + dockerfile: Dockerfile + container_name: web-ui + ports: + - "80:80" + networks: + - rustnet + +networks: + rustnet: \ No newline at end of file diff --git a/redme.txt b/redme.txt new file mode 100644 index 0000000..2353749 --- /dev/null +++ b/redme.txt @@ -0,0 +1,42 @@ +你的需求是将Rust编译环境、后端Web服务器(如Flask)和前端Web UI分别放在三个不同的Docker镜像中。我们将使用Docker Compose来管理这些服务,并确保它们能够相互通信。 +项目结构 + +假设你的项目结构如下: + + + +project-root/ +├── docker-compose.yml +├── rust-compiler/ +│ ├── Dockerfile +│ └── main.rs +├── web-server/ +│ ├── Dockerfile +│ ├── app.py +│ └── requirements.txt +└── web-ui/ + ├── Dockerfile + └── index.html + + + + + + + +5. 构建并运行Docker Compose + +现在,你可以使用 docker-compose 来构建和运行整个应用: + +bash +深色版本 + +docker-compose up --build + +这样,你将有三个独立的Docker容器: + + rust-compiler:包含Rust编译环境的服务。 + web-server:包含Flask Web服务器的服务。 + web-ui:包含前端Web界面的服务。 + +用户可以通过浏览器访问 http://localhost 来使用前端Web界面,输入Rust代码,点击“Compile and Run”按钮,然后查看编译和运行的结果。后端的Flask应用会将代码发送到Rust编译环境容器进行编译,并将结果返回给前端显示。 \ No newline at end of file diff --git a/rust-compiler/Dockerfile b/rust-compiler/Dockerfile new file mode 100644 index 0000000..da1070b --- /dev/null +++ b/rust-compiler/Dockerfile @@ -0,0 +1,28 @@ +# 使用官方的Rust镜像作为基础 +FROM rust:latest + +# 设置工作目录 +WORKDIR /usr/src/rust-compiler + +# 更新源列表并安装必要的依赖 +RUN sed -i 's|http://deb.debian.org/debian|https://mirrors.tuna.tsinghua.edu.cn/debian|g' /etc/apt/sources.list && + apt-get update && + apt-get install -y --no-install-recommends curl && + rm -rf /var/lib/apt/lists/* + + + +# 复制编译服务代码 +COPY . . + +# 编译Rust程序 +RUN cargo new rust-compiler-service --bin +WORKDIR /usr/src/rust-compiler/rust-compiler-service +RUN cp /usr/src/rust-compiler/main.rs . +RUN cargo build --release + +# 暴露端口 +EXPOSE 8080 + +# 启动服务 +CMD ["./target/release/rust-compiler-service"] \ No newline at end of file diff --git a/rust-compiler/main.rs b/rust-compiler/main.rs new file mode 100644 index 0000000..d4e5146 --- /dev/null +++ b/rust-compiler/main.rs @@ -0,0 +1,52 @@ +use hyper::{Body, Request, Response, Server}; +use hyper::service::{make_service_fn, service_fn}; +use hyper::rt::run; +use std::fs::File; +use std::io::Write; +use std::process::Command; +use lapin::{options::*, types::FieldTable, Connection, ConnectionProperties, ExchangeKind, QueueDeclareOptions, QueueBindOptions}; + +async fn handle_compile(req: Request
) -> Result