Skip to content

Commit 925df35

Browse files
authored
Merge pull request #375 from ohkami-rs/v0.23
v0.23
2 parents 6e243ac + 9a0219c commit 925df35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2530
-545
lines changed

Diff for: .github/workflows/CI.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
toolchain: [stable, nightly]
14+
toolchain: ['stable', 'nightly']
15+
task: ['check', 'test:core', 'test:other', 'bench:dryrun']
1516

1617
steps:
1718
- uses: actions/checkout@v4
@@ -61,4 +62,4 @@ jobs:
6162
- name: Run tasks
6263
run: |
6364
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
64-
task CI
65+
task ${{ matrix.task }}

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exclude = [
1818
]
1919

2020
[workspace.package]
21-
version = "0.22.0"
21+
version = "0.23.0"
2222
edition = "2021"
2323
authors = ["kanarus <[email protected]>"]
2424
homepage = "https://crates.io/crates/ohkami"

Diff for: README.md

+80-48
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
- *macro-less and type-safe* APIs for intuitive and declarative code
99
- *various runtimes* are supported:`tokio`, `async-std`, `smol`, `nio`, `glommio` and `worker` (Cloudflare Workers), `lambda` (AWS Lambda)
10-
- *extremely fast*[Web Frameworks Benchmark](https://web-frameworks-benchmark.netlify.app/result)
11-
- no-network testing, well-structured middlewares, Server-Sent Events, WebSocket, OpenAPI document generation, ...
10+
- extremely fast, no-network testing, well-structured middlewares, Server-Sent Events, WebSocket, highly integrated OpenAPI document generation, ...
1211

1312
<div align="right">
1413
<a href="https://github.com/ohkami-rs/ohkami/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/crates/l/ohkami.svg" /></a>
@@ -24,7 +23,7 @@
2423

2524
```toml
2625
[dependencies]
27-
ohkami = { version = "0.22", features = ["rt_tokio"] }
26+
ohkami = { version = "0.23", features = ["rt_tokio"] }
2827
tokio = { version = "1", features = ["full"] }
2928
```
3029

@@ -195,7 +194,7 @@ async fn main() {
195194

196195
`"openapi"` provides highly integrated OpenAPI support.
197196

198-
This enables *as consistent as possible* OpenAPI document generation, where most of the consistency between document and behavior is automatically assured by Ohkami's internal work.
197+
This enables **macro-less**, *as consistent as possible* OpenAPI document generation, where most of the consistency between document and behavior is automatically assured by Ohkami's internal work.
199198

200199
Only you have to
201200

@@ -284,48 +283,6 @@ async fn main() {
284283

285284
## Snippets
286285

287-
### Middlewares
288-
289-
Ohkami's request handling system is called "**fang**s", and middlewares are implemented on this.
290-
291-
*builtin fang* : `CORS`, `JWT`, `BasicAuth`, `Timeout`, `Context`
292-
293-
```rust,no_run
294-
use ohkami::prelude::*;
295-
296-
#[derive(Clone)]
297-
struct GreetingFang(usize);
298-
299-
/* utility trait; automatically impl `Fang` trait */
300-
impl FangAction for GreetingFang {
301-
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
302-
let Self(id) = self;
303-
println!("[{id}] Welcome request!: {req:?}");
304-
Ok(())
305-
}
306-
async fn back<'a>(&'a self, res: &'a mut Response) {
307-
let Self(id) = self;
308-
println!("[{id}] Go, response!: {res:?}");
309-
}
310-
}
311-
312-
#[tokio::main]
313-
async fn main() {
314-
Ohkami::new((
315-
// register fangs to a Ohkami
316-
GreetingFang(1),
317-
318-
"/hello"
319-
.GET(|| async {"Hello, fangs!"})
320-
.POST((
321-
// register *local fangs* to a handler
322-
GreetingFang(2),
323-
|| async {"I'm `POST /hello`!"}
324-
))
325-
)).howl("localhost:3000").await
326-
}
327-
```
328-
329286
### Typed payload
330287

331288
*builtin payload* : `JSON`, `Text`, `HTML`, `URLEncoded`, `Multipart`
@@ -366,10 +323,10 @@ use ohkami::prelude::*;
366323
#[tokio::main]
367324
async fn main() {
368325
Ohkami::new((
369-
"/hello/:name"
370-
.GET(hello),
371326
"/hello/:name/:n"
372327
.GET(hello_n),
328+
"/hello/:name"
329+
.GET(hello),
373330
"/search"
374331
.GET(search),
375332
)).howl("localhost:5000").await
@@ -404,6 +361,81 @@ async fn search(
404361
}
405362
```
406363

364+
### Middlewares
365+
366+
Ohkami's request handling system is called "**fang**s", and middlewares are implemented on this.
367+
368+
*builtin fang* :
369+
370+
- `Context` *( typed interaction with reuqest context )*
371+
- `CORS`, `JWT`, `BasicAuth`
372+
- `Timeout` *( native runtime )*
373+
- `Enamel` *( experimantal; security headers )*
374+
375+
```rust,no_run
376+
use ohkami::prelude::*;
377+
378+
#[derive(Clone)]
379+
struct GreetingFang(usize);
380+
381+
/* utility trait; automatically impl `Fang` trait */
382+
impl FangAction for GreetingFang {
383+
async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
384+
let Self(id) = self;
385+
println!("[{id}] Welcome request!: {req:?}");
386+
Ok(())
387+
}
388+
async fn back<'a>(&'a self, res: &'a mut Response) {
389+
let Self(id) = self;
390+
println!("[{id}] Go, response!: {res:?}");
391+
}
392+
}
393+
394+
#[tokio::main]
395+
async fn main() {
396+
Ohkami::new((
397+
// register fangs to a Ohkami
398+
GreetingFang(1),
399+
400+
"/hello"
401+
.GET(|| async {"Hello, fangs!"})
402+
.POST((
403+
// register *local fangs* to a handler
404+
GreetingFang(2),
405+
|| async {"I'm `POST /hello`!"}
406+
))
407+
)).howl("localhost:3000").await
408+
}
409+
```
410+
411+
### Database connection management with `Context`
412+
413+
```rust,no_run
414+
use ohkami::prelude::*;
415+
use ohkami::typed::status;
416+
use sqlx::postgres::{PgPoolOptions, PgPool};
417+
418+
#[tokio::main]
419+
async fn main() {
420+
let pool = PgPoolOptions::new()
421+
.connect("postgres://ohkami:password@localhost:5432/db").await
422+
.expect("failed to connect");
423+
424+
Ohkami::new((
425+
Context::new(pool),
426+
"/users".POST(create_user),
427+
)).howl("localhost:5050").await
428+
}
429+
430+
async fn create_user(
431+
Context(pool): Context<'_, PgPool>,
432+
) -> status::Created {
433+
//...
434+
435+
status::Created(())
436+
}
437+
```
438+
407439
### Static directory serving
408440

409441
```rust,no_run

Diff for: Taskfile.yaml

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ tasks:
1212

1313
test:
1414
deps:
15-
- task: test:deps
16-
- task: test:doc
17-
- task: test:examples
18-
- task: test:samples
15+
- task: test:core
16+
- task: test:other
17+
test:core:
18+
deps:
1919
- task: test:no_rt
2020
- for: [tokio, async-std, smol, nio, glommio, lambda, worker]
2121
task: test:rt
2222
vars: { rt: '{{.ITEM}}' }
23+
test:other:
24+
deps:
25+
- task: test:deps
26+
- task: test:doc
27+
- task: test:examples
28+
- task: test:samples
2329

2430
check:
2531
deps:

Diff for: ohkami/Cargo.toml

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ features = ["rt_tokio", "nightly", "sse", "ws"]
1818

1919
[dependencies]
2020
# workspace members
21-
ohkami_lib = { version = "=0.22.0", path = "../ohkami_lib" }
22-
ohkami_macros = { version = "=0.22.0", path = "../ohkami_macros" }
23-
ohkami_openapi = { optional = true, version = "=0.22.0", path = "../ohkami_openapi" }
21+
ohkami_lib = { version = "=0.23.0", path = "../ohkami_lib" }
22+
ohkami_macros = { version = "=0.23.0", path = "../ohkami_macros" }
23+
ohkami_openapi = { optional = true, version = "=0.23.0", path = "../ohkami_openapi" }
2424

2525
# workspace dependencies
2626
byte_reader = { workspace = true }
@@ -107,4 +107,8 @@ DEBUG = ["tokio?/rt-multi-thread", "tokio?/macros"]
107107
# #"rt_glommio",
108108
# #"rt_worker",
109109
# "DEBUG",
110-
#]
110+
#]
111+
112+
113+
[dev-dependencies]
114+
sqlx = { version = "0.8", features = ["runtime-tokio", "postgres"] }

Diff for: ohkami/src/fang/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub use jwt::{JWT, JWTToken};
1010
mod context;
1111
pub use context::Context;
1212

13+
pub mod enamel;
14+
pub use enamel::Enamel;
15+
1316
#[cfg(feature="__rt_native__")]
1417
mod timeout;
1518
#[cfg(feature="__rt_native__")]

0 commit comments

Comments
 (0)