diff --git a/Cargo.lock b/Cargo.lock index 2b4a361..1b47801 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1839,6 +1839,8 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket.git?rev=556206e)", + "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9f39ce2..83b1393 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ rev = "556206e" [dependencies.rocket_contrib] version = "0.4.0-dev" default_features = false -features = ["serve"] +features = ["json", "serve"] [dependencies.rusqlite] version = "0.14.0" diff --git a/src/api.rs b/src/api.rs index f1db58f..0766847 100644 --- a/src/api.rs +++ b/src/api.rs @@ -7,6 +7,7 @@ use iron::prelude::*; use iron::{status, AroundMiddleware, Handler}; use mount::Mount; use params; +use rocket_contrib::json::Json; use router::Router; use secure_session::middleware::{SessionConfig, SessionMiddleware}; use secure_session::session::ChaCha20Poly1305SessionManager; @@ -66,6 +67,10 @@ where Ok(secret) } +pub fn get_routes() -> Vec { + routes![version] +} + pub fn get_handler(db: &Arc, index: &Arc>>) -> Result { let api_handler = get_endpoints(&db.clone(), &index); let mut api_chain = Chain::new(api_handler); @@ -87,7 +92,6 @@ fn get_endpoints(db: &Arc, index_channel: &Arc> let mut api_handler = Mount::new(); { - api_handler.mount("/version/", self::version); { let db = db.clone(); api_handler.mount("/auth/", move |request: &mut Request| { @@ -373,22 +377,19 @@ impl Handler for AdminHandler { } } -fn version(_: &mut Request) -> IronResult { - #[derive(Serialize)] - struct Version { - major: i32, - minor: i32, - } +#[derive(Serialize)] +struct Version { + major: i32, + minor: i32, +} +#[get("/version")] +fn version() -> Json { let current_version = Version { major: CURRENT_MAJOR_VERSION, minor: CURRENT_MINOR_VERSION, }; - - match serde_json::to_string(¤t_version) { - Ok(result_json) => Ok(Response::with((status::Ok, result_json))), - Err(e) => Err(IronError::new(e, status::InternalServerError)), - } + Json(current_version) } fn initial_setup(_: &mut Request, db: &DB) -> IronResult { diff --git a/src/main.rs b/src/main.rs index 7c32db4..60f9357 100644 --- a/src/main.rs +++ b/src/main.rs @@ -269,6 +269,7 @@ fn run() -> Result<()> { rocket::ignite() .mount(&static_url, StaticFiles::from(web_dir_path)) + .mount(&api_url, api::get_routes()) .launch(); // Start DDNS updates