Expand description
A minimal, Express-style web framework: simple routing, simple middleware.
use rustyweb::prelude::*;
fn main() {
let mut app = App::new();
app.middleware(logger);
app.get("/", |_req| async { "Hello World!" });
app.get("/users/:id", |req| async move {
format!("user {}", req.param("id"))
});
app.get("/teapot", |_req| async { (StatusCode::IM_A_TEAPOT, "short and stout") });
app.listen(3000); // http://localhost:3000
}- Handlers are
asyncfunctions or closures taking aRequestand returning anything that implementsIntoResponse. - Middleware is an
asyncfunction taking aRequestandNext. Callnext.run(req).awaitto continue, or return a response directly to short-circuit. Code afternext.runruns once the handler has responded. - Routes match in registration order.
:namecaptures a segment, a trailing*captures the rest of the path.
Re-exports§
pub use self;
Modules§
- header
- HTTP header types
- prelude
- Everything most apps need, in one import:
use rustyweb::prelude::*; - upgrade
- Connection upgrades, the building block for WebSocket and other protocol packages.
Macros§
- json
- Build JSON inline, like a JavaScript object literal:
Json(json!({ "ok": true, "items": [1, 2, 3] })). Construct aserde_json::Valuefrom a JSON literal.
Structs§
- App
- An application: routes, middleware and a server.
- Body
- A response body: either a single buffered chunk or a stream of chunks.
- Cookie
- A cookie to send to the browser, like the options to Express’s
res.cookie(). Set it on a response withResponseExt::with_cookie. - Error
- An HTTP error: a status code and a plain-text message.
- File
- A file to send as a response, streamed from disk. Like Express’s
res.sendFile()andres.download(). - Header
Map - A specialized multimap for header names and values.
- Html
- An HTML response.
- Json
- A JSON response. Serialization failures become a
500Error. - Method
- The Request Method (VERB)
- Next
- The rest of the middleware chain, ending in the route handler.
- Redirect
- A redirect response.
- Request
- An incoming HTTP request with its body fully buffered.
- Route
Chain - Several handlers for one path, from
App::routeorRouter::route, like Express’sapp.route("/users").get(..).post(..). - Route
Handle - A route that was just registered. Use
withto add middleware that runs for this route only. - Router
- A group of routes with its own middleware, mounted into an
App(or anotherRouter) under a path prefix — Express’sexpress.Router(). - Static
Files - Middleware returned by
static_files. - Status
Code - An HTTP status code (
status-codein RFC 9110 et al.).
Enums§
- Same
Site - The
SameSitecookie attribute. - Value
- Any JSON value. Read a body without declaring a struct:
let body: Value = req.json()?;thenbody["name"]. Represents any valid JSON value.
Constants§
- DEFAULT_
BODY_ LIMIT - Default maximum request body size: 1 MiB.
- DEFAULT_
BODY_ TIMEOUT - Default time allowed for receiving a request body: 60 seconds.
Traits§
- Into
Response - Anything a handler can return.
- Listen
Addr - An address
App::listencan bind to. - Middleware
- Middleware: code that runs around a request, like an Express
app.usefunction. - Response
Ext - Cookie helpers for
Response, like Express’sres.cookie()andres.clearCookie(). Included in the prelude.
Functions§
- logger
- Request-logging middleware, like Express’s
morgan("dev"). - static_
files - Serves files from a folder, like Express’s
express.static().