rust (actix_web) 项目目录 构建 指南

启动文件 main.rs

use controller;//引入的本地文件
use actix_web::{middleware, web, App, HttpServer};

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();
    HttpServer::new(||
        {
            App::new()
                .wrap(middleware::Logger::default())
                .route("/psot",web::post().to(controller::post))//post请求
                .route("/get",web::get().to(controller::get))//get请求
        })
        .bind(":100")?
        .run()
        .await
}

controller 层

记得创建 mod.rs ,并在其中 添加controller

该mod需要在main.rs中 引用

use actix_web::{Result, HttpResponse, web, HttpRequest};
use crate::entity::{PageList};

pub async fn post(json: web::Json<PageList>) -> Result<HttpResponse> {
    Ok(HttpResponse::Ok().body(format!("name:{}",json.into_inner().name)))
}
pub async fn get() -> Result<HttpResponse> {
    Ok(HttpResponse::Ok().body("请求成功!"))
}

entity 层

记得创建 mod.rs ,并在其中 添加entity

该mod需要在main.rs中 引用

use serde::{Deserialize, Serialize};

# [derive(Serialize,Deserialize)]
pub struct PageList {
    pub name: String,
}

有了这个基本的网络架构,就可以构建自己的项目了。

可按自己喜好进行划分代码结构。

看到这里了,给个免费的赞呀。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 随着我们的坑越来越多,越来越大,我们必须要对各种坑进行管理了。Rust为我们提供了一套坑务管理系统,方便大家有条不...
    Jackeyzhe阅读 663评论 0 1
  • 通用编程概念 变量与可变性 变量默认不可变,如需要改变,可在变量名前加 mut 使其可变。例如:let mut a...
    soojade阅读 12,611评论 2 30
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,191评论 1 0
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,989评论 6 342
  • 上一节,我们已经实现了一个最小可运行版本。之所以使用Rust而不是C,是因为Rust具备了必要的抽象能力,还能获得...
    不告诉你_阅读 1,335评论 0 0