Skip to main content

ServerApi

Attribute Macro ServerApi 

Source
#[ServerApi]
Expand description

ยง๐Ÿ”— API Server Macro

The api_server macro is a procedural macro that generates the code necessary to start an actix-web HTTP server with support for OpenAPI documentation and a health check endpoint.

The api_server macro takes the following attributes:

  • controllers_path: A comma-separated list of paths to modules containing controllers. The macro will recursively traverse the directories and generate code to register the controllers with the HTTP server.

  • openapi_title: A string used as the title of the OpenAPI documentation.

  • openapi_api_description: A string used as the description of the OpenAPI documentation.

  • database: A boolean indicating whether the microservice should enable database integration. If set to true, the macro will generate code to initialize the database connection pool using the sea_orm crate.

  • banner: A string used as the banner of the microservice. The banner is displayed in the server logs during startup.

Example of a minimal server bootstrap using this crate:

use rust_microservice::ServerApi;

#[ServerApi(
    controllers_path = "src/module, src/controllers",
    openapi_title = "๐ŸŒ Rest API Server",
    openapi_api_description = "Rest API OpenApi Documentation built with Rust ๐Ÿฆ€.",
    database = "true",
    banner = r#"
            _~^~^~_         ___    ___   ____    ____
        \) /  o o  \ (/    / _ |  / _ \ /  _/   / __/___  ____ _  __ ___  ____
          '_   -   _'     / __ | / ___/_/ /    _\ \ / -_)/ __/| |/ //! -_)/ __/
          / '-----' \    /_/ |_|/_/   /___/   /___/ \__//!_/   |___/ \__//!_/
    "#
)]
async fn start_server() -> rust_microservice::Result<(), String> {}

ยง๐Ÿ”— API Server Macro

The api_server macro is a procedural macro that generates the code necessary to start an actix-web HTTP server with support for OpenAPI documentation and a health check endpoint.

The api_server macro takes the following attributes:

  • controllers_path: A comma-separated list of paths to modules containing controllers. The macro will recursively traverse the directories and generate code to register the controllers with the HTTP server.

  • openapi_title: A string used as the title of the OpenAPI documentation.

  • openapi_api_description: A string used as the description of the OpenAPI documentation.

  • database: A boolean indicating whether the microservice should enable database integration. If set to true, the macro will generate code to initialize the database connection pool using the sea_orm crate.

  • banner: A string used as the banner of the microservice. The banner is displayed in the server logs during startup.

Example of a minimal server bootstrap using this crate:

โ“˜
use rust_microservice::ServerApi;

#[ServerApi(
    controllers_path = "src/module, src/controllers",
    openapi_title = "๐ŸŒ Rest API Server",
    openapi_api_description = "Rest API OpenApi Documentation built with Rust ๐Ÿฆ€.",
    database = "true",
    banner = r#"
            _~^~^~_         ___    ___   ____    ____
        \) /  o o  \ (/    / _ |  / _ \ /  _/   / __/___  ____ _  __ ___  ____
          '_   -   _'     / __ | / ___/_/ /    _\ \ / -_)/ __/| |/ //! -_)/ __/
          / '-----' \    /_/ |_|/_/   /___/   /___/ \__//!_/   |___/ \__//!_/
    "#
)]
async fn start_server() -> rust_microservice::Result<(), String> {}