ohkami - [狼] means wolf in Japanese - is simple and macro free web framework for Rust.
toml
[dependencies]
ohkami = "0.6.2"
```rust use ohkami::prelude::*;
fn main() -> Result<()> { Ohkami::default() .GET("/", || async { Response::OK("Hello, world!") }) .howl(":3000") } ```
```rust let name: &str = c.req.query("name")?;
let count: usize = c.req.query("count")?; ```
```rust fn main() -> Result<()> { Ohkami::default() .GET("/api/users", reflect) .GET("/api/users/name", reflect_name) .howl(":3000") }
struct User { id: i64, name: String, }
async fn reflect(user: JSON
async fn reflect_name(user: JSONJSON
implements Deref
Response::OK(name)
} ```
```rust fn main() -> Result<()> { Ohkami::default() .GET("/sleepy/:time/:name", sleepy_hello) .howl("localhost:8080") }
async fn sleepyhello(time: u64, name: String) -> Result
```rust use serde::{Serialize, Deserialize}; use ohkami::{ prelude::*, group::{GET, POST} // import this };
struct User { id: usize, name: String, }
fn main() -> Result<()> { Ohkami::default() .GET("/", || async { Response::OK("Hello!") }) .route("/api", GET(hello_api).POST(reflect) ) .howl(":3000") }
async fn hello_api() -> Result
async fn reflect(payload: JSON
rust
let host = c.req.header(Header::Host)?;
rust
async fn reflect_header_custom(c: Context) -> Result<Response> {
let custom_header_value = c.req.header("X-Custom")?;
c.OK(format!("`X-Custom`'s value is {custom_header_value}"))
}
rust
c.header(Header::AccessControlAllowOrigin, "mydomain:8000");
// or
c.header("Access-Control-Allow-Origin", "mydomain:8000");
```rust
use ohkami::prelude::*;
use Header::{AccessControlAllowOrigin};
async fn cors(c: Context) -> Context { c.header(AccessControlAllowOrigin, "mydomain:8000"); c }
fn main() -> Result<()> { let middleware = Middleware::new() .ANY("/api/*", cors);
// ...
```
text/plain
rust
Response::OK("Hello, world!")
// without Context
rust
c.OK("Hello, world!")
// with Context
application/json
rust
Response::OK(json!{"ok": true})
// or
c.OK(json!{"ok": true})
rust
Response::OK(json("Hello!"))
Response::OK(json(user))
//or
c.OK(json("Hello!"))
c.OK(json(user))
// `json()` serializes Rust value into JSON
// value has to implemant `serde::Serialize`
```rust makeohkamiresult()?;
// or, you can add an error context message: makeohkamiresult() .else(|e| e.errorcontext("failed to get user data"))?;
// or discard original error:
makeohkamiresult()
.else(|| Response::InternalServerError("can't get user"))?;
// or
.else(|| Response::InternalServerError(None))?;
rust
makesomeresult(/* can't use ?
*/)
.else(|e| Response::InternalServerError(e.tostring()))?;
makesomeresult() .else(|| Response::InternalServerError(None))?; ```
rust
let handler = self.handler.as_ref()
._else(|| Response::NotFound("handler not found"))?;
// or
._else(|| Response::NotFound(None))?;
rust
(count < 10)
._else(|| Response::BadRequest("`count` must be less than 10"))?;
// or
._else(|| Response::BadRequest(None))?;
rust
fn main() -> Result<()> {
let config = Config {
log_subscribe: Some(
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
),
..Default::default()
};
Ohkami::new(config)
.GET("/", || async {Response::OK("Hello!")})
}
rust
let config = Config {
db_profile: DBprofile {
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(c.pool())
.await?; // `Response` implements `From<sqlx::Error>`
```rust fn main() -> Result<()> { let middleware = Middleware::new() .ANY("*", |c| async { tracing::info!("Hello, middleware!"); c });
Ohkami::new(middleware)
.GET("/", || async {
Response::OK("Hello!")
})
.howl("localhost:3000")
}
rust
fn main() -> Result<()> {
let config = Config {
logsubscribe: Some(
tracingsubscriber::fmt()
.withmaxlevel(tracing::Level::TRACE)
),
..Default::default()
};
let middleware = Middleware::new()
.ANY("/*", |c| async {
tracing::info!("Hello, middleware!");
c
});
let thirdparty_middleware = some_external_crate::x;
Ohkami::new(config.and(middleware).and(x))
.GET("/", || async {
Response::OK("Hello!")
})
.howl("localhost:3000")
} ```
main
function:
```rust
fn server() -> Server {
Ohkami::default()
.GET("/", || async {Response::OK("Hello!")})
}fn main() -> Result<()> {
server().howl(":3000")
}
2. import `test::Test` and other utils
rust
mod test { use ohkami::{Ohkami::Server, response::Response, test::{Test, Request, Method}}; use once_cell::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, Response::BadRequest(None));
}
} ```
ohkami is on early stage now and not for producntion use. Please give me your feedback ! → GetHub issue
This project is licensed under MIT LICENSE (LICENSE-MIT or https://opensource.org/licenses/MIT).