3 releases

0.1.2 Apr 16, 2024
0.1.1 Apr 14, 2024
0.1.0 Apr 14, 2024

#1955 in Network programming

MIT license

67KB
1K SLoC

Contributors Forks Stargazers Issues

        nano-rs is a lightweight, non-invasive, convention-over-configuration Rust Web service component, aimed at providing a fast and efficient development experience. By reducing the burden of configuration, it allows you to focus more on implementing business logic.


  • Lightweight: Ensures fast startup and low resource consumption through a streamlined design.
  • Non-invasive: The framework's design allows for seamless integration into existing Rust projects without fear of interfering with business logic.
  • Convention Over Configuration: The framework adopts an "intelligent defaults" approach, providing pre-set configurations for common scenarios. This means you can get started with little to no configuration, yet it still offers ample configuration options to suit specific needs ensuring ultimate flexibility and extensibility.
  • Business Focused: Our goal is to simplify the development process of Web services. By reducing the tedious and miscellaneous configuration tasks, you can concentrate more on implementing the business logic, thus improving the efficiency and quality of project development.
Table of Contents
  1. Environment Requirements
  2. Installation
  3. Quick Start
  4. Roadmap
  5. License
  6. Contact
  7. Acknowledgments

Environment Requirements

MSRV >= 1.66

Installation

  cargo add nano-rs

Quick Start

Axum

  • Add build dependencies
[build-dependencies]
nano-rs = "0.1.0"
nano-rs-build = "0.1.0"
  • Add build.rs
use std::error::Error;
use nano_rs_build::core::NanoBuilder;
use nano_rs::axum::gen::gen_route::AxumGenRoute;

fn main() -> Result<(), Box<dyn Error>> {
    NanoBuilder::new(None).gen_api_route(AxumGenRoute::new());
    Ok(())
}
  • Add the configuration file to your desired directory (in the example, it is placed in etc/config.yaml)
port: 8888
name: example
host: 127.0.0.1
  • Write your API code anywhere in project with marco (for example, under api/pet), for macros, please refer to example (documentation coming soon...)
#[get(path = "/store/name", layers = ["crate::layers::auth::auth_token1"])]
pub async fn get_store_name() -> Result<RestResp<String>, ServerError> {
    biz_ok("Doggy Store".to_string())
}
  • Run build once (only needed for the project's first compilation)
cargo build
  • Then you will get file named routes.rs in your src/.
  • Do not edit routes.rs as it will be overwritten every time you build.
  • Edit main.rs. (refer to the project structure in the example)
use axum::Router;
use nano_rs::axum::start::run;
use nano_rs::config::init_config_with_cli;
use nano_rs::config::rest::RestConfig;
use crate::routes::get_routes;

mod routes;
mod layers;
mod api;

#[tokio::main]
async fn main() {
    let rest_config = init_config_with_cli::<RestConfig>();
    let _guard = nano_rs::tracing::init_tracing(&rest_config);
    let service_context = ServiceContext {
        rest_config: rest_config.clone(),
    };
    let app = Router::new().nest(
        rest_config.base_path.as_str(),
        get_routes(service_context.clone(), rest_config.clone()),
    );
    run(app, rest_config).await
}

#[derive(Clone)]
pub struct ServiceContext {
    pub rest_config: RestConfig,
}
  • Run your web application
cargo run -- --config etc/config.yaml
  • After this, you only need to focus on writing your business logic code; nano-rs will automatically generate routes and register them with axum, allowing you to concentrate solely on implementing business logic.

Roadmap

  • Auto-generate Axum framework routes
  • Default configuration for Tracing framework
  • Preset common web service configuration (managed via yaml)
  • Auto-generate OpenApi

For a full list of proposed features (and known issues), please see the open issues.

License

Distributed under the MIT License. For more information, see LICENSE.txt.

Contact

Acknowledgments

Dependencies

~34–51MB
~835K SLoC