Giter Club home page Giter Club logo

axum-server-starter's Introduction

Axum Starter

Github Crates.io License

Why axum-starter

With the growing of the server functions, the code which prepare multiply infrastructures for the server in the main become more and more complex.
For example, I need connect to Mysql and Redis, start MessageQuery , start GracefulShutdown and so on.
In other to simplify the start up code with my server project, there comes the axum-starter

Safety

the outer attribute #![forbid(unsafe_code)] enable

Simple Example

The following example using axum-starter starting a web server which server on http://127.0.0.1:5050

It can do

  1. show info before launch (with logger feature)
  2. using simple_logger and adding TraceLayer as logger middleware
  3. request http://127.0.0.1:5050/greet/{name} will respond greet with your name
use axum::{extract::Path, routing::get};
use axum_starter::{prepare, router::Route, ServerPrepare};
use config::Conf;
use tower_http::trace::TraceLayer;

#[tokio::main]
async fn main() {
    start().await;
}

async fn start() {
    ServerPrepare::with_config(Conf::default())
        .init_logger()
        .expect("Init Logger Error")
        .prepare_route(GreetRoute)
        .layer(TraceLayer::new_for_http())
        .no_state()
        .prepare_start()
        .await
        .expect("Prepare for Start Error")
        .launch()
        .await
        .expect("Server Error");
}

mod config {
    use std::net::Ipv4Addr;

    use axum_starter::{Configure, Provider};
    use log::LevelFilter;
    use log::SetLoggerError;
    use simple_logger::SimpleLogger;

    // prepare the init configure
    #[derive(Debug, Default, Provider, Configure)]
    #[conf(
        address(func(
            path = "||(Ipv4Addr::LOCALHOST, 5050)",
            ty = "(Ipv4Addr, u16)",
            associate,
        )),
        logger(
            func = "||SimpleLogger::new().with_level(LevelFilter::Debug).init()",
            error = "SetLoggerError",
            associate,
        ),
        server
    )]
    pub(super) struct Conf {}
}

async fn greet(Path(name): Path<String>) -> String {
    format!("Welcome {name} !")
}

#[prepare(GreetRoute)]
fn greet_route<S, B>() -> Route<S, B>
where
    B: http_body::Body + Send + 'static,
    S: Clone + Send + Sync + 'static,
{
    Route::new("/greet/:name", get(greet))
}

Core Concept

Each task before starting the server call Prepare. Each Prepare will Return a PreparedEffect for ServerPrepare to apply each prepare's effect on the server. Finally, all Prepare are done and the server can be launch

Prepare trait

the trait define the prepare task, after prepare down, it return a PreparedEffect

PreparedEffect trait family

the trait family will apply multiply effect on the server. include the following

Concurrently or Serially

Prepares will run one by one in default, in another word, they running serially, if you want run some Prepares concurrently, you can call ServerPrepare::prepare_concurrent, to give a group of Prepares running concurrently

Set Middleware

if you want to adding a middleware on the root of server Router, using ServerPrepare::layer then giving the Layer

or using PrepareMiddlewareEffect apply middleware in Prepare

axum-server-starter's People

Contributors

goodjooy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.