ohkami - [狼] means wolf in Japanese - is simple and non macro-based web framework for Rust.
toml
[dependencies]
ohkami = "0.1"
```rust use ohkami::prelude::*;
fn main() -> Result<()> { Server::setup() .GET("/", || async {Response::OK("Hello, world!")}) .serveon(":3000") } ```
rust
let param: Option<&str> = ctx.param();
// current ohkami only supports single path param at the end of a path
rust
let query: Result<&str> = ctx.query("key");
rust
let body: Result<D> = ctx.body::<D>();
text/plain
rust
Response::OK("Hello, world!")
application/json
rust
Response::OK(JSON("Hello, world!"))
rust
Response::OK(json!("ok": true))
rust
Response::OK(json(user)?) // serialize Rust value into JSON
rust
let count = ctx.query("count")?.parse::<usize>()
._else(|_| Response::BadRequest("`count` must be an integer"))?;
```rust
let user = ctx.body::
// or, you can add an error context message:
let user = ctx.body::
// or discard original error:
let user = ctx.body::
rust
(count < 10)._else(|| Response::BadRequest("`count` must be less than 10"))
rust
fn main() -> Result<()> {
let config = Config {
log_subscribe:
Some(tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
),
..Default::default()
};
Server::setup_with(config)
.GET("/", |_| async {Response::OK("Hello!")})
}
rust
let config = Config {
db_profile: DBprofile {
pool_options: PgPoolOptions::new().max_connections(20),
url: DB_URL.as_str(),
},
..Default::default()
};
rust
let user = sqlx::query_as::<_, User>(
"SELECT id, name FROM users WHERE id = $1"
).bind(1)
.fetch_one(ctx.pool())
.await?; // `Response` implements `From<sqlx::Error>`
rust
fn server() -> Server {
Server::setup()
.GET("/", |_| async {Response::OK("Hello!")})
}
fn main() -> Result<()> {
server().serve_on(":3000")
}
assert_to_res
, assert_not_to_res
:
```rust
mod test { use ohkami::{server::Server, testsystem::{Request, Method}, response::Response}; use oncecell::sync::Lazy;
static SERVER: Lazy<Server> = Lazy::new(|| super::server());
#[test]
fn test_hello() {
let request = Request::new(Method::GET, "/");
(*SERVER).assert_to_res(&request, Response::OK("Hello!"));
(*SERVER).assert_not_to_res(&request, Err(Response::BadRequest("")));
}
} ```
ohkami is on very early stage now and not for producntion use.
This project is under MIT LICENSE (LICENSE-MIT or https://opensource.org/licenses/MIT).