Skip to content

Commit

Permalink
aw-server: Make UUID part of ServerState
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Aug 6, 2020
1 parent a72cefe commit 7f6f63d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions aw-client-rust/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod test {
let state = ServerState {
datastore: Mutex::new(aw_datastore::Datastore::new_in_memory(false)),
asset_path: PathBuf::from("."), // webui won't be used, so it's invalidly set
device_id: "test_id".to_string(),
};
let mut aw_config = aw_server::config::AWConfig::default();
aw_config.port = PORT;
Expand Down
25 changes: 3 additions & 22 deletions aw-server/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;

use gethostname::gethostname;
use rocket::response::NamedFile;
use rocket::State;
use rocket_contrib::json::JsonValue;
use uuid::Uuid;

use crate::config::AWConfig;
use crate::dirs;

#[macro_export]
macro_rules! endpoints_get_lock {
Expand All @@ -36,6 +33,7 @@ use aw_datastore::Datastore;
pub struct ServerState {
pub datastore: Mutex<Datastore>,
pub asset_path: PathBuf,
pub device_id: String,
}

#[get("/")]
Expand Down Expand Up @@ -68,25 +66,8 @@ fn root_favicon(state: State<ServerState>) -> Option<NamedFile> {
NamedFile::open(state.asset_path.join("favicon.ico")).ok()
}

/// Retrieves the device ID, if none exists it generates one (using UUID v4)
fn get_device_id() -> String {
// TODO: Cache to avoid retrieving on every /info call
// TODO: How should these unwraps be removed?
// Should this be propagated into a 500 Internal Server Error? How?
// I chose get_data_dir over get_config_dir since the latter isn't yet supported on Android.
let mut path = dirs::get_data_dir().unwrap();
path.push("device_id");
if path.exists() {
fs::read_to_string(path).unwrap()
} else {
let uuid = Uuid::new_v4().to_hyphenated().to_string();
fs::write(path, &uuid).unwrap();
uuid
}
}

#[get("/")]
fn server_info(config: State<AWConfig>) -> JsonValue {
fn server_info(config: State<AWConfig>, state: State<ServerState>) -> JsonValue {
#[allow(clippy::or_fun_call)]
let hostname = gethostname().into_string().unwrap_or("unknown".to_string());
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
Expand All @@ -95,7 +76,7 @@ fn server_info(config: State<AWConfig>) -> JsonValue {
"hostname": hostname,
"version": format!("v{} (rust)", VERSION.unwrap_or("(unknown)")),
"testing": config.testing,
"device_id": get_device_id(),
"device_id": state.device_id,
})
}

Expand Down
18 changes: 18 additions & 0 deletions aw-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ extern crate getopts;

use getopts::Options;
use rocket::config::Environment;
use uuid::Uuid;

use std::env;
use std::fs;

use aw_server::*;

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

/// Retrieves the device ID, if none exists it generates one (using UUID v4)
fn get_device_id() -> String {
// I chose get_data_dir over get_config_dir since the latter isn't yet supported on Android.
let mut path = dirs::get_data_dir().unwrap();
path.push("device_id");
if path.exists() {
fs::read_to_string(path).unwrap()
} else {
let uuid = Uuid::new_v4().to_hyphenated().to_string();
fs::write(path, &uuid).unwrap();
uuid
}
}

fn main() {
use std::sync::Mutex;

Expand Down Expand Up @@ -67,6 +84,7 @@ fn main() {
// it will not happen there
datastore: Mutex::new(aw_datastore::Datastore::new(db_path, true)),
asset_path,
device_id: get_device_id(),
};

endpoints::build_rocket(server_state, config).launch();
Expand Down
1 change: 1 addition & 0 deletions aw-server/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod api_tests {
let state = endpoints::ServerState {
datastore: Mutex::new(aw_datastore::Datastore::new_in_memory(false)),
asset_path: PathBuf::from("aw-webui/dist"),
device_id: "test_id".to_string(),
};
let aw_config = config::AWConfig::default();
endpoints::build_rocket(state, aw_config)
Expand Down

0 comments on commit 7f6f63d

Please sign in to comment.