actix-web构建RESTful API --基于内存

cargo new book_api
cd book_api

toml

[dependencies]
actix-web = "4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

rs

use actix_web::{web, App, HttpServer, Responder, HttpResponse};
use std::sync::Mutex;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Book {
    id: usize,
    title: String,
    author: String,
}

struct AppState {
    books: Mutex<HashMap<usize, Book>>,
}

#[actix_web::post("/books")]
async fn create_book(state: web::Data<AppState>, book: web::Json<Book>) -> impl Responder {
    let mut books = state.books.lock().unwrap();
    books.insert(book.id, book.into_inner());
    HttpResponse::Created().finish()
}

#[actix_web::get("/books/{id}")]
async fn get_book(state: web::Data<AppState>, web::Path(id): web::Path<usize>) -> impl Responder {
    let books = state.books.lock().unwrap();
    if let Some(book) = books.get(&id) {
        HttpResponse::Ok().json(book)
    } else {
        HttpResponse::NotFound().finish()
    }
}

#[actix_web::put("/books/{id}")]
async fn update_book(state: web::Data<AppState>, id: web::Path<usize>, book: web::Json<Book>) -> impl Responder {
    let mut books = state.books.lock().unwrap();
    if books.contains_key(&id) {
        books.insert(id.into_inner(), book.into_inner());
        HttpResponse::Ok().finish()
    } else {
        HttpResponse::NotFound().finish()
    }
}

#[actix_web::delete("/books/{id}")]
async fn delete_book(state: web::Data<AppState>, web::Path(id): web::Path<usize>) -> impl Responder {
    let mut books = state.books.lock().unwrap();
    if books.remove(&id).is_some() {
        HttpResponse::NoContent().finish()
    } else {
        HttpResponse::NotFound().finish()
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let app_state = web::Data::new(AppState {
        books: Mutex::new(HashMap::new()),
    });

    HttpServer::new(move || {
        App::new()
            .app_data(app_state.clone())
            .service(create_book)
            .service(get_book)
            .service(update_book)
            .service(delete_book)
    })
    .bind("127.0.0.1:9999")?
    .run()
    .await
}
posted @   朝阳1  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2023-11-04 Golang使用mqtt
2023-11-04 Linux如何设置证书免密登录
2023-11-04 Linux服务器之间设置ssh免密登录
2023-11-04 filebeat和logstash的简单配置
点击右上角即可分享
微信分享提示