Rust Web框架warp使用

2023-05-16

目录

    • 简介
    • 快速开始
    • Request和Response
        • 从path和body中获取参数
        • 从query中获取参数、设置状态码
    • 静态文件、目录
    • websocket
    • 重定向
    • tls

简介

warp是一个超级便捷、可组合、速度极快的异步Web框架。目前最新版本为v0.2.3,尚未稳定。内部主要基于hyper框架实现,同一个作者,而hyper主要基于tokio。因此hyper的大部分特性都可以在warp中得到了继承:

  • HTTP/1
  • HTTP/2
  • Asynchronous

warp的基本组成部分是filter,他们可以通过组合满足各种不同的需求,个人不太喜欢这种编程风格,warp提供以下开箱即用的功能:

  • Path routing and parameter extraction
  • Header requirements and extraction
  • Query string deserialization
  • JSON and Form bodies
  • Multipart form data
  • Static Files and Directories
  • Websockets
  • Access logging
  • Gzip, Deflate, and Brotli compression

快速开始

  1. 创建项目
cargo new warp-demo
  1. 在cargo.toml中添加依赖
[dependencies]
tokio = { version = "0.2", features = ["macros"] }
warp = "0.2"
  1. 修改main.js
use warp::Filter;

#[tokio::main]
async fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 8080))
        .await;
}
  1. 启动
cargo run
  1. 访问
    打开浏览器访问http://localhost:8080/hello/warp

Request和Response

从path和body中获取参数

#![deny(warnings)]

use serde_derive::{Deserialize, Serialize};

use warp::Filter;

#[derive(Deserialize, Serialize)]
struct Employee {
    name: String,
    rate: u32,
}

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    // POST /employees/:rate  {"name":"Sean","rate":2}
    let promote = warp::post()
        .and(warp::path("employees"))
        .and(warp::path::param::<u32>())
        // Only accept bodies smaller than 16kb...
        .and(warp::body::content_length_limit(1024 * 16))
        .and(warp::body::json())
        .map(|rate, mut employee: Employee| {
            employee.rate = rate;
            warp::reply::json(&employee)
        });

    warp::serve(promote).run(([127, 0, 0, 1], 3030)).await
}

从query中获取参数、设置状态码

cargo.toml

tokio = { version = "0.2", features = ["macros"] }
warp = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0.55"}
log = "0.4" 
pretty_env_logger = "0.3"
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use warp::{
    http::{Response, StatusCode},
    Filter,
};

#[derive(Deserialize, Serialize)]
struct MyObject {
    key1: String,
    key2: u32,
}

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    // get /example1?key=value
    // demonstrates an optional parameter.
    let example1 = warp::get()
        .and(warp::path("example1"))
        .and(warp::query::<HashMap<String, String>>())
        .map(|p: HashMap<String, String>| match p.get("key") {
            Some(key) => Response::builder().body(format!("key = {}", key)),
            None => Response::builder().body(String::from("No \"key\" param in query.")),
        });

    // get /example2?key1=value,key2=42
    // uses the query string to populate a custom object
    let example2 = warp::get()
        .and(warp::path("example2"))
        .and(warp::query::<MyObject>())
        .map(|p: MyObject| {
            Response::builder().body(format!("key1 = {}, key2 = {}", p.key1, p.key2))
        });

    let opt_query = warp::query::<MyObject>()
        .map(Some)
        .or_else(|_| async { Ok::<(Option<MyObject>,), std::convert::Infallible>((None,)) });

    // get /example3?key1=value,key2=42
    // builds on example2 but adds custom error handling
    let example3 =
        warp::get()
            .and(warp::path("example3"))
            .and(opt_query)
            .map(|p: Option<MyObject>| match p {
                Some(obj) => {
                    Response::builder().body(format!("key1 = {}, key2 = {}", obj.key1, obj.key2))
                }
                None => Response::builder()
                    .status(StatusCode::BAD_REQUEST)
                    .body(String::from("Failed to decode query param.")),
            });

    warp::serve(example1.or(example2).or(example3))
        .run(([127, 0, 0, 1], 3030))
        .await
        }

