Expand description
Puff HTTP Server
The Puff web server is powered by Axum. Build up a service using the get, post, etc methods.
PuffHandlers are functions or closures that take Axum Extractors as arguments. Puff handlers are almost
the same as Axum handlers, but they can be sync and do not have to return a future.
Sync Puff Handlers are run in a Puff context on a coroutine thread. Async Puff Handlers are not run inside of puff and behave as normal Axum Handlers. In an Async Puff Handler you must manually use the puff_context if you want to renter the puff context.
All Puff handlers must return a type compatible with [axum::response::IntoResponse].
If using an extractor, Puff handlers must also include as the final argument to their function,
a FromRequestExtractor (use _: Request to fill in a null one).
Basic Example
This example shows how to make a simple Puff web service with a variety of Request extractors and Responses.
use std::process::ExitCode;
use puff_rs::program::commands::http::ServerCommand;
use puff_rs::program::Program;
use puff_rs::errors::Result;
use puff_rs::types::text::{Text, ToText};
use puff_rs::web::server::{Request, Response, ResponseBuilder, Router, Json, body_text};
use axum::extract::Path;
use std::time::Duration;
use serde_json::{Value, json};
fn main() -> ExitCode {
// build our application with a route
let app = Router::new()
.get("/", root)
.get("/user/:id", get_user);
Program::new("my_first_app")
.about("This is my first app")
.command(ServerCommand::new(app)) // Expose the `server` command to the CLI
.run()
}
// basic handler that responds with a static string
async fn root() -> Text {
"ok".to_text()
}
// basic handler that uses an Axum extractor. We must use a FromRequest Extractor as the final argument.
async fn get_user(Path(user_id): Path<String>, _: Request) -> Json<Value> {
Json(json!({ "data": 42 }))
}
Structs
status-code in RFC 7230 et al.).