Skip to content

Commit

Permalink
Merge pull request #129 from ngyn-rs/dev
Browse files Browse the repository at this point in the history
v0.4.2 - Perf improvements + Swagger Crates
  • Loading branch information
elcharitas authored Aug 4, 2024
2 parents b9a14eb + cc18dca commit 55294fd
Show file tree
Hide file tree
Showing 42 changed files with 1,044 additions and 235 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: PR Workflow
on:
pull_request:
types: ready_for_review
push:
branches:
- dev
Expand Down
63 changes: 44 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Crates.io](https://img.shields.io/crates/v/ngyn.svg)](https://crates.io/crates/ngyn)
[![Docs.rs](https://docs.rs/ngyn/badge.svg)](https://ngyn.rs)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
![MSRV](https://img.shields.io/badge/MSRV-1.63-blue)
![MSRV](https://img.shields.io/badge/MSRV-1.75-blue)

A progressive framework in [Rust](https://www.rust-lang.org/) for building scalable web servers through an opinionated architecture.

Expand Down
1 change: 1 addition & 0 deletions cog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ ngyn_macros = { path = "crates/macros" }
ngyn_shared = { path = "crates/shared" }
ngyn-hyper = { path = "crates/hyper" }
ngyn-vercel = { path = "crates/vercel" }
ngyn-swagger = { path = "crates/swagger" }
6 changes: 3 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ngyn"
version = "0.4.0"
version = "0.4.2"
edition = "2021"
description = "Modular backend framework for web applications"
license = "MIT"
Expand All @@ -15,6 +15,6 @@ async-trait = "0.1"
http-body-util = "0.1"
hyper = { version = "1", features = ["full"] }
hyper-util = { version = "0.1", features = ["full"] }
ngyn_macros = { version = "^0.4.0", path = "../macros" }
ngyn_shared = { version = "^0.4.0", path = "../shared" }
ngyn_macros = { version = "0.4", path = "../macros" }
ngyn_shared = { version = "0.4", path = "../shared" }
tokio = { version = "1", features = ["full"] }
2 changes: 1 addition & 1 deletion crates/core/src/app/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<Application: NgynEngine> NgynFactory<Application> {
///
/// let server = NgynFactory::<HyperApplication>::create::<YourAppModule>();
/// ```
pub fn create<AppModule: NgynModule>() -> Application {
pub fn create<AppModule: NgynModule + 'static>() -> Application {
Application::build::<AppModule>()
}
}
4 changes: 2 additions & 2 deletions crates/hyper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ impl HyperApplication {
let mut buf = Vec::new();
if let Some(frame) = body.frame().await {
let chunk = frame?.into_data();
if let Ok(data) = chunk {
buf.extend_from_slice(&data)
if let Ok(bytes) = chunk {
buf.extend_from_slice(&bytes)
}
}
buf
Expand Down
5 changes: 3 additions & 2 deletions crates/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[package]
name = "ngyn_macros"
version = "0.4.0"
version = "0.4.2"
edition = "2021"
description = "Modular backend framework for web applications"
license = "MIT"

[dependencies]
http = "1.1"
syn = "2.0"
quote = "1.0"
ngyn_shared = { version = "^0.4.0", path = "../shared" }
ngyn_shared = { version = "0.4", path = "../shared" }

[lib]
path = "src/lib.rs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::ItemImpl;
use crate::utils::str::str_to_ident;

/// This macro is used to add a check attribute to all methods in an impl block.
pub(crate) fn check_impl_macro(impl_item: ItemImpl, args: TokenStream) -> TokenStream {
pub(crate) fn check_impl_macro(args: TokenStream, impl_item: ItemImpl) -> TokenStream {
let mut new_items = Vec::new();

// Create the check attribute
Expand All @@ -22,7 +22,7 @@ pub(crate) fn check_impl_macro(impl_item: ItemImpl, args: TokenStream) -> TokenS
for item in impl_item.items {
let new_item = match item {
syn::ImplItem::Fn(mut method) => {
if method.attrs.clone().into_iter().any(|attr| {
if method.attrs.iter().any(|attr| {
attr.path().is_ident("route")
|| attr.path().is_ident("get")
|| attr.path().is_ident("post")
Expand All @@ -48,6 +48,6 @@ pub(crate) fn check_impl_macro(impl_item: ItemImpl, args: TokenStream) -> TokenS
}

/// This macro is used to add a check attribute handler to a method.
pub(crate) fn check_fn_macro(_args: TokenStream, input: TokenStream) -> TokenStream {
input
pub(crate) fn check_fn_macro(_args: TokenStream, input: syn::ItemFn) -> TokenStream {
quote::quote!(#input).into()
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub(crate) fn controller_macro(args: TokenStream, input: TokenStream) -> TokenSt
..
}| {
add_fields.push(quote! {
#ident #colon_token #ty::default()
#ident #colon_token Default::default()
});
if attrs.iter().any(|attr| attr.path().is_ident("inject")) {
inject_fields.push(quote! {
Expand Down Expand Up @@ -154,8 +154,15 @@ pub(crate) fn controller_macro(args: TokenStream, input: TokenStream) -> TokenSt

let path_prefix = {
if let Some(prefix) = prefix {
quote! {
format!("{}{}", #prefix, "/")
let str_prefix = prefix.value();
if !str_prefix.starts_with('/') {
quote! {
format!("/{}", #prefix)
}
} else {
quote! {
format!("{}", #prefix)
}
}
} else {
quote! {
Expand Down Expand Up @@ -196,33 +203,41 @@ pub(crate) fn controller_macro(args: TokenStream, input: TokenStream) -> TokenSt
#(#fields),*
}

impl #generics ngyn::shared::traits::NgynControllerHandler for #ident #generics_params {}

impl #generics ngyn::shared::traits::NgynInjectable for #ident #generics_params {
fn new() -> Self {
#init_controller
}

fn inject(&mut self, cx: &ngyn::prelude::NgynContext) {
#(#inject_fields)*
#inject_controller
}
}

#[ngyn::prelude::async_trait]
impl #generics ngyn::shared::traits::NgynController for #ident #generics_params {
fn routes(&self) -> Vec<(String, String, String)> {
use ngyn::shared::traits::NgynControllerHandler;
Self::ROUTES.iter().map(|(path, method, handler)| {
((format!("{}{}", #path_prefix, path)).replace("//", "/"), method.to_string(), handler.to_string())
// prefix path with controller prefix, and remove double slashes
let path = format!("{}", path).trim_start_matches('/').to_string();
let prefix = #path_prefix.trim_end_matches('/').to_string();
(format!("{}/{}", prefix, path), method.to_string(), handler.to_string())
}).collect()
}

fn prefix(&self) -> String {
#path_prefix
}

async fn handle(
&mut self,
handler: &str,
cx: &mut ngyn::prelude::NgynContext,
res: &mut ngyn::prelude::NgynResponse,
) {
#(#inject_fields)*
use ngyn::shared::traits::NgynControllerHandler;
self.inject(cx);
#(#add_middlewares)*
self.__handle_route(handler, cx, res).await;
}
Expand Down
19 changes: 19 additions & 0 deletions crates/macros/src/common/http_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use http::StatusCode;
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn, LitInt};

pub(crate) fn http_code_macro(args: TokenStream, raw_input: TokenStream) -> TokenStream {
match syn::parse::<ItemFn>(raw_input.clone()) {
Ok(_) => {
let args = parse_macro_input!(args as LitInt);
let code = args.base10_parse::<u16>().unwrap();
StatusCode::from_u16(code)
.unwrap_or_else(|_| panic!("Invalid HTTP status code: {}", code));
}
Err(err) => {
panic!("failed to parse route: {}", err)
}
};

raw_input
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) fn injectable_macro(args: TokenStream, input: TokenStream) -> TokenSt
..
}| {
add_fields.push(quote! {
#ident #colon_token #ty::default()
#ident #colon_token Default::default()
});
if attrs.iter().any(|attr| attr.path().is_ident("inject")) {
inject_fields.push(quote! {
Expand Down
13 changes: 7 additions & 6 deletions crates/macros/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod check_macro;
pub mod controller_macro;
pub mod inject_macro;
pub mod injectable_macro;
pub mod route_macro;
pub mod routes_macro;
pub mod check;
pub mod controller;
pub mod http_code;
pub mod inject;
pub mod injectable;
pub mod route;
pub mod routes;
File renamed without changes.
Loading

0 comments on commit 55294fd

Please sign in to comment.