Struct cataclysm::ServerBuilder[][src]

pub struct ServerBuilder<T> { /* fields omitted */ }
Expand description

Builder pattern for the server structure

It is the main method for building a server and configuring certain behaviour

Implementations

Creates a new server from a given branch

let branch: Branch<()> = Branch::new("/").with(Method::Get.to(|| async {Response::ok().body("Ok!")}));
let mut server_builder = ServerBuilder::new(branch);
// ...

Declare some information to be shared with the Shared extractor

use cataclysm::{Server, Branch, Shared, http::{Response, Method, Path}};
 
// Receives a string, and concatenates the shared suffix
async fn index(path: Path<(String,)>, shared: Shared<String>) -> Response {
    let (prefix,) = path.into_inner();
    let suffix = shared.into_inner();
    Response::ok().body(format!("{}{}", prefix, suffix))
}
 
#[tokio::main]
async fn main() {
    // We create our tree structure
    let branch = Branch::new("/{:prefix}").with(Method::Get.to(index));
    // We create a server with the given tree structure
    let server = Server::builder(branch).share("!!!".into()).build().unwrap();
    // And we launch it on the following address
    server.run("127.0.0.1:8000").await.unwrap();
}

If you intend to share a mutable variable, consider using rust’s Mutex, ad the shared value is already inside an Arc.

Sets a custom Key for cookie signature

use cataclysm::{Server, Session, Branch, Shared, http::{Response, Method, Path}};
 
async fn index(session: Session) -> Response {
    // the session will be empty if the signature was invalid
    // ... do something with the session
    // apply changes to response
    session.apply(Response::ok())
}
 
#[tokio::main]
async fn main() {
    // We create our tree structure
    let branch: Branch<()> = Branch::new("/").with(Method::Get.to(index));
    // We create a server with the given tree structure
    let server = Server::builder(branch).secret("very secret").build().unwrap();
    // And we launch it on the following address
    server.run("127.0.0.1:8000").await.unwrap();
}

If no secret is provided, a random key will be used (generated by ring).

Sets a log string, to log information per call

// Tree structure
let branch: Branch<()> = Branch::new("/").with(Method::Get.to(|| async {Response::ok()}));
// Now we configure the server
let server = Server::builder(branch).log_format("[%M %P] %S, from %A").build().unwrap();

The list of available format elements are the following

  • %M: Method from the request
  • %P: Path from the request
  • %S: Status from the response
  • %A: Socket address and port from the connection (more data to be added soon)

Builds the server

use cataclysm::{Server, Branch, Shared, http::{Response, Method, Path}};
 
// Receives a string, and concatenates the shared suffix
async fn index() -> Response {
    Response::ok().body("Hello")
}
 
#[tokio::main]
async fn main() {
    // We create our tree structure
    let branch: Branch<()> = Branch::new("/").with(Method::Get.to(index));
    // We create a server with the given tree structure
    let server = Server::builder(branch).build().unwrap();
    // And we launch it on the following address
    server.run("127.0.0.1:8000").await.unwrap();
}

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.