Skip to content

Commit 8d68876

Browse files
committed
feat: initialize users table on DB
1 parent 91b8234 commit 8d68876

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

src/args.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ mod postgres;
44

55
use core::time::Duration;
66
use std::{
7-
error::Error,
87
net::SocketAddr,
98
path::{Path, PathBuf},
109
};

src/server.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! The `server` module functions to spawn an [`axum_server`] which communicates over TLS.
22
3+
mod auth;
34
mod response;
45
mod state;
56

67
use core::{marker::PhantomData, time::Duration};
78
use std::net::SocketAddr;
89

10+
use auth::InitUsersTable;
911
use axum::{
1012
error_handling::HandleErrorLayer,
1113
http::StatusCode,
@@ -14,9 +16,8 @@ use axum::{
1416
Router,
1517
};
1618
use axum_server::tls_rustls::RustlsConfig;
17-
use casbin::Enforcer;
1819
pub use response::{LoginResponse, LogoutResponse, Response};
19-
use sqlx::{Connection, Database, Executor, Pool, Transaction};
20+
use sqlx::{Connection, Database, Executor, Transaction};
2021
pub use state::State;
2122
use tower::{timeout, ServiceBuilder};
2223
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
@@ -35,15 +36,11 @@ use winvoice_adapter::{
3536
Updatable,
3637
};
3738

38-
use crate::{lock::Lock, DynResult};
39+
use crate::DynResult;
3940

4041
/// A Winvoice server.
4142
#[derive(Clone, Debug)]
4243
pub struct Server<Db>
43-
where
44-
Db: Database,
45-
Db::Connection: core::fmt::Debug,
46-
<Db::Connection as Connection>::Options: Clone,
4744
{
4845
/// The [`SocketAddr`] that self server is bound to.
4946
address: SocketAddr,
@@ -56,7 +53,7 @@ where
5653

5754
impl<Db> Server<Db>
5855
where
59-
Db: Database,
56+
Db: Database + InitUsersTable,
6057
Db::Connection: core::fmt::Debug,
6158
<Db::Connection as Connection>::Options: Clone,
6259
for<'connection> &'connection mut Db::Connection: Executor<'connection, Database = Db>,
@@ -87,13 +84,8 @@ where
8784
T: Deletable<Db = Db> + TimesheetAdapter,
8885
X: Deletable<Db = Db> + ExpensesAdapter,
8986
{
90-
axum_server::bind_rustls(self.address, self.tls)
91-
.serve(
92-
Self::router::<C, E, J, L, O, T, X>(state, session_ttl, timeout)
93-
.into_make_service(),
94-
)
95-
.await?;
96-
87+
let router = Self::router::<C, E, J, L, O, T, X>(state, session_ttl, timeout).await?;
88+
axum_server::bind_rustls(self.address, self.tls).serve(router.into_make_service()).await?;
9789
Ok(())
9890
}
9991

@@ -108,11 +100,11 @@ where
108100
}
109101

110102
/// Create the [`Router`] that will be used by the [`Server`].
111-
fn router<C, E, J, L, O, T, X>(
103+
async fn router<C, E, J, L, O, T, X>(
112104
state: State<Db>,
113105
session_ttl: Duration,
114106
timeout: Option<Duration>,
115-
) -> Router
107+
) -> DynResult<Router>
116108
where
117109
C: Deletable<Db = Db> + ContactAdapter,
118110
E: Deletable<Db = Db> + EmployeeAdapter,
@@ -122,8 +114,9 @@ where
122114
T: Deletable<Db = Db> + TimesheetAdapter,
123115
X: Deletable<Db = Db> + ExpensesAdapter,
124116
{
125-
let mut router = Router::new();
117+
Db::init_users_table(state.pool()).await?;
126118

119+
let mut router = Router::new();
127120
if let Some(t) = timeout
128121
{
129122
router = router.layer(
@@ -143,7 +136,7 @@ where
143136
);
144137
}
145138

146-
router
139+
Ok(router
147140
.layer(CompressionLayer::new())
148141
.layer(TraceLayer::new_for_http())
149142
.route("/login", routing::put(|| async { todo("login") }))
@@ -190,7 +183,7 @@ where
190183
.get(|| async { todo("timesheet retrieve") })
191184
.post(|| async { todo("timesheet create") }),
192185
)
193-
.with_state(state)
186+
.with_state(state))
194187
}
195188
}
196189

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Contains the [`InitUsersTable`] trait, and implementations for various [`Database`]s.
2+
3+
use sqlx::{Database, Executor, Result};
4+
5+
/// Initialize the `users`
6+
#[async_trait::async_trait]
7+
pub trait InitUsersTable: Database
8+
{
9+
/// Initialize the `users` table on the [`Database`]
10+
async fn init_users_table<'conn, C>(connection: C) -> Result<()>
11+
where
12+
C: Executor<'conn, Database = Self>;
13+
}
14+
15+
#[cfg(feature = "postgres")]
16+
#[async_trait::async_trait]
17+
impl InitUsersTable for sqlx::Postgres
18+
{
19+
async fn init_users_table<'conn, C>(connection: C) -> Result<()>
20+
where
21+
C: Executor<'conn, Database = Self>,
22+
{
23+
Ok(())
24+
}
25+
}

src/server/auth/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Contains data and algorithms used for authenticating users.
2+
3+
mod init_users_table;
4+
mod user;
5+
6+
pub use init_users_table::InitUsersTable;

src/server/auth/user.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! Contains the definition for what a [`User`] row in the [`Database`](sqlx::Database) is.

0 commit comments

Comments
 (0)