Crates.io | nidrs |
lib.rs | nidrs |
version | 0.2.0 |
source | src |
created_at | 2024-03-30 09:43:51.770846 |
updated_at | 2024-09-25 15:31:00.656544 |
description | Nidrs is a web framework based on axum and tokio. |
homepage | https://github.com/nidrs/nidrs.git |
repository | https://github.com/nidrs/nidrs.git |
max_upload_size | |
id | 1190981 |
size | 50,843 |
Tributing the NestJS framework, Nidrs is a Rust-based enterprise-level modular development framework that draws inspiration from NestJS ideology while being developed and designed on Axum.
Nidrs provides a plug-and-play application architecture enabling developers and teams to effortlessly create highly testable, scalable, loosely coupled, and maintainable applications.
NestJS is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (but still allows developers to write code in pure JavaScript), and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
use std::{collections::HashMap, sync::Arc};
use axum::{extract::{Query, State}, http::{version, StatusCode}, Json};
use nidrs::{throw, version, Exception, Inject, StateCtx};
use nidrs_macro::{controller, get, meta, post, uses};
use crate::{shared::fn_test::fn_test, AppError, AppResult};
use super::{dto::{Status}, service::AppService};
// #[uses(LogInterceptor)]
#[version("v1")]
#[meta(role = "admin", auth = "true")]
#[meta(test = true)]
#[controller("/app")]
pub struct AppController {
app_service: Inject<AppService>,
}
impl AppController {
#[meta(arr = ["user"])]
// #[uses(LogInterceptor)]
#[version("v2")]
#[get("/hello")]
pub async fn get_hello_world(&self, Query(q): Query<HashMap<String, String>>) -> AppResult<Status> {
println!("Query {:?}", q);
// fn_test()?;
Ok(Status { db: "ok".to_string(), redis: "ok".to_string() })
}
// #[uses(LogInterceptor)]
#[get("/hello2")]
pub async fn get_hello_world2(&self, Query(q): Query<HashMap<String, String>>) -> AppResult<String> {
println!("Query {:?}", q);
Ok(self.app_service.get_hello_world())
}
#[uses(LogInterceptor)]
#[post("/hello")]
pub async fn post_hello_world(&self, Query(q): Query<HashMap<String, String>>, Json(j): Json<serde_json::Value>) -> AppResult<String> {
println!("Query {:?}", q);
println!("Json {:?}", j);
Ok("Hello, World2!".to_string())
}
}
use nidrs::Inject;
use nidrs_macro::{injectable, on_module_init};
use crate::user::service::UserService;
#[injectable()]
pub struct AppService{
user_service: Inject<UserService>
}
impl AppService {
pub fn get_hello_world(&self) -> String {
self.user_service.get_hello_world()
}
pub fn get_hello_world2(&self) -> String {
"Hello, nidrs2xx333!".to_string()
}
}
use nidrs::default_uses;
use nidrs_macro::module;
pub mod controller;
pub mod dto;
pub mod exception;
pub mod service;
use crate::modules::conf::ConfModule;
use crate::modules::conf::ConfOptions;
use crate::modules::log::LogModule;
use crate::modules::user::UserModule;
use controller::AppController;
use service::AppService;
#[default_uses(LogInterceptor)]
#[module({
imports: [
ConfModule::for_root(ConfOptions{
log_level: "info".to_string(),
}),
LogModule,
UserModule,
],
controllers: [AppController],
services: [AppService],
exports: [AppService],
})]
pub struct AppModule;
mod app;
mod conf;
mod user;
mod log;
mod shared;
pub use nidrs::AppResult;
pub use nidrs::AppError;
#[nidrs::main]
fn main() {
let app = nidrs::NidrsFactory::create(app::AppModule);
let app = app.default_prefix("/api/{version}");
let app = app.default_version("v1");
app.listen(3000).block();
}
Run Example:
git clone https://github.com/nidrs/nidrs.git
cd nidrs/example
cargo run
Launch Log:
[nidrs] Registering module AppModule.
[nidrs] Registering interceptor LogInterceptor.
[nidrs] Registering controller AppController.
[nidrs] Registering router 'GET /api/v1/app/hello2'.
[nidrs] Registering router 'POST /api/v1/app/hello'.
[nidrs] Registering router 'GET /api/v2/app/hello'.
[nidrs] Registering service AppService.
[nidrs] Registering dyn service ConfOptions.
[nidrs] Registering module ConfModule.
[nidrs] Registering service ConfService.
[nidrs] Injecting ConfService.
[nidrs] Triggering event on_module_init for ConfService.
ConfService initialized with log_level: ConfOptions { log_level: "info" }
[nidrs] Registering module LogModule.
[nidrs] Registering service LogService.
[nidrs] Injecting LogService.
[nidrs] Registering module UserModule.
[nidrs] Registering controller UserController.
[nidrs] Registering router 'GET /api/v1/user/hello'.
[nidrs] Registering service UserService.
[nidrs] Injecting UserService.
[nidrs] Injecting UserController.
[nidrs] Injecting AppService.
[nidrs] Injecting AppController.
[nidrs] Injecting LogInterceptor.
[nidrs] Listening on 127.0.0.1:3000
Open source is not easy, and sponsorship is also not easy. Both require courage. I believe that those who give love will receive love in return. May your good fortune start from this contribution~
The entire framework is currently in its early stages, with versions in the 0.x.x range being in testing. The stable version starts from 1.0. However, if you're simply looking for a high-level framework similar to Axum without needing the additional features mentioned later on, you can still give it a try.
Finally, if there are any interested individuals who wish to contribute and develop, they are welcome to join the Discord server below to contribute to the Rust community.