Struct cataclysm::ServerBuilder
source · [−]pub struct ServerBuilder<T> { /* private fields */ }
Expand description
Builder pattern for the server structure
It is the main method for building a server and configuring certain behaviour
Implementations
sourceimpl<T: Sync + Send> ServerBuilder<T>
impl<T: Sync + Send> ServerBuilder<T>
sourcepub fn new(branch: Branch<T>) -> ServerBuilder<T>
pub fn new(branch: Branch<T>) -> ServerBuilder<T>
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();
Response::ok().body(format!("{}{}", prefix, *shared))
}
#[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, as the shared value is already inside an Arc.
sourcepub fn secret<A: AsRef<[u8]>>(self, secret: A) -> Self
pub fn secret<A: AsRef<[u8]>>(self, secret: A) -> Self
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).
sourcepub fn log_format<A: Into<String>>(self, log_string: A) -> Self
pub fn log_format<A: Into<String>>(self, log_string: A) -> Self
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)
sourcepub fn max_connections(self, n: usize) -> Self
pub fn max_connections(self, n: usize) -> Self
Sets up a maximum number of connections for the server to be dealt with
// Tree structure
let branch: Branch<()> = Branch::new("/").with(Method::Get.to(|| async {Response::ok()}));
// Now we configure the server
let server = Server::builder(branch).max_connections(10_000).build().unwrap();
sourcepub fn timeout(self, duration: Duration) -> Self
pub fn timeout(self, duration: Duration) -> Self
Sets up a custom timeout for http requests to be finished
use std::time::Duration;
// Tree structure
let branch: Branch<()> = Branch::new("/").with(Method::Get.to(|| async {Response::ok()}));
// Now we configure the server
let server = Server::builder(branch).timeout(Duration::from_millis(5_000)).build().unwrap();
sourcepub fn build(self) -> Result<Arc<Server<T>>, Error>
pub fn build(self) -> Result<Arc<Server<T>>, Error>
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
impl<T> !RefUnwindSafe for ServerBuilder<T>
impl<T> Send for ServerBuilder<T> where
T: Send + Sync,
impl<T> Sync for ServerBuilder<T> where
T: Send + Sync,
impl<T> Unpin for ServerBuilder<T>
impl<T> !UnwindSafe for ServerBuilder<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more