Crate saphir

source ·
Expand description

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
  • validate-requests : Enable the #[controller] macro to generate validation code for all Json<T> and Form<T>request payloads using the validator crate.

More feature will be added in the future

Re-exports

Modules

  • Controllers are responsible for handling requests and returning responses to the client.
  • Error definitions
  • filefile
  • 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.
  • Definition of types which can handle an http request
  • Context enveloping every request <-> response
  • macrosmacro
    Saphir macro for code generation Saphir provides a proc_macro attribute and multiple function attributes.
  • 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.
  • multipartmultipart
    The async Multipart Form-Data representation
  • Contains everything you need to bootstrap your http server
  • redirectredirect
  • The Http Request type
  • Definition of type which can map to a response
  • The Http Response type
  • Router is responsible for redirecting requests to handlers.
  • Server implementation and default runtime Server is the centerpiece on saphir, it contains everything to handle request and dispatch it the proper router