@@ -32,18 +32,12 @@ use win_api_wrappers::token::Token;
3232use win_api_wrappers:: undoc:: PIPE_ACCESS_FULL_CONTROL ;
3333use win_api_wrappers:: utils:: Pipe ;
3434
35- use elevate_session:: elevate_session;
36- use elevate_temporary:: elevate_temporary;
37- use launch:: post_launch;
38- use revoke:: post_revoke;
39- use state:: { AppState , AppStateError } ;
40- use status:: get_status;
41-
4235use crate :: config:: Config ;
43- use crate :: db:: DbError ;
36+ use crate :: db:: { Db , DbError , InitSchemaError } ;
4437use crate :: error:: { Error , ErrorResponse } ;
4538use crate :: utils:: AccountExt ;
4639
40+ mod about;
4741mod elevate_session;
4842mod elevate_temporary;
4943mod err;
@@ -53,6 +47,14 @@ mod revoke;
5347pub ( crate ) mod state;
5448mod status;
5549
50+ use about:: about;
51+ use elevate_session:: elevate_session;
52+ use elevate_temporary:: elevate_temporary;
53+ use launch:: post_launch;
54+ use revoke:: post_revoke;
55+ use state:: { AppState , AppStateError } ;
56+ use status:: get_status;
57+
5658#[ derive( Debug , Clone ) ]
5759struct NamedPipeConnectInfo {
5860 pub ( crate ) user : User ,
@@ -132,6 +134,7 @@ fn create_pipe(pipe_name: &str) -> anyhow::Result<NamedPipeServer> {
132134
133135pub ( crate ) fn api_router ( ) -> ApiRouter < AppState > {
134136 ApiRouter :: new ( )
137+ . api_route ( "/about" , aide:: axum:: routing:: get ( about) )
135138 . api_route ( "/elevate/temporary" , aide:: axum:: routing:: post ( elevate_temporary) )
136139 . api_route ( "/elevate/session" , aide:: axum:: routing:: post ( elevate_session) )
137140 . api_route ( "/launch" , aide:: axum:: routing:: post ( post_launch) )
@@ -165,8 +168,12 @@ async fn health_check() -> &'static str {
165168 "OK"
166169}
167170
171+ /// Initializes the appliation and starts the named pipe server.
168172pub async fn serve ( config : Config ) -> Result < ( ) , ServeError > {
169- let state = AppState :: load ( & config) . await ?;
173+ let db = Db :: new ( & config) . await ?;
174+ db. init_schema ( ) . await ?;
175+
176+ let state = AppState :: new ( db, & config. pipe_name ) . await ?;
170177
171178 // a plain Axum router
172179 let hello_router = Router :: new ( ) . route ( "/health" , axum:: routing:: get ( health_check) ) ;
@@ -182,11 +189,11 @@ pub async fn serve(config: Config) -> Result<(), ServeError> {
182189 let mut server = create_pipe ( pipe_name) ?;
183190
184191 // Log the server startup.
185- let run_id = state. db . log_server_startup ( pipe_name) . await ?;
186- info ! ( "Started server at {pipe_name} with run ID {run_id}" ) ;
192+ info ! ( "Started named pipe server with name `{pipe_name}`" ) ;
187193 info ! (
188- "Starting request ID counter at {}" ,
189- state. req_counter. load( Ordering :: Relaxed )
194+ "Run ID is {run_id}, request ID counter is {req_count}" ,
195+ run_id = state. startup_info. run_id,
196+ req_count = state. req_counter. load( Ordering :: Relaxed )
190197 ) ;
191198
192199 loop {
@@ -219,6 +226,7 @@ pub enum ServeError {
219226 TokioIo ( tokio:: io:: Error ) ,
220227 AppState ( AppStateError ) ,
221228 Db ( DbError ) ,
229+ InitSchema ( InitSchemaError ) ,
222230 Other ( anyhow:: Error ) ,
223231}
224232
@@ -228,6 +236,7 @@ impl core::error::Error for ServeError {
228236 Self :: TokioIo ( e) => Some ( e) ,
229237 Self :: AppState ( e) => Some ( e) ,
230238 Self :: Db ( e) => Some ( e) ,
239+ Self :: InitSchema ( e) => Some ( e) ,
231240 Self :: Other ( e) => Some ( e. as_ref ( ) ) ,
232241 }
233242 }
@@ -239,6 +248,7 @@ impl fmt::Display for ServeError {
239248 Self :: TokioIo ( e) => e. fmt ( f) ,
240249 Self :: AppState ( e) => e. fmt ( f) ,
241250 Self :: Db ( e) => e. fmt ( f) ,
251+ Self :: InitSchema ( e) => e. fmt ( f) ,
242252 Self :: Other ( e) => e. fmt ( f) ,
243253 }
244254 }
@@ -259,6 +269,11 @@ impl From<DbError> for ServeError {
259269 Self :: Db ( e)
260270 }
261271}
272+ impl From < InitSchemaError > for ServeError {
273+ fn from ( e : InitSchemaError ) -> Self {
274+ Self :: InitSchema ( e)
275+ }
276+ }
262277impl From < anyhow:: Error > for ServeError {
263278 fn from ( e : anyhow:: Error ) -> Self {
264279 Self :: Other ( e)
0 commit comments