# actic-web-wenzhi **Repository Path**: loyalty-code/actic-web-wenzhi ## Basic Information - **Project Name**: actic-web-wenzhi - **Description**: No description available - **Primary Language**: Rust - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-28 - **Last Updated**: 2024-07-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 创建 cargo 项目 ```shell cargo new 项目名称 cd 项目名称 cargo build 编译 cargo test 测试(集成#[test]测试) Rust 由工具 rustup 安装和管理。 rustup update 来升级 Rust # windows 不行?设置rustup镜像源都失败?直接安装包再安装升级了! 卸载 Rust rustup self uninstall # 安装依赖 cargo add actix-web cargo add deadpool-postgres cargo add tokio-postgres cargo add tokio-postgres-migration # 3年前的了 cargo add env_logger cargo add log cargo add serde cargo add serde_json ``` # 包搜索地址 https://crates.io/ # Rust做游戏开发框架 bevy ```shell Rust做游戏开发框架 bevy https://arewegameyet.rs/#get-started https://blessed.rs/crates#section-graphics-subsection-game-development ``` # rust-analyzer.procMacro.attributes.enable: 启用对程序宏的支持,可以通过配置 rust-analyzer 来实现 -> 不管用,这个警告 ```shell rustup component add rust-analyzer 配置 rust-analyzer:在项目的根目录下创建或编辑 .rust-analyzer 文件,并添加以下配置: [procMacro] attributes = ["get"] 这里的 "get" 是你提到的宏的名称,你可以根据需要添加其他宏的名称。 确保 Cargo.toml 中包含必要的依赖 重启编辑器 ``` # https://rsproxy.cn/ crates.io Mirror ```shell # 设置 crates.io 镜像, 修改配置 ~/.cargo/config,已支持git协议和sparse协议,>=1.68 版本建议使用 sparse-index,速度更快 [source.crates-io] replace-with = 'rsproxy-sparse' [source.rsproxy] registry = "https://rsproxy.cn/crates.io-index" [source.rsproxy-sparse] registry = "sparse+https://rsproxy.cn/index/" [registries.rsproxy] index = "https://rsproxy.cn/crates.io-index" [net] git-fetch-with-cli = true ``` # Rustup Mirror MacOS Linux ~/.zshrc or ~/.bashrc: ```shell export RUSTUP_DIST_SERVER="https://rsproxy.cn" export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" ``` # Windows用户配置环境变量 Windows需要设置两个系统变量,分别为: RUSTUP_DIST_SERVER https://mirrors.aliyun.com/rustup/rustup RUSTUP_UPDATE_ROOT https://mirrors.aliyun.com/rustup # deadpool-postgres ```shell PS E:\Desktop\front-learn\actic-web-wenzhi> cargo run error: package `deadpool-postgres v0.14.0` cannot be built because it requires rustc 1.75 or newer, while the currently active rustc version is 1.73.0 Either upgrade to rustc 1.75 or newer, or use cargo update -p deadpool-postgres@0.14.0 --precise ver where `ver` is the latest version of `deadpool-postgres` supporting rustc 1.73.0 ``` ```shell PS E:\Desktop\front-learn\actic-web-wenzhi> rustc --version rustc 1.73.0 (cc66ad468 2023-10-03) PS E:\Desktop\front-learn\actic-web-wenzhi> ``` > rustup update stable 升级到1.80.0版本,就好了,// proc macro `main` not expanded: No proc-macros present for crate 这个警告也没了! # https://www.hello-algo.com/ 89.9K https://github.com/krahets/hello-algo # [actix-web](https://github.com/actix/examples) 例子 3.6K > Community showcase and examples of Actix Web ecosystem usage. > https://actix.rs/ https://github.com/actix/examples/tree/master/basics/hello-world ```shell # Cargo.toml [package] name = "hello-world" version = "1.0.0" edition = "2021" [dependencies] actix-web.workspace = true env_logger.workspace = true log.workspace = true ``` ```rust use actix_web::{middleware, web, App, HttpRequest, HttpServer}; async fn index(req: HttpRequest) -> &'static str { println!("REQ: {req:?}"); "Hello world!" } #[actix_web::main] async fn main() -> std::io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| { App::new() // enable logger .wrap(middleware::Logger::default()) .service(web::resource("/index.html").to(|| async { "Hello world!" })) .service(web::resource("/").to(index)) }) .bind(("127.0.0.1", 8080))? .run() .await } #[cfg(test)] mod tests { use actix_web::{body::to_bytes, dev::Service, http, test, Error}; use super::*; #[actix_web::test] async fn test_index() -> Result<(), Error> { let app = App::new().route("/", web::get().to(index)); let app = test::init_service(app).await; let req = test::TestRequest::get().uri("/").to_request(); let resp = app.call(req).await?; assert_eq!(resp.status(), http::StatusCode::OK); let response_body = resp.into_body(); assert_eq!(to_bytes(response_body).await?, r##"Hello world!"##); Ok(()) } } ``` # 运行 ```shell cargo run ``` # http://github.com/docker/awesome-compose/blob/master/react-rust-postgres # rust 版本 ```shell C:\Users\zql>rustc --version rustc 1.80.0 (051478957 2024-07-21) C:\Users\zql>rustc -V rustc 1.80.0 (051478957 2024-07-21) ```