Ported version endpoint to rocket

This commit is contained in:
Antoine Gersant 2018-10-27 15:19:31 -07:00
parent 43894d71f7
commit 33ae1c07b2
4 changed files with 17 additions and 13 deletions

2
Cargo.lock generated
View file

@ -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]]

View file

@ -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"

View file

@ -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<rocket::Route> {
routes![version]
}
pub fn get_handler(db: &Arc<DB>, index: &Arc<Mutex<Sender<index::Command>>>) -> Result<Chain> {
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<DB>, index_channel: &Arc<Mutex<Sender<index::Command>>
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<Response> {
#[derive(Serialize)]
struct Version {
major: i32,
minor: i32,
}
#[derive(Serialize)]
struct Version {
major: i32,
minor: i32,
}
#[get("/version")]
fn version() -> Json<Version> {
let current_version = Version {
major: CURRENT_MAJOR_VERSION,
minor: CURRENT_MINOR_VERSION,
};
match serde_json::to_string(&current_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<Response> {

View file

@ -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