1- use std:: fs;
21use std:: path:: PathBuf ;
32use std:: sync:: Mutex ;
43
54use gethostname:: gethostname;
65use rocket:: response:: NamedFile ;
76use rocket:: State ;
87use rocket_contrib:: json:: JsonValue ;
9- use uuid:: Uuid ;
108
119use crate :: config:: AWConfig ;
12- use crate :: dirs;
1310
1411#[ macro_export]
1512macro_rules! endpoints_get_lock {
@@ -36,6 +33,7 @@ use aw_datastore::Datastore;
3633pub struct ServerState {
3734 pub datastore : Mutex < Datastore > ,
3835 pub asset_path : PathBuf ,
36+ pub device_id : String ,
3937}
4038
4139#[ get( "/" ) ]
@@ -68,25 +66,8 @@ fn root_favicon(state: State<ServerState>) -> Option<NamedFile> {
6866 NamedFile :: open ( state. asset_path . join ( "favicon.ico" ) ) . ok ( )
6967}
7068
71- /// Retrieves the device ID, if none exists it generates one (using UUID v4)
72- fn get_device_id ( ) -> String {
73- // TODO: Cache to avoid retrieving on every /info call
74- // TODO: How should these unwraps be removed?
75- // Should this be propagated into a 500 Internal Server Error? How?
76- // I chose get_data_dir over get_config_dir since the latter isn't yet supported on Android.
77- let mut path = dirs:: get_data_dir ( ) . unwrap ( ) ;
78- path. push ( "device_id" ) ;
79- if path. exists ( ) {
80- fs:: read_to_string ( path) . unwrap ( )
81- } else {
82- let uuid = Uuid :: new_v4 ( ) . to_hyphenated ( ) . to_string ( ) ;
83- fs:: write ( path, & uuid) . unwrap ( ) ;
84- uuid
85- }
86- }
87-
8869#[ get( "/" ) ]
89- fn server_info ( config : State < AWConfig > ) -> JsonValue {
70+ fn server_info ( config : State < AWConfig > , state : State < ServerState > ) -> JsonValue {
9071 #[ allow( clippy:: or_fun_call) ]
9172 let hostname = gethostname ( ) . into_string ( ) . unwrap_or ( "unknown" . to_string ( ) ) ;
9273 const VERSION : Option < & ' static str > = option_env ! ( "CARGO_PKG_VERSION" ) ;
@@ -95,7 +76,7 @@ fn server_info(config: State<AWConfig>) -> JsonValue {
9576 "hostname" : hostname,
9677 "version" : format!( "v{} (rust)" , VERSION . unwrap_or( "(unknown)" ) ) ,
9778 "testing" : config. testing,
98- "device_id" : get_device_id ( ) ,
79+ "device_id" : state . device_id ,
9980 } )
10081}
10182
0 commit comments