Skip to content

Commit 7f6f63d

Browse files
aw-server: Make UUID part of ServerState
1 parent a72cefe commit 7f6f63d

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

aw-client-rust/tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod test {
4444
let state = ServerState {
4545
datastore: Mutex::new(aw_datastore::Datastore::new_in_memory(false)),
4646
asset_path: PathBuf::from("."), // webui won't be used, so it's invalidly set
47+
device_id: "test_id".to_string(),
4748
};
4849
let mut aw_config = aw_server::config::AWConfig::default();
4950
aw_config.port = PORT;

aw-server/src/endpoints/mod.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
use std::fs;
21
use std::path::PathBuf;
32
use std::sync::Mutex;
43

54
use gethostname::gethostname;
65
use rocket::response::NamedFile;
76
use rocket::State;
87
use rocket_contrib::json::JsonValue;
9-
use uuid::Uuid;
108

119
use crate::config::AWConfig;
12-
use crate::dirs;
1310

1411
#[macro_export]
1512
macro_rules! endpoints_get_lock {
@@ -36,6 +33,7 @@ use aw_datastore::Datastore;
3633
pub 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

aw-server/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ extern crate getopts;
44

55
use getopts::Options;
66
use rocket::config::Environment;
7+
use uuid::Uuid;
8+
79
use std::env;
10+
use std::fs;
811

912
use aw_server::*;
1013

@@ -19,6 +22,20 @@ fn print_usage(program: &str, opts: Options) {
1922
print!("{}", opts.usage(&brief));
2023
}
2124

25+
/// Retrieves the device ID, if none exists it generates one (using UUID v4)
26+
fn get_device_id() -> String {
27+
// I chose get_data_dir over get_config_dir since the latter isn't yet supported on Android.
28+
let mut path = dirs::get_data_dir().unwrap();
29+
path.push("device_id");
30+
if path.exists() {
31+
fs::read_to_string(path).unwrap()
32+
} else {
33+
let uuid = Uuid::new_v4().to_hyphenated().to_string();
34+
fs::write(path, &uuid).unwrap();
35+
uuid
36+
}
37+
}
38+
2239
fn main() {
2340
use std::sync::Mutex;
2441

@@ -67,6 +84,7 @@ fn main() {
6784
// it will not happen there
6885
datastore: Mutex::new(aw_datastore::Datastore::new(db_path, true)),
6986
asset_path,
87+
device_id: get_device_id(),
7088
};
7189

7290
endpoints::build_rocket(server_state, config).launch();

aw-server/tests/api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod api_tests {
2626
let state = endpoints::ServerState {
2727
datastore: Mutex::new(aw_datastore::Datastore::new_in_memory(false)),
2828
asset_path: PathBuf::from("aw-webui/dist"),
29+
device_id: "test_id".to_string(),
2930
};
3031
let aw_config = config::AWConfig::default();
3132
endpoints::build_rocket(state, aw_config)

0 commit comments

Comments
 (0)