Skip to main content

Crate rustyweb

Crate rustyweb 

Source
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 async functions or closures taking a Request and returning anything that implements IntoResponse.
  • Middleware is an async function taking a Request and Next. Call next.run(req).await to continue, or return a response directly to short-circuit. Code after next.run runs once the handler has responded.
  • Routes match in registration order. :name captures 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 a serde_json::Value from 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 with ResponseExt::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() and res.download().
HeaderMap
A specialized multimap for header names and values.
Html
An HTML response.
Json
A JSON response. Serialization failures become a 500 Error.
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.
RouteChain
Several handlers for one path, from App::route or Router::route, like Express’s app.route("/users").get(..).post(..).
RouteHandle
A route that was just registered. Use with to add middleware that runs for this route only.
Router
A group of routes with its own middleware, mounted into an App (or another Router) under a path prefix — Express’s express.Router().
StaticFiles
Middleware returned by static_files.
StatusCode
An HTTP status code (status-code in RFC 9110 et al.).

Enums§

SameSite
The SameSite cookie attribute.
Value
Any JSON value. Read a body without declaring a struct: let body: Value = req.json()?; then body["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§

IntoResponse
Anything a handler can return.
ListenAddr
An address App::listen can bind to.
Middleware
Middleware: code that runs around a request, like an Express app.use function.
ResponseExt
Cookie helpers for Response, like Express’s res.cookie() and res.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().

Type Aliases§

Response
An outgoing HTTP response. Middleware can inspect and modify it freely (res.status(), res.headers_mut(), …).
Result
Result with Error as the default error type, so handlers that can fail read simply: