Commit 359f724b authored by xiezhi's avatar xiezhi

Initial commit: Rust 1.89.0 template with Docker support

parents
{
"version": "1.0.0",
"name": "Rust Hello World 应用",
"tags": ["rust", "actix-web", "web", "api"],
"description": "基于 Rust 和 Actix Web 框架的简单 Web 应用",
"init_commands": ["make install"],
"start_commands": ["make run"],
"stop_commands": ["make stop"],
"mapping_ports":
{
"APP_PORT_1": 8000,
"APP_PORT_2": 8001,
"APP_PORT_3": 8002,
"APP_PORT_4": 8003
},
"deploy":
[
{
"image_name": "rust-app",
"app_access_entry": true,
"ports":
[
{
"name": "APP_PORT_1",
"main_port": true,
"inner_access_env_key": "API_URL",
"open_access": true,
"open_access_env_key": "API_DOMAIN"
},
{
"name": "APP_PORT_2",
"main_port": false,
"dns_env_key": null,
"open_access": false,
"open_access_env_key": null
}
],
"docker_file_dir": "./",
"deploy_resources":
{
"cpu_limit": 0.5,
"memory_limit": 2,
"storage_limit": 1
}
}
]
}
# 依赖文件
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# 环境变量文件
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# 构建输出
dist/
build/
out/
# 日志文件
*.log
# 运行时数据
pids
*.pid
*.seed
*.pid.lock
# 覆盖率目录
coverage/
.nyc_output
# 依赖目录
jspm_packages/
# 可选的npm缓存目录
.npm
# 可选的eslint缓存
.eslintcache
# 可选的REPL历史
.node_repl_history
# 输出目录
*.tgz
# Yarn完整性文件
.yarn-integrity
# dotenv环境变量文件
.env
# 编辑器目录和文件
.idea/
*.swp
*.swo
*~
# 操作系统生成的文件
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# 临时文件
*.tmp
*.temp
# 备份文件
*.bak
*.backup
{
"recommendations": [
"rust-lang.rust-analyzer",
"dustypomerleau.rust-syntax"
]
}
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4.3.1"
# 配置清华源镜像
[[source]]
name = "tuna"
replace-with = "tuna"
[[source.tuna]]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# 使用官方 Rust 镜像作为构建环境
FROM hb.eazytec-cloud.com/eazytec/eazydevelop-rust:ubuntu22.04-rust1.89.0-latest as builder
WORKDIR /app
# 复制 Cargo.toml
COPY Cargo.toml ./
# 创建虚拟项目来缓存依赖
RUN mkdir src && echo "fn main() {}" > src/main.rs
# 构建依赖(这一步会被缓存)
RUN cargo build --release
# 删除虚拟项目并复制源代码
RUN rm -rf src
COPY src ./src
# 构建实际应用
RUN cargo build --release
# 生产环境
FROM hb.eazytec-cloud.com/eazytec/eazydevelop-rust:ubuntu22.04-rust1.89.0-latest
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/target/release/hello_world /app/hello_world
# 设置环境变量
ENV APP_PORT_1=8000
# 暴露端口
EXPOSE $APP_PORT_1
# 使用自定义启动脚本
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
# 变量定义
APP_NAME = hello_world
RUST = cargo
APP_PORT = $(or $(APP_PORT_1),8000)
RESOURCE_PATH = $(abspath $(dir $(lastword $(MAKEFILE_LIST))))/.resource/resource_dev.json
LOG_FILE = $(abspath $(dir $(lastword $(MAKEFILE_LIST))))/.logs/app.log
# 安装命令
.PHONY: install
install:
@echo "检查 Rust 版本..."
@rustc --version
@echo "检查 Cargo 版本..."
@cargo --version
@echo "正在安装依赖..."
@cargo build --release
@echo "✅ 依赖安装完成!"
# 运行命令
.PHONY: run
run:
@echo "正在启动 Rust 应用..."
@mkdir -p $(dir $(LOG_FILE))
RESOURCE_PATH=$(RESOURCE_PATH) APP_PORT_1=$(APP_PORT) cargo run --release > $(LOG_FILE) 2>&1 &
@echo "✅ Rust 应用已启动,运行在端口 $(APP_PORT)"
# 停止命令
.PHONY: stop
stop:
@echo "正在停止 Rust 应用..."
@if command -v lsof >/dev/null 2>&1; then \
echo "使用 lsof 命令停止应用..."; \
lsof -ti:$(APP_PORT) | xargs kill -9 2>/dev/null || echo "没有找到运行在端口 $(APP_PORT) 的进程"; \
elif command -v ss >/dev/null 2>&1; then \
echo "使用 ss 命令停止应用..."; \
ss -ltnp | grep ':$(APP_PORT)' | awk '{print $7}' | sed 's/.*pid=\([0-9]*\).*/\1/' | xargs kill 2>/dev/null || echo "没有找到运行在端口 $(APP_PORT) 的进程"; \
else \
echo "❌ 错误:系统中没有找到 lsof 或 ss 命令"; \
exit 1; \
fi
@echo "✅ Rust 应用已停止"
# Rust Hello World 应用
基于 Rust 1.89.0 和 Actix Web 框架的简单 Web 应用模板。
## 项目特性
- 🦀 **Rust 1.89.0** - 使用最新的 Rust 稳定版本
- 🌐 **Actix Web 4.3.1** - 高性能的 Rust Web 框架
- 🐳 **Docker 支持** - 多阶段构建,优化镜像大小
- 🔧 **环境变量配置** - 端口可通过环境变量配置
- 📦 **清华源镜像** - 使用清华大学镜像源加速依赖下载
- 🚀 **一键部署** - 支持 Makefile 和 .eazy 配置
## 快速开始
### 环境要求
- Rust 1.89.0 或更高版本
- Cargo(Rust 包管理器)
- Docker(可选,用于容器化部署)
### 本地开发
1. **克隆项目**
```bash
git clone <项目地址>
cd rust1.89.0-template
```
2. **安装依赖**
```bash
make install
```
3. **启动应用**
```bash
make run
```
4. **访问应用**
打开浏览器访问:http://localhost:8000
5. **停止应用**
```bash
make stop
```
### 环境变量配置
| 变量名 | 默认值 | 说明 |
|--------|--------|------|
| `APP_PORT_1` | 8000 | 应用监听端口 |
### Docker 部署
1. **构建镜像**
```bash
docker build -t rust-hello-world .
```
2. **运行容器**
```bash
docker run -p 8000:8000 -e APP_PORT_1=8000 rust-hello-world
```
## 项目结构
```
rust1.89.0-template/
├── .eazy # 应用配置文件
├── .resource/
│ └── resource_dev.json # 资源配置文件
├── .vscode/ # VS Code 配置
├── src/
│ └── main.rs # 主程序
├── Cargo.toml # 依赖配置
├── Dockerfile # Docker 构建文件
├── docker-entrypoint.sh # Docker 启动脚本
├── Makefile # 构建和运行脚本
├── .gitignore # Git 忽略文件
└── README.md # 项目说明文档
```
## 开发指南
### 添加新路由
在 `src/main.rs` 中添加新的处理函数和路由:
```rust
async fn new_handler() -> impl Responder {
HttpResponse::Ok().body("New endpoint!")
}
// 在 main 函数中添加路由
.route("/new", web::get().to(new_handler))
```
### 添加依赖
在 `Cargo.toml` 的 `[dependencies]` 部分添加新的依赖:
```toml
[dependencies]
actix-web = "4.3.1"
serde = "1.0" # 新增依赖
```
### 配置说明
- **Cargo.toml**: 配置了清华源镜像,加速依赖下载
- **Dockerfile**: 使用多阶段构建,最终镜像基于 Debian Bookworm
- **Makefile**: 提供 install、run、stop 命令
- **.eazy**: 应用部署配置文件
## API 接口
### GET /
返回简单的 Hello World 消息。
**响应示例:**
```
Hello, World!
```
## 部署说明
### 使用 .eazy 配置部署
项目包含 `.eazy` 配置文件,支持一键部署:
- **端口映射**: APP_PORT_1 (8000) 作为主端口
- **资源限制**: CPU 0.5核,内存 2GB,存储 1GB
- **访问入口**: 支持外网域名访问
### 生产环境建议
1. **性能优化**: 根据实际负载调整资源限制
2. **安全配置**: 配置 HTTPS 和防火墙规则
3. **监控日志**: 集成日志收集和监控系统
4. **健康检查**: 添加健康检查端点
## 故障排除
### 常见问题
1. **端口被占用**
```bash
# 查看端口占用
lsof -i :8000
# 或使用 make stop 停止应用
```
2. **依赖下载失败**
- 检查网络连接
- 确认 Cargo.toml 中的清华源配置
3. **Docker 构建失败**
- 检查 Dockerfile 语法
- 确认基础镜像可用
## 贡献指南
1. Fork 项目
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
4. 推送到分支 (`git push origin feature/AmazingFeature`)
5. 打开 Pull Request
## 许可证
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
## 联系方式
如有问题或建议,请通过以下方式联系:
- 项目 Issues: [GitLab Issues](项目地址/-/issues)
- 邮箱: [联系邮箱]
---
**Happy Coding with Rust! 🦀**
#!/bin/sh
# 设置默认端口
APP_PORT_1=${APP_PORT_1:-8000}
echo "Starting Rust application on port $APP_PORT_1"
# 启动应用
exec /app/hello_world
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use std::env;
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello, World!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// 从环境变量获取端口,默认为 8080
let port = env::var("APP_PORT_1").unwrap_or_else(|_| "8080".to_string());
let bind_address = format!("0.0.0.0:{}", port);
println!("Server running at http://{}", bind_address);
HttpServer::new(|| {
App::new()
.route("/", web::get().to(hello))
})
.bind(&bind_address)?
.run()
.await
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment