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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ struct UserRow {
name: String,
}

#[derive(Clone)]
struct PostgresUserRepository(sqlx::PgPool);
impl UserRepository for PostgresUserRepository {
async fn get_user_by_id(&self, id: i64) -> Result<UserRow, MyError> {
Expand Down Expand Up @@ -790,7 +791,12 @@ fn users_ohkami<R: UserRepository>() -> Ohkami {

#[tokio::main]
async fn main() {
let pool = sqlx::PgPool::connect("postgres://ohkami:password@localhost:5432/db")
.await
.expect("failed to connect to database");

Ohkami::new((
Context::new(PostgresUserRepository(pool)),
"/users".By(users_ohkami::<PostgresUserRepository>()),
)).howl("0.0.0.0:4040").await
}
Expand Down
24 changes: 20 additions & 4 deletions ohkami/src/ohkami/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ use crate::tls::TlsStream;
///
/// ## Generics DI
///
/// A way of DI -*Dependency Injection*- is **generics** :
/// A way of DI is **generics** :
///
/// ```
/// ```no_run
/// use ohkami::{Ohkami, Route};
/// use ohkami::handle::{Path, Json};
/// use ohkami::fang::Context;
Expand Down Expand Up @@ -295,8 +295,9 @@ use crate::tls::TlsStream;
/// ) -> impl Future<Output = Result<String, MyError>> + Send;
/// }
///
/// struct PostgresRepository(sqlx::PgPool);
/// impl UserRepository for PostgresRepository {
/// #[derive(Clone)]
/// struct PostgresUserRepository(sqlx::PgPool);
/// impl UserRepository for PostgresUserRepository {
/// async fn get_user_name_by_id(&self, id: i64) -> Result<String, MyError> {
/// let sql = r#"
/// SELECT name FROM users WHERE id = $1
Expand Down Expand Up @@ -335,6 +336,21 @@ use crate::tls::TlsStream;
/// "/:id".GET(get_user::<R>),
/// ))
/// }
///
/// //////////////////////////////////////////////////////////////////////
/// /// entry point
///
/// #[tokio::main]
/// async fn main() {
/// let pool = sqlx::PgPool::connect("postgres://ohkami:password@localhost:5432/db")
/// .await
/// .expect("failed to connect to database");
///
/// Ohkami::new((
/// Context::new(PostgresUserRepository(pool)),
/// "/users".By(users_ohkami::<PostgresUserRepository>()),
/// )).howl("0.0.0.0:4040").await
/// }
/// ```
pub struct Ohkami {
router: Router,
Expand Down