Crate rwf

Source
Expand description

Rwf is a comprehensive framework for building web applications in Rust. Written using the classic MVC pattern (model-view-controller), Rwf comes standard with everything you need to easily build fast and secure web apps.

This documentation serves primarily as a reference for methods and types provided by this and rwf_macros crates. For user guides, refer to the documentation here.

§Getting started

Rwf is a Rust library built on top of Tokio, and can be added to any binary or library Rust project:

cargo add rwf
cargo add tokio@1 --features full

Rwf has many types and traits that make it ergonomic. You can include them all with just one import:

use rwf::prelude::*;

While not required, this makes things simpler.

§Controllers

Rwf is an MVC framework, so Controllers are fundamental to serving HTTP requests. Defining controllers requires imlementing the controller::Controller trait for a struct:

use rwf::prelude::*;

#[derive(Default)]
struct Index;

#[rwf::async_trait]
impl Controller for Index {
    async fn handle(&self, request: &Request) -> Result<Response, Error> {
        Ok(Response::new().html("<h1>Hello from Rwf!</h1>"))
    }
}

Most Rwf traits are asynchronous and use the async_trait crate to make it user-friendly.

§HTTP server

Launching the Rwf HTTP server requires mapping routes to controllers, and can be done at application startup:

use rwf::http::Server;

let server = Server::new(vec![
    route!("/" => Index),
]);

With all the routes mapped to controllers, you can launch the server from anywhere in your app. Typically though, this is done from the main function:

use rwf::http::{Server, self};

#[tokio::main]
async fn main() -> Result<(), http::Error> {
    Server::new(vec![
        route!("/" => Index),
    ])
    .launch("0.0.0.0:8000")
    .await
}

Re-exports§

pub use rwf_macros as macros;
pub use serde;
pub use tokio;
pub use tokio_postgres;

Modules§

analytics
Analytics around aplication usage.
colors
Wrapper around colored::Colorize to conditionally use colors when the terminal is TTY.
comms
Communication channels between clients and servers.
config
Server configuration handler.
controller
HTTP controllers, the C in MVC.
crypto
Cryptographic primitives, wrapped in a simple interface.
error
Global error type.
hmr
Hot reload used for local development.
http
HTTP protocol.
job
Asynchronous background job queue.
lock
Distributed locking primitives.
logging
Wrapper around tracing_subscriber for logging.
model
Object-relational mapper (ORM), the M in MVC.
prelude
A collection of types, methods and macros which when imported make Rwf development ergonomic and easy.
view
Dynamic templates and views, the V in MVC.

Functions§

capitalize
Convert the first letter of the stirng to uppercase lettering.
pascal_case
Convert string to PascalCase (often confused with camelCase).
peer_addr
Extract the first socket address from a string.
safe_html
Remove unsafe characters from a string printed inside an HTML template.
snake_case
Convert text to snake_case.
title_case
Convert string to title case.

Attribute Macros§

async_trait
Wrapper around async traits to make them easy to use.