摘要:本文介绍了基于Actix-web框架的多个Rust后端管理系统实现方案,包括CRM客户关系管理系统、MIS管理系统和供应商管理系统。这些系统均采用模块化设计,包含数据库模型定义、路由处理、业务逻辑实现等核心模块,并支持PostgreSQL/SQLite数据库集成。文章详细展示了各系统的代码结构、Docker容器化部署方法以及Kuber***es集群配置方案,同时提供了完整的API接口示例和测试方法。此外还整理了开发过程中常用的Docker、Kuber***es和Rust操作命令,覆盖了从项目初始化到生产部署的全流程技术要点。这些实现方案具有代码简洁、性能高效的特点,适合作为企业级后台管理系统的开发参考。
目录
基于Actix-web的CMS客户关系管理系统
项目结构
Cargo.toml配置
数据库模型(models.rs)
数据库连接(db.rs)
路由处理器(handlers.rs)
主程序(main.rs)
Dockerfile配置
docker-***pose.yml配置
运行步骤
环境变量配置
使用actix-web编写MIS管理系统
基础服务搭建示例代码(src/main.rs):
数据库集成
添加sqlx依赖:
部署到Kuber***es
容器化应用
创建Dockerfile:
K8s资源配置
数据库服务配置
执行部署
使用 Actix-web 和 SQLite 编写供应商管理系统
项目初始化
数据库模型
路由处理
主程序
扩展功能
路由注册
测试接口
Docker常用操作命令
Kuber***es (k8s) 常用操作命令
Rust常用操作命令
安装与更新
项目管理
依赖管理
代码检查与格式化
文档生成
测试
交叉编译
调试工具
版本切换
环境配置
基于Actix-web的CMS客户关系管理系统
以下是一个基于Actix-web的CMS客户关系管理系统实现方案,包含Docker部署步骤。代码结构清晰,可直接运行。
项目结构
crm_system/
├── Cargo.toml
├── Dockerfile
├── docker-***pose.yml
├── src/
│ ├── main.rs
│ ├── models.rs
│ ├── handlers.rs
│ └── db.rs
Cargo.toml配置
[package]
name = "crm_system"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sqlx = { version = "0.6", features = ["postgres", "runtime-tokio-native-tls"] }
dotenv = "0.15"
数据库模型(models.rs)
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct Customer {
pub id: i32,
pub name: String,
pub email: String,
pub phone: String,
pub address: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NewCustomer {
pub name: String,
pub email: String,
pub phone: String,
pub address: String,
}
数据库连接(db.rs)
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use dotenv::dotenv;
use std::env;
pub async fn establish_connection() -> PgPool {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("Failed to create pool")
}
路由处理器(handlers.rs)
use actix_web::{web, HttpResponse, Responder};
use sqlx::PgPool;
use crate::models::{Customer, NewCustomer};
pub async fn list_customers(pool: web::Data<PgPool>) -> impl Responder {
let customers = sqlx::query_as::<_, Customer>("SELECT * FROM customers")
.fetch_all(pool.get_ref())
.await
.unwrap();
HttpResponse::Ok().json(customers)
}
pub async fn create_customer(
pool: web::Data<PgPool>,
new_customer: web::Json<NewCustomer>,
) -> impl Responder {
let customer = sqlx::query_as::<_, Customer>(
"INSERT INTO customers (name, email, phone, address) VALUES ($1, $2, $3, $4) RETURNING *",
)
.bind(&new_customer.name)
.bind(&new_customer.email)
.bind(&new_customer.phone)
.bind(&new_customer.address)
.fetch_one(pool.get_ref())
.await
.unwrap();
HttpResponse::Created().json(customer)
}
主程序(main.rs)
use actix_web::{App, HttpServer, web};
use dotenv::dotenv;
use sqlx::postgres::PgPool;
use std::env;
mod models;
mod handlers;
mod db;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv().ok();
let pool = db::establish_connection().await;
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone()))
.service(
web::scope("/api")
.route("/customers", web::get().to(handlers::list_customers))
.route("/customers", web::post().to(handlers::create_customer))
)
})
.bind(format!("0.0.0.0:{}", env::var("PORT").unwrap_or("8080".to_string())))?
.run()
.await
}
Dockerfile配置
FROM rust:1.60 as builder
WORKDIR /usr/src/crm_system
COPY . .
RUN cargo install --path .
FROM debian:buster-slim
COPY --from=builder /usr/local/cargo/bin/crm_system /usr/local/bin/crm_system
ENV PORT=8080
EXPOSE 8080
CMD ["crm_system"]
docker-***pose.yml配置
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
environment:
- DATABASE_URL=postgres://postgres:password@db/postgres
depends_on:
- db
db:
image: postgres:13
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
运行步骤
- 创建数据库表:
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(50),
address TEXT
);
- 构建并运行Docker容器:
docker-***pose up --build
- 测试API端点:
# 创建客户
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.***","phone":"1234567890","address":"123 Main St"}' \
http://localhost:8080/api/customers
# 获取客户列表
curl http://localhost:8080/api/customers
环境变量配置
创建.env文件:
DATABASE_URL=