29 releases

0.1.28 Nov 25, 2022
0.1.27 Nov 25, 2022
0.1.25 Oct 6, 2022
0.1.24 Aug 25, 2022
0.1.6 May 31, 2022

#559 in HTTP server

27 downloads per month

MIT license

32KB
756 lines

dev_api

A set of pre-configured modules for Rust web APIs. Currently in early stage development.

Purpose

We want to open source how we do backend development in Rust and offer a fast and simple way to get started writing web APIs without locking you in into another custom framework.

Example

Note: given how early stage this project is, the example is not complete. You need to install actix-web and create your own configs and dependencies that you want to inject into app_data.

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dev_api::tracing::init("my-service".to_string()).expect("Failed to initialize tracer.");
    // All logs should be wrapped in a span. This is automatically done for each controller.
    tracing::info_span!("main:server_starting").in_scope(|| {
        tracing::info!("Starting server...");
    });



    let server = HttpServer::new(move || {
        // The configure function must be a ServiceConfig factory function: https://docs.rs/actix-web/latest/actix_web/web/struct.ServiceConfig.html
        let configs: Vec<fn(&mut web::ServiceConfig)> = vec![
            users::configure,
            products::configure
        ];

        // dev_api will mount your services, configure the app, and send the app back so you can extend it further.
        let app = dev_api::http::new(configs);

        // Extend the app with your own dependencies.
        app
            .app_data(web::Data::new(jwt.clone()))
            .app_data(web::Data::new(users_repo.clone()))
            .app_data(web::Data::new(products_repo.clone()))
    })
    .bind(("0.0.0.0", 8080))?
    .run();

    tracing::info_span!("main:server_started").in_scope(|| {
        tracing::info!("Server started!");
    });

    server.await

Please look at the source code for more information. src/http.rs will show how we setup the server app.

When it works, you can verify it by going to localhost:8080 in your browser. You should get back an empty 200 success response in your network tab.

Dependencies

~39–55MB
~1M SLoC