awpak-rs
awpak-rs is a lightweight web framework for Rust, built on top of tokio and hyper. It simplifies request handling, middleware processing, and response generation, using declarative macros for a more ergonomic development experience.
⚠️ This is an initial version of the project, and the API may change in future releases.
Features
- Declarative Macros: Define routes using
#[get],#[post],#[put],#[delete], and more. - Flexible Request Handling: Extract query parameters, request headers, cookies, request bodies, and file uploads effortlessly.
- Middleware Support: Define and chain middleware to modify requests and responses.
- Context Sharing: Use middleware to pass shared state across request processing.
- Automatic JSON Serialization: Responses and request bodies seamlessly integrate with
serde. - File Upload Support: Handle
multipart/form-datafile uploads efficiently.
Installation
To use awpak-rs, add it to your Cargo.toml:
[]
= "0.0.1"
= { = "1.0", = ["derive"] }
Getting Started
Basic Example
use *;
use ;
Run the server and test with:
Response:
Setting Up the Main Function
The #[awpak_main] macro defines the entry point of the application:
ip(optional): The IP address to bind to (default:127.0.0.1).port(optional): The port the server will listen on (default:3000).
Defining Routes
Endpoints are defined using route macros such as #[get] and #[post]:
Extracting Query Parameters
Path Variables
Extracting a User from a Path Variable
async
Handling Request and Response Headers
Extract request headers:
Modify response headers:
Working with Cookies
Retrieve request cookies:
Modify response cookies:
Middleware Support
Middlewares allow modifying incoming requests, responses, or setting shared context before or after executing an endpoint. They can:
- Modify request headers, cookies, or body.
- Modify response headers, cookies, or status codes.
- Add authentication or logging logic.
- Execute conditionally based on URL patterns, HTTP methods, or execution order.
Middlewares return:
MiddlewareResponse::Next(io): Continue to the next middleware or endpoint.MiddlewareResponse::Cancel(io): Stop execution and return a response immediately.
Using Context in Middleware
Middleware can set shared context using IO::set_context:
Endpoints can then retrieve this context:
Handling File Uploads
Extract a single uploaded file:
Extract multiple uploaded files:
Supported HTTP Methods
Awpak-rs supports the following HTTP methods:
#[get]#[post]#[put]#[delete]#[patch]#[head]#[options]#[trace]#[connect]
License
Awpak-rs is released under the MIT License.