warp_subdomain

Crates.iowarp_subdomain
lib.rswarp_subdomain
version1.2.0
sourcesrc
created_at2022-08-09 08:29:01.052397
updated_at2022-08-10 07:43:33.736986
descriptionA simple subdomain parser for Warp web server framework with nano second processing time.
homepage
repositoryhttps://github.com/mochamadsatria/warp_subdomain
max_upload_size
id641640
size6,847
(mochamadsatria)

documentation

README

warp_subdomain

A simple subdomain parser middleware for Warp web server framework with nano second processing time. 🚀🚀

Usage

... warp route
.and(warp_subdomain::with_subdomain)`
... route handler

Example

use std::collections::HashMap;
use std::sync::Arc;

use warp::Filter;

use warp_subdomain::with_subdomain;

async fn query(
    _query: HashMap<String, String>,
    subdomain: Arc<Vec<String>>,
) -> Result<impl warp::Reply, warp::Rejection> {
    // get last subdomain
    let subdomain_0 = subdomain.get(0);

    Ok(warp::reply::with_status(
        subdomain_0.unwrap().to_string(),
        warp::http::StatusCode::FOUND,
    ))
}

#[tokio::main]
async fn main() {
    let route = warp::path!("home")
        .and(warp::get())
        .and(warp::query::<HashMap<String, String>>())
        .and(with_subdomain())
        .and_then(query);

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

/// If host eg. super-alloy.api.cilen.com.
/// This middleware will return
vec!["super-alloy", "api"]
/// This middleware also works for localhost that have port in host header.
/// eg. api.localhost:3999

Note

This middlewares will return value with type Arc<Vec<String>>

Misc

Commit count: 10

cargo fmt