Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 49 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,48 +284,6 @@ async fn main() {

## Snippets

### Middlewares

Ohkami's request handling system is called "**fang**s", and middlewares are implemented on this.

*builtin fang* : `CORS`, `JWT`, `BasicAuth`, `Timeout`, `Context`

```rust,no_run
use ohkami::prelude::*;

#[derive(Clone)]
struct GreetingFang(usize);

/* utility trait; automatically impl `Fang` trait */
impl FangAction for GreetingFang {
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
let Self(id) = self;
println!("[{id}] Welcome request!: {req:?}");
Ok(())
}
async fn back<'a>(&'a self, res: &'a mut Response) {
let Self(id) = self;
println!("[{id}] Go, response!: {res:?}");
}
}

#[tokio::main]
async fn main() {
Ohkami::new((
// register fangs to a Ohkami
GreetingFang(1),

"/hello"
.GET(|| async {"Hello, fangs!"})
.POST((
// register *local fangs* to a handler
GreetingFang(2),
|| async {"I'm `POST /hello`!"}
))
)).howl("localhost:3000").await
}
```

### Typed payload

*builtin payload* : `JSON`, `Text`, `HTML`, `URLEncoded`, `Multipart`
Expand Down Expand Up @@ -366,10 +324,10 @@ use ohkami::prelude::*;
#[tokio::main]
async fn main() {
Ohkami::new((
"/hello/:name"
.GET(hello),
"/hello/:name/:n"
.GET(hello_n),
"/hello/:name"
.GET(hello),
"/search"
.GET(search),
)).howl("localhost:5000").await
Expand Down Expand Up @@ -404,6 +362,53 @@ async fn search(
}
```

### Middlewares

Ohkami's request handling system is called "**fang**s", and middlewares are implemented on this.

*builtin fang* :

- `Context` *( typed interaction with reuqest context )*
- `CORS`, `JWT`, `BasicAuth`
- `Timeout` *( native runtime )*
- `Enamel` *( experimantal; security headers )*

```rust,no_run
use ohkami::prelude::*;

#[derive(Clone)]
struct GreetingFang(usize);

/* utility trait; automatically impl `Fang` trait */
impl FangAction for GreetingFang {
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
let Self(id) = self;
println!("[{id}] Welcome request!: {req:?}");
Ok(())
}
async fn back<'a>(&'a self, res: &'a mut Response) {
let Self(id) = self;
println!("[{id}] Go, response!: {res:?}");
}
}

#[tokio::main]
async fn main() {
Ohkami::new((
// register fangs to a Ohkami
GreetingFang(1),

"/hello"
.GET(|| async {"Hello, fangs!"})
.POST((
// register *local fangs* to a handler
GreetingFang(2),
|| async {"I'm `POST /hello`!"}
))
)).howl("localhost:3000").await
}
```

### Database connection management with `Context`

```rust,no_run
Expand Down
3 changes: 3 additions & 0 deletions ohkami/src/fang/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use jwt::{JWT, JWTToken};
mod context;
pub use context::Context;

pub mod enamel;
pub use enamel::Enamel;

#[cfg(feature="__rt_native__")]
mod timeout;
#[cfg(feature="__rt_native__")]
Expand Down
Loading