Skip to content

Commit

Permalink
aw-server: Fix compilation on android
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Aug 6, 2020
1 parent b14a173 commit d985c51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 2 additions & 0 deletions aw-server/src/android/mod.rs
Expand Up @@ -7,6 +7,7 @@ use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::sync::Mutex;

use crate::device_id;
use crate::dirs;

use android_logger::Config;
Expand Down Expand Up @@ -111,6 +112,7 @@ pub mod android {
let server_state = endpoints::ServerState {
datastore: Mutex::new(openDatastore()),
asset_path: PathBuf::from(asset_path),
device_id: device_id::get_device_id(),
};

let mut config = AWConfig::default();
Expand Down
19 changes: 19 additions & 0 deletions aw-server/src/device_id.rs
@@ -0,0 +1,19 @@
use std::fs;

use uuid::Uuid;

use crate::dirs;

/// Retrieves the device ID, if none exists it generates one (using UUID v4)
pub 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
}
}
1 change: 1 addition & 0 deletions aw-server/src/lib.rs
Expand Up @@ -31,6 +31,7 @@ extern crate toml;
#[macro_use]
pub mod macros;
pub mod config;
pub mod device_id;
pub mod dirs;
pub mod endpoints;
pub mod logging;
Expand Down
18 changes: 1 addition & 17 deletions aw-server/src/main.rs
Expand Up @@ -4,10 +4,8 @@ extern crate getopts;

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

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

use aw_server::*;

Expand All @@ -22,20 +20,6 @@ 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 @@ -84,7 +68,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(),
device_id: device_id::get_device_id(),
};

endpoints::build_rocket(server_state, config).launch();
Expand Down

0 comments on commit d985c51

Please sign in to comment.