actix-web 是 Rust 生态中的最为优秀的 web 框架之一,具有类型安全、功能丰富、扩展性强,以及速度极快的诸多优点。

让我们通过 actix-web 的典型代码,来对其做一个整体认知。

use actix_web::{web, App, HttpRequest, HttpServer, Responder};

async fn greet(req: HttpRequest) -> impl Responder {

let name = req.match_info().get("name").unwrap_or("World");

format!("Hello {}!", &name)

}

#[actix_web::main]

async fn main() -> std::io::Result {

HttpServer::new(|| {

App::new()

.route("/", web::get().to(greet))

.route("/{name}", web::get().to(greet))

})

.bind("127.0.0.1:8080")?

.run()

.await

}

忘掉字符串类型的对象吧,从请求到响应,所有的数据信息都有类型。

actix 提供了诸多开箱即用的功能和特性,如 HTTP/2、日志记录等。

轻松创建自定义库,任何 actix 应用程序都可以无缝集成。

Actix 具有极快的速度,请参见 techempower 性能基准测试。

actix 中的处理函数可以返回大量实现了 Responder trait 的对象,这使得从诸多 API 返回一致的响应变得轻而易举。

#![allow(unused)]fn main() {#[derive(Serialize)]

struct Measurement {

temperature: f32,

}

async fn hello_world() -> impl Responder {

"Hello World!"

}

async fn current_temperature() -> impl Responder {

web::Json(Measurement { temperature: 42.3 })

}

}

actix 自实现了一个强大的提取器系统,可以从传入的 HTTP 请求中提取数据,并将其传递给视图函数。这不仅有助于实现一个简捷的 API,而且还意味着你的视图函数可以是同步代码,但仍然受益于异步 IO 处理。

#![allow(unused)]fn main() {#[derive(Deserialize, Serialize)]

struct Event {

id: Option,

timestamp: f64,

kind: String,

tags: Vec,

}

async fn capture_event(evt: web::Json) -> impl Responder {

let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);

format!("got event {}", new_event.id.unwrap())

}

}

处理 multipart/urlencoded 表单数据很容易。只需定义一个可以反序列化的结构,actix 将处理其余部分。

#![allow(unused)]fn main() {#[derive(Deserialize)]

struct Register {

username: String,

country: String,

}

async fn register(form: web::Form) -> impl Responder {

format!("Hello {} from {}!", form.username, form.country)

}

}

actix 具备 URL 路由系统,可以匹配 URL 并调用各个 handler。为了获得额外的灵活性,可以使用作用域。

#![allow(unused)]fn main() {#[get("/")]

async fn index(_req: HttpRequest) -> impl Responder {

"Hello from the index page!"

}

async fn hello(path: web::Path) -> impl Responder {

format!("Hello {}!", &path)

}

let app = App::new()

.service(index)

.route("/{name}", web::get().to(hello));

}

Rust actix aiohttp_介绍 - actix-web 中文文档 - Rust-Web 开发指南相关推荐

  1. SpringBoot中文文档 SpringBoot中文参考指南 SpringBoot中文参考文档 springboot中文文档 springboot中文

    SpringBoot中文文档 SpringBoot中文参考指南 SpringBoot中文参考文档 springboot中文文档 springboot中文 SpringBoot中文文档 SpringBo ...

  2. unity3d api 中文文档_unity3D游戏开发工程师完整简历范文

    基本信息 姓名:七分简历 年龄:23岁 电话:131****7089 邮箱:689262****@qq.com 经验:1年 意向:unity3D游戏开发工程师 教育背景 时间:2011-09 - 20 ...

  3. [Eigen中文文档] 稀疏矩阵快速参考指南

    文档总目录 本文目录 稀疏矩阵初始化 构造 重置大小/预分配内存空间 赋值 逐元素插入 批量插入 指定位置或随机插入 矩阵属性 算术运算 加减 标量积 稀疏矩阵乘积 转置/伴随 排列 组件级操作 其他 ...

  4. windows api中文文档_Web服务开发:Spring集成Swagger,3步自动生成API文档

    目录: 1,Spring Boot集成Swagger 2,Swagger接口文档页面 3,常见问题和解决方法 在Sping开发REST接口服务时,API文档是不可缺少的一个重要部分.Swagger框架 ...

  5. PhpSpreadsheet中文文档 | 基础Spreadsheet开发使用示例

    · 1. 实例化Spreadsheet对象 <?phpnamespace app// 给类文件的命名空间起个别名use PhpOffice\PhpSpreadsheet\Spreadsheet; ...

  6. dash html中文文档,Dash Docset 创建指南

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? Dash 是 Macos 上一款热门的API查询工具,可以用来查询很多常用语言/框架的官方文档.Windows 平台有类 ...

  7. golang roadrunner中文文档(一)基础介绍

    2021年5月24日14:34:05 golang roadrunner中文文档(一)基础介绍 golang roadrunner中文文档(二)PHP Workers golang roadrunne ...

  8. Spark 2.0.2 中文文档 | 片刻 ApacheCN(Apache中文网)

    ApacheCN(Apache中文网)- 关于我们 : http://www.apache.wiki/pages/viewpage.action?pageId=2887249 ApacheCN(Apa ...

  9. react中文文档、英文文档及JavaScript相关文档及web前端相关资料

    一. react中文文档 https://doc.react-china.org 二. react英文文档 https://reactjs.org 三.react Github https://git ...

  10. springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列

    源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...

最新文章

  1. inner join on, left join on, right join on要详细点的介绍?内连接,左外连接,右外连接。...
  2. 基于Springboot实现企业人事管理系统
  3. 完美世界第二题:模拟读数字
  4. list转为 数组[] 形式
  5. linux 用journalctl --vacuum-size=10M命令清理/var/log/journal目录下的日志文件
  6. Thread 和 Runnable创建新线程的区别,Runnable可以共享线程类的实例属性
  7. dabeicun 2013源码下载
  8. 【WebGIS bug】WARNING: Too many active WebGL contexts. Oldest context will be lost.
  9. Sqlserver-循环执行sql语句
  10. 手机版支付宝转账截图生成器Q574651623
  11. HDU 1429 bfs 状态压缩
  12. 最小二乘估计与卡尔曼滤波公式推导
  13. PHP WEB程序设计信息表,WEB程序设计(PHP)2020知到答案全套
  14. 抖音小店在哪里登录?抖音店怎么运营?
  15. 《Human-Level Artificial Intelligence? Be Serious!》论文阅读报告
  16. 数据库优化思路 oracle,自己几年前整理的数据库优化技术方案
  17. 2022年证券从业资格证券市场基本法律法规考试多选题专练及答案
  18. Hexo博客配置优化
  19. 20X29 FCPX插件50种可爱流行手绘图形MG元素包 Hand Painted
  20. 奋斗中最美坐标--中国女企协与清华社科院交流践行人类命运共同体

热门文章

  1. 常用电脑硬件技术术语集锦
  2. java中repository_java中的 repository什么意思?
  3. 百度地图api,第一次定位成功,后面505错误
  4. 苍蝇的下场——金庸人物之招式应对
  5. DevOps到底是什么意思?看完这篇不要再问我了
  6. 2021.02.04——用爬虫爬取nga ow区1-10页的帖子并生成词云图
  7. 练习_20220305
  8. windows 运维工具
  9. 14. 查询表sc——查询学生201215121选修课程的总学分数。
  10. 基姆拉尔森计算公式(根据日期判断星期几)