静态文件、目录

#![deny(warnings)]

use warp::Filter;

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    let readme = warp::get()
        .and(warp::path::end())
        .and(warp::fs::file("./README.md"));

    // dir already requires GET...
    let examples = warp::path("ex").and(warp::fs::dir("./examples/"));

    // GET / => README.md
    // GET /ex/... => ./examples/..
    let routes = readme.or(examples);

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

websocket

#![deny(warnings)]

use futures::{FutureExt, StreamExt};
use warp::Filter;

#[tokio::main]
async fn main() {
    pretty_env_logger::init();

    let routes = warp::path("echo")
        // The `ws()` filter will prepare the Websocket handshake.
        .and(warp::ws())
        .map(|ws: warp::ws::Ws| {
            // And then our closure will be called when it completes...
            ws.on_upgrade(|websocket| {
                // Just echo all messages back...
                let (tx, rx) = websocket.split();
                rx.forward(tx).map(|result| {
                    if let Err(e) = result {
                        eprintln!("websocket error: {:?}", e);
                    }
                })
            })
        });

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

重定向

#![deny(warnings)]
use warp::{http::Uri, Filter};

#[tokio::test]
async fn redirect_uri() {
    let over_there = warp::any().map(|| warp::redirect(Uri::from_static("/over-there")));

    let req = warp::test::request();
    let resp = req.reply(&over_there).await;

    assert_eq!(resp.status(), 301);
    assert_eq!(resp.headers()["location"], "/over-there");
}

tls

#![deny(warnings)]

// Don't copy this `cfg`, it's only needed because this file is within
// the warp repository.
#[cfg(feature = "tls")]
#[tokio::main]
async fn main() {
    use warp::Filter;

    // Match any request and return hello world!
    let routes = warp::any().map(|| "Hello, World!");

    warp::serve(routes)
        .tls()
        .cert_path("examples/tls/cert.pem")
        .key_path("examples/tls/key.rsa")
        .run(([127, 0, 0, 1], 3030))
        .await;
}

#[cfg(not(feature = "tls"))]
fn main() {
    eprintln!("Requires the `tls` feature.");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rust Web框架warp使用 的相关文章

随机推荐

  • python OCR Tesseract 训练

    Ps xff1a Tesseract识别英文和字母效果好 中文的话 xff0c 虽然有训练数据也可以识别 xff0c 但是效果不是很好 Tesseract的安装和使用 xff1a 1 首先用 pip 下载包 pip install tess
  • ffmpeg实战教程(十)ffmpeg/camera实现最近很火的视频壁纸,相机壁纸

    本篇实现一个有意思的玩意儿 xff0c 视频壁纸 xff0c 相机壁纸 这玩意好像现在还都是国外版本 xff0c 哈哈 先上图 xff1a 视频壁纸 相机壁纸 1 动态壁纸制作的知识 xff1a 每一个动态壁纸都继承自WallpaperSe
  • C/C++ 连接两个字符串

    一 xff1a C风格字符串连接 include lt iostream gt using namespace std int main const char str 61 34 hello 34 const char str2 61 34
  • 返回urls跳转后的页面地址

    string urls 61 34 http xxx xxx com SoftDown aspx SoftID 61 275781 amp fdid 61 119418725f1d4e20 34 Response Redirect urls
  • 阶乘数码(基础算法-高精度)

    P1591 阶乘数码 洛谷 计算机科学教育新生态 luogu com cn include lt bits stdc 43 43 h gt include lt iostream gt include lt unordered map gt
  • MPICH的安装与使用

    1 安装MPICH之前 xff0c 首先要在centos6 5上安装c编译器 xff0c 使用指令安装如下 xff1a yum install make 安装make程序 yum install gcc 安装GCC编译器 xff08 支持C
  • 使用cmake生成可执行文件以及开发中常用到的一些指令说明

    span class token macro property span class token directive keyword include span span class token string lt iostream gt s
  • Ubuntu18.04 CUDA10 cudaGetDeviceCount returned 35

    报错现象 我的环境是Ubuntu18 04 安装了CUDA10 运行deviceQuery报错如下 xff1a deviceQuery Starting CUDA Device Query Runtime API version CUDAR
  • Wampserver最全安装教程(一定全绿图标)

    Wampserver最全安装教程 xff08 一定全绿图标 xff09 通过以下步骤进行一定可以全绿 xff0c 一定要按照步骤来哦 xff1a 打开http wampserver aviatechno net 滚到最下面 xff0c 下载
  • WSL安装及配置

    WSL xff0c 全称Windows Subsystem for Linux xff0c 是微软为Windows 10 11集成的功能 xff0c 可以在Windows中不用虚拟机使用Linux 使用WSL首先要开启Windows功能 x
  • “invalid character ‘\x1f‘ looking for beginning of value“ 报错解决方法

    目录 问题背景 分析方法 问题原因 问题背景 这个问题来源于我在开启kubectl proxy代理的时候 xff0c 通过kubectl apply f xxx yaml向代理提交一个configMap对象 xff0c 返回了 34 Err
  • 08-1 UIControl 及其子类 UISegmentedControl 、UISlider

    1 UIControl UIControl是所有控制控件 xff08 比如UIButton UISlider UISegmentedControl等 xff09 的基类 只要跟控制有关的控件都是继承于该类 UISlider是可以响应滑动事件
  • android Settings.Secure的使用

    在Android中 xff0c 许多的系统属性都在settings应用当中进行设置的 xff0c 比如wifi 蓝牙状态 xff0c 当前本机语言 xff0c 屏幕亮度等等一些相关的系统属性值 这些数据主要是存储在数据库中 xff0c 对应
  • 关于VM一启动虚拟机电脑就重启或蓝屏的几个解决方法

    最近在刚开始学习Linux在使用VMware创建新的虚拟机时只要一点启动虚拟机电脑就直接重启了 xff0c 最开始以为是vm版本或者是Linux镜像的原因来来回回换了好几个vm和Linux xff0c 电脑重启了二三十次都没成功启动虚拟机
  • Golang + Qt5 桌面开发终极解决方案

    Golang 43 Qt5 桌面开发终极解决方案 首先要安装Qt和Golang 一 安装前准备 1 下载Go1 4版本的压缩包版本 xff0c 解压至C盘User目录下 2 安装MinGW 并配置相关环境变量 参考链接 xff1a MinG
  • Oracle snapper ASH监控工具

    Oracle snapper ASH监控工具 snapper工具是由国外技术人员 xff0c 将基于Oracle ash技术原理用来监控数据库会话的负载情况 比较适合小范围时间监控 xff0c 可以生成多个快照 xff0c 例如1小时内 x
  • Matlab之数据筛选

    Matlab功能强大 xff0c 这里介绍一些数据筛选方法 xff0c 至少让其达到Excel的数据筛选程度 一 从多维数组中取某些行或列组合为新数组 示例如下 xff1a 取某些列组成新数组 newdata span class toke
  • kurento-room的搭建教程,绝对可行

    目前网上参考的kurento room的搭建教程 xff0c 比如https blog csdn net u010602143 article details 106670864 已经跑不起了 我估计原来也跑不起 原因很简单 xff0c k
  • Python 爬取携程所有机票

    打开携程网 xff0c 查询机票 xff0c 如广州到成都 这时网址为 xff1a http flights ctrip com booking CAN CTU day 1 html DDate1 61 2018 06 15 其中 xff0
  • Rust Web框架warp使用

    目录 简介快速开始Request和Response从path和body中获取参数从query中获取参数 设置状态码 静态文件 目录websocket重定向tls 简介 warp是一个超级便捷 可组合 速度极快的异步Web框架 目前最新版本为