Commit 5baace2b authored by Daniel Brüning's avatar Daniel Brüning Committed by Robert Czechowski
Browse files

Added the functionality to automatically open the web browser when medal gets...

Added the functionality to automatically open the web browser when medal gets started (can be enabled via command line or config.json)
parent 40d475fc
Pipeline #182 passed with stage
in 13 minutes and 2 seconds
......@@ -677,6 +677,7 @@ dependencies = [
"structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
"urlencoded 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"webbrowser 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -1720,6 +1721,20 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "webbrowser"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "widestring"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.2.8"
......@@ -1970,6 +1985,8 @@ dependencies = [
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c66c0b9792f0a765345452775f3adbd28dde9d33f30d13e5dcc5ae17cf6f3780"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"
"checksum webbrowser 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c01efd7cb6939b7f34983f1edff0550e5b21b49e2db4495656295922df8939ac"
"checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
......
......@@ -32,6 +32,8 @@ linked-hash-map = "0.5.1"
bcrypt = "0.3"
webbrowser = "0.5.1"
[dependencies.serde_json]
version = "1.0.20"
features = ["preserve_order"]
......
......@@ -22,6 +22,7 @@ extern crate staticfile;
extern crate structopt;
extern crate time;
extern crate urlencoded;
extern crate webbrowser;
mod db_apply_migrations;
mod db_conn;
......@@ -59,6 +60,7 @@ pub struct Config {
database_url: Option<String>,
template: Option<String>,
no_contest_scan: Option<bool>,
open_browser: Option<bool>,
}
fn read_config_from_file(file: &Path) -> Config {
......@@ -97,6 +99,9 @@ fn read_config_from_file(file: &Path) -> Config {
if config.no_contest_scan.is_none() {
config.no_contest_scan = Some(false)
}
if config.open_browser.is_none() {
config.open_browser = Some(false)
}
println!("OAuth providers will be told to redirect to {}", config.self_url.as_ref().unwrap());
......@@ -133,6 +138,10 @@ struct Opt {
/// Scan for contests without starting medal
#[structopt(short = "s", long = "only-contest-scan")]
onlycontestscan: bool,
/// Automatically open medal in the default browser
#[structopt(short = "b", long = "browser")]
openbrowser: bool,
}
fn read_contest(p: &path::PathBuf) -> Option<Contest> {
......@@ -241,15 +250,30 @@ fn prepare_and_start_server<C>(mut conn: C, config: Config, onlycontestscan: boo
if !onlycontestscan {
add_admin_user(&mut conn, resetadminpw);
let self_url = config.self_url.clone();
let open_browser = config.open_browser;
match start_server(conn, config) {
Ok(_) => println!("Server started"),
Ok(_) => {
println!("Server started");
if let (Some(self_url), Some(true)) = (self_url, open_browser) {
open_browser_window(&self_url);
}
},
Err(_) => println!("Error on server start …"),
};
println!("Could not run server. Is the port already in use?");
}
}
fn open_browser_window(self_url: &str) {
match webbrowser::open(&self_url) {
Ok(_) => (),
Err(e) => println!("Error while opening webbrowser: {:?}", e)
}
}
fn main() {
let opt = Opt::from_args();
//println!("{:?}", opt); // Show in different debug level?
......@@ -268,11 +292,14 @@ fn main() {
if opt.nocontestscan {
config.no_contest_scan = Some(true);
}
if opt.openbrowser {
config.open_browser = Some(true)
}
if config.database_url.is_some() {
let conn =
postgres::Connection::connect(config.database_url.clone().unwrap(), postgres::TlsMode::None).unwrap();
prepare_and_start_server(conn, config, opt.onlycontestscan, opt.resetadminpw);
} else {
let conn = match config.database_file {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment