rust (actix_web) 項目目錄 構(gòu)建 指南

啟動文件 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 層

記得創(chuàng)建 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 層

記得創(chuàng)建 mod.rs ,并在其中 添加entity

該mod需要在main.rs中 引用

use serde::{Deserialize, Serialize};

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

有了這個基本的網(wǎng)絡(luò)架構(gòu),就可以構(gòu)建自己的項目了。

可按自己喜好進行劃分代碼結(jié)構(gòu)。

看到這里了,給個免費的贊呀。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 隨著我們的坑越來越多,越來越大,我們必須要對各種坑進行管理了。Rust為我們提供了一套坑務(wù)管理系統(tǒng),方便大家有條不...
    Jackeyzhe閱讀 708評論 0 1
  • 通用編程概念 變量與可變性 變量默認不可變,如需要改變,可在變量名前加 mut 使其可變。例如:let mut a...
    soojade閱讀 12,734評論 2 30
  • JAVA面試題 1、作用域public,private,protected,以及不寫時的區(qū)別答:區(qū)別如下:作用域 ...
    JA尐白閱讀 1,269評論 1 0
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評論 6 342
  • 上一節(jié),我們已經(jīng)實現(xiàn)了一個最小可運行版本。之所以使用Rust而不是C,是因為Rust具備了必要的抽象能力,還能獲得...
    不告訴你_閱讀 1,428評論 0 0

友情鏈接更多精彩內(nèi)容