11//! The `server` module functions to spawn an [`axum_server`] which communicates over TLS.
22
3+ mod auth;
34mod response;
45mod state;
56
67use core:: { marker:: PhantomData , time:: Duration } ;
78use std:: net:: SocketAddr ;
89
10+ use auth:: InitUsersTable ;
911use axum:: {
1012 error_handling:: HandleErrorLayer ,
1113 http:: StatusCode ,
@@ -14,9 +16,8 @@ use axum::{
1416 Router ,
1517} ;
1618use axum_server:: tls_rustls:: RustlsConfig ;
17- use casbin:: Enforcer ;
1819pub use response:: { LoginResponse , LogoutResponse , Response } ;
19- use sqlx:: { Connection , Database , Executor , Pool , Transaction } ;
20+ use sqlx:: { Connection , Database , Executor , Transaction } ;
2021pub use state:: State ;
2122use tower:: { timeout, ServiceBuilder } ;
2223use 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 ) ]
4243pub 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 ,
5653
5754impl < Db > Server < Db >
5855where
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 > ,
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
0 commit comments