1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#![feature(option_expect_none)]
#![feature(fn_traits)]
#![feature(unboxed_closures)]
#![warn(anonymous_parameters)]
#![warn(bare_trait_objects)]
#![warn(elided_lifetimes_in_paths)]
#![warn(missing_debug_implementations)]
#![warn(single_use_lifetimes)]
#![warn(trivial_casts)]
#![warn(unreachable_pub)]
#![forbid(unsafe_code)]
#![warn(unused_extern_crates)]
#![warn(unused_import_braces)]
#![warn(unused_qualifications)]
#![warn(unused_results)]
#![warn(variant_size_differences)]
#![warn(missing_docs)]
#![doc(html_favicon_url = "https://raw.githubusercontent.com/KyleU/dbui/master/dbui-assets/embed/favicon.ico")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/KyleU/dbui/master/dbui-assets/embed/favicon.png")]
#![doc(issue_tracker_base_url = "https://github.com/kyleu/dbui/issues/")]

//! `dbui` is a work in progress. Docs soon.

mod args;
mod log;
mod server;

#[cfg(test)]
pub mod tests;

use dbui_service::AppConfig;

#[cfg(debug_assertions)]
fn is_debug() -> bool {
  true
}

#[cfg(not(debug_assertions))]
fn is_debug() -> bool {
  false
}

/// Application entrypoint, creates and starts the server
pub fn go() -> std::io::Result<()> {
  let matches = args::get_matches();
  let verbose = matches.is_present("verbose") || is_debug();
  let log = crate::log::root_logger(verbose);
  let cfg_dir = match matches.value_of("config") {
    None => default_cfg_dir(&log),
    Some(o) => o.into()
  };
  let address = match matches.value_of("address") {
    None => "127.0.0.1",
    Some(o) => o
  };
  let port: i32 = match matches.value_of("port") {
    Some(p) => match p.parse() {
      Ok(p) => p,
      Err(_) => {
        slog::warn!(log, "Can't parse port number from [{}]", p);
        4200
      }
    },
    None => 4200
  };
  let cfg = AppConfig::new(cfg_dir, address.into(), port, verbose, log);
  server::start_server(cfg)
}

fn default_cfg_dir(log: &slog::Logger) -> String {
  let app_info = app_dirs2::AppInfo {
    name: dbui_core::APPNAME,
    author: "kyleu"
  };
  let ret: String = match app_dirs2::get_app_root(app_dirs2::AppDataType::UserConfig, &app_info) {
    Ok(d) => d.to_string_lossy().into(),
    Err(e) => {
      slog::warn!(log, "Cannot find default config directory: [{}]", e);
      ".".into()
    }
  };
  ret
}

/// External app entrypoint, calls `go()` directly and swallows errors
#[no_mangle]
pub extern "C" fn libgo() {
  match go() {
    Ok(_) => println!("Successfully started [{}]", dbui_core::APPNAME),
    Err(e) => println!("Error starting [{}]: {}", dbui_core::APPNAME, e)
  };
}

#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
  extern crate jni;

  use self::jni::objects::JClass;
  use self::jni::JNIEnv;
  use super::go;

  #[no_mangle]
  pub unsafe extern "C" fn Java_com_kyleu_dbui_Dbui_go(env: JNIEnv, _: JClass) {
    println!("Android!");
    go();
  }
}