Skip to content

Commit

Permalink
Merge pull request #97 from myyrakle/feat/#95
Browse files Browse the repository at this point in the history
[#95]
  • Loading branch information
myyrakle authored Feb 2, 2024
2 parents da31e13 + b82a87e commit 89287a4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rupring

![](https://img.shields.io/badge/language-Rust-red) ![](https://img.shields.io/badge/version-0.6.0-brightgreen) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/myyrakle/rupring/blob/master/LICENSE)
![](https://img.shields.io/badge/language-Rust-red) ![](https://img.shields.io/badge/version-0.7.0-brightgreen) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/myyrakle/rupring/blob/master/LICENSE)

spring on rust

Expand Down
4 changes: 2 additions & 2 deletions rupring/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rupring"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
license = "MIT"
authors = ["myyrakle <[email protected]>"]
Expand All @@ -14,7 +14,7 @@ homepage = "https://github.com/myyrakle/rupring/blob/master/README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rupring_macro={ version="0.6.0", path="../rupring_macro" }
rupring_macro={ version="0.7.0", path="../rupring_macro" }
hyper = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
http-body-util = "0.1.0"
Expand Down
92 changes: 92 additions & 0 deletions rupring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,86 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
```
# Request
You can access any value provided in an HTTP Request through the Request parameter.
```
#[rupring::Get(path = /:id)]
pub fn hello(request: rupring::Request) -> rupring::Response {
let method = request.method;
assert_eq!(method, rupring::Method::GET);
let path = request.path;
assert_eq!(path, "/");
let body = request.body;
assert_eq!(body, "");
let headers = request.headers;
let content_type = headers.get("content-type").unwrap();
assert_eq!(content_type, "text/plain");
let id = request.path_param("id").unwrap();
assert_eq!(id, "123");
let query = request.query_param("query").unwrap();
assert_eq!(query, "asdf");
...
}
```
## Request: Path Param
For path parameters, auto binding is provided through annotation.
The annotation name can be one of `Path`, `path`, or `PathVariable`.
```
#[rupring::Get(path = /echo/:id)]
pub fn echo(
#[PathVariable="id"] id: i32
) -> rupring::Response {
println!("id: {}", id);
rupring::Response::new().text(request.body)
}
```
If the Path Param is optional, just wrap the type in `Option`.
```
#[rupring::Get(path = /echo/:id)]
pub fn echo(
#[PathVariable="id"] id: Option<i32>
) -> rupring::Response {
...
}
```
If you need Swagger documentation for the Path Param, you should add the `Description` annotation.
`Description` can also be used as `Desc`, `desc`, etc.
```
#[rupring::Get(path = /echo/:id)]
pub fn echo(
#[path="id"] #[desc="asdf"] id: i32
) -> rupring::Response {
println!("id: {}", id);
rupring::Response::new().text(request.body)
}
```
If you want to define a custom type for PathParam, you can implement the ParamStringDeserializer trait.
```
impl ParamStringDeserializer<SomeCustomType> for ParamString {
type Error = ();
fn deserialize(&self) -> Result<SomeCustomType, Self::Error> {
...
}
}
```
# Response
You can create a response like this:
Expand Down Expand Up @@ -300,14 +380,23 @@ fn inject_counter_service(something: SomethingRepository) -> CounterService {
middlewares=[]
)]
```
# Swagger
When rupring starts the server, it automatically serves swagger documents to the `/docs` path.
Details are still being implemented.
*/

pub(crate) mod boot;

/// header constants
pub mod header;
mod logger;
/// MEME type constants
pub mod meme;
pub(crate) mod request;
pub(crate) mod response;
/// swagger module
pub mod swagger;

use std::panic::UnwindSafe;
Expand Down Expand Up @@ -428,7 +517,9 @@ pub type HeaderName = hyper::header::HeaderName;
pub use boot::di::DIContext;
/// Dependency Injection Provider
pub use boot::di::IProvider;
/// String wrapper type for ParamStringDeserializer.
pub use request::ParamString;
/// ParamStringDeserializer trait
pub use request::ParamStringDeserializer;
/// HTTP Request
pub use request::Request;
Expand All @@ -444,6 +535,7 @@ pub trait IModule {
fn middlewares(&self) -> Vec<MiddlewareFunction>;
}

/// Middleware function type
pub type MiddlewareFunction =
Box<dyn Fn(Request, Response, NextFunction) -> Response + Send + Sync + UnwindSafe + 'static>;

Expand Down
7 changes: 5 additions & 2 deletions rupring/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ pub struct HomeController {}
// #[summary = "기본 root API입니다."]
// #[description = "별다른 기능은 없습니다."]
// #[tags = [home]]
// pub fn hello(_request: rupring::Request) -> rupring::Response {
// pub fn hello(request: rupring::Request) -> rupring::Response {
// let body = request.body;
// println!("body: {}", body);

// rupring::Response::new().text("123214")
// }

Expand All @@ -163,7 +166,7 @@ pub struct HomeController {}
pub fn echo(
#[path="id"] #[desc="asdf"] id: i32
) -> rupring::Response {
println!("id: {}", id);
println!("id: {}", id);

rupring::Response::new().text(request.body)
}
Expand Down
2 changes: 1 addition & 1 deletion rupring_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rupring_macro"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
description = "rupring macro"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion rupring_macro/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use proc_macro::{Punct, TokenStream, TokenTree};
use proc_macro::{TokenStream, TokenTree};

// Find the structure name immediately to the right of the struct keyword.
pub(crate) fn find_struct_name(item: TokenStream) -> String {
Expand Down

0 comments on commit 89287a4

Please sign in to comment.