Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aw_server"
version = "0.1.0"
version = "0.8.0"
authors = ["Johan Bjäreholt <johan@bjareho.lt>", "Erik Bjäreholt <erik@bjareho.lt>"]
edition = "2018"
default-run = "aw-server-rust"
Expand Down Expand Up @@ -37,6 +37,7 @@ fern = { version = "0.5", features = ["colored"] }
toml = "0.5"
gethostname = "0.2"
regex = "1.0"
uuid = { version = "0.8", features = ["serde", "v4"] }

[target.'cfg(target_os="android")'.dependencies]
jni = { version = "0.5", default-features = false }
Expand Down
23 changes: 22 additions & 1 deletion src/endpoints/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;

Expand All @@ -6,7 +7,9 @@ use rocket::response::{NamedFile};
use rocket::State;
use rocket_contrib::json::JsonValue;
use gethostname::gethostname;
use uuid::Uuid;

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

#[macro_export]
Expand Down Expand Up @@ -60,6 +63,23 @@ 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");
Comment thread
johan-bjareholt marked this conversation as resolved.
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() -> JsonValue {
let testing : bool;
Expand All @@ -78,7 +98,8 @@ fn server_info() -> JsonValue {
json!({
"hostname": hostname,
"version": format!("aw-server-rust v{}", VERSION.unwrap_or("(unknown)")),
"testing": testing
"testing": testing,
"device_id": get_device_id(),
})
}

Expand Down