[][src]Crate saphir

Saphir is a fully async-await http server framework for rust

The goal is to give low-level control to your web stack (as hyper does) without the time consuming task of doing everything from scratch.

Just use the prelude module, and you're ready to go!

Quick Overview

Saphir provide multiple functionality through features. To try it out without fuss, we suggest that use all the features:

saphir = { version = "2.0.0", features = ["full"] }

Then bootstrapping the server is as easy as:

use saphir::prelude::*;

struct TestController {}

#[controller]
impl TestController {
    #[get("/{var}/print")]
    async fn print_test(&self, var: String) -> (u16, String) {
        (200, var)
    }
}

async fn test_handler(mut req: Request) -> (u16, Option<String>) {
    (200, req.captures_mut().remove("variable"))
}

#[tokio::main]
async fn main() -> Result<(), SaphirError> {
    env_logger::init();

    let server = Server::builder()
        .configure_listener(|l| {
            l.interface("127.0.0.1:3000")
        })
        .configure_router(|r| {
            r.route("/{variable}/print", Method::GET, test_handler)
                .controller(TestController {})
        })
        .build();

    // Start server with
    // server.run().await
}

Saphir's Features

Even though we strongly recommend that you use at least the macro feature, Saphir will work without any of the following feature, Saphir's features don't rely on each other to work.

  • macro : Enable the #[controller] macro attribute for code generation, Recommended and active by default
  • https : Provide everything to allow Saphir server to listen an accept HTTPS traffic
  • json : Add the Json wrapper type to simplify working with json data
  • form : Add the Form wrapper type to simplify working with urlencoded data

More feature will be added in the future

Re-exports

pub use http;

Modules

body
controller

Controllers are responsible for handling requests and returning responses to the client.

cookie
error

Error definitions

extension
guard

A guard is called before the request is processed by the router and can modify the request data or stops request processing by returning a response immediately.

handler

Definition of types which can handle an http request

http_context

Context enveloping every request <-> response

macros

Saphir macro for code generation Saphir provides a proc_macro attribute and multiple function attributes.

middleware

A middleware is an object being called before the request is processed by the router, allowing to continue or stop the processing of a given request by calling / omitting next.

prelude

Contains everything you need to bootstrap your http server

request

The Http Request type

responder

Definition of type which can map to a response

response

The Http Response type

router

Router is responsible for redirecting requests to handlers.

server

Server implementation and default runtime Server is the centerpiece on saphir, it contains everything to handle request and dispatch it the proper router

utils