Implemented version endpoint

This commit is contained in:
Antoine Gersant 2020-01-14 22:40:36 -08:00
parent e248f3b983
commit 052dc88f14
9 changed files with 62 additions and 17 deletions

13
src/service/actix/api.rs Normal file
View file

@ -0,0 +1,13 @@
use actix_web::{get, HttpResponse};
use crate::service::constants::*;
use crate::service::dto;
#[get("/version")]
pub async fn get_version() -> Result<HttpResponse, actix_web::Error> {
let current_version = dto::Version {
major: CURRENT_MAJOR_VERSION,
minor: CURRENT_MINOR_VERSION,
};
Ok(HttpResponse::Ok().json(current_version))
}

View file

@ -4,6 +4,7 @@ use std::path::Path;
pub mod server;
mod api;
#[cfg(test)]
mod tests;
@ -15,6 +16,7 @@ fn configure_app(
swagger_dir_path: &Path,
) {
// TODO logging
cfg.service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html"))
cfg.service(web::scope("/api").service(api::get_version))
.service(fs::Files::new(swagger_url, swagger_dir_path).index_file("index.html"))
.service(fs::Files::new(web_url, web_dir_path).index_file("index.html"));
}

View file

@ -0,0 +1,23 @@
use actix_web::body::Body::Bytes;
use actix_web::dev::*;
use actix_web::test::TestRequest;
use actix_web::{test, App};
use crate::service::dto;
#[actix_rt::test]
async fn test_version() {
let app = App::new().configure(super::configure_test_app);
let mut service = test::init_service(app).await;
let req = TestRequest::get().uri("/api/version").to_request();
let resp = service.call(req).await.unwrap();
assert!(resp.status().is_success());
let body = match resp.response().body().as_ref() {
Some(Bytes(bytes)) => bytes,
_ => panic!("Response error"),
};
let response_json: dto::Version = serde_json::from_slice(body).unwrap();
assert_eq!(response_json, dto::Version { major: 4, minor: 0 });
}

View file

@ -1,5 +1,6 @@
use std::path::PathBuf;
mod api;
mod swagger;
mod web;

5
src/service/constants.rs Normal file
View file

@ -0,0 +1,5 @@
pub const CURRENT_MAJOR_VERSION: i32 = 4;
pub const CURRENT_MINOR_VERSION: i32 = 0;
pub const COOKIE_SESSION: &str = "session";
pub const COOKIE_USERNAME: &str = "username";
pub const COOKIE_ADMIN: &str = "admin";

7
src/service/dto.rs Normal file
View file

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Version {
pub major: i32,
pub minor: i32,
}

View file

@ -1,3 +1,6 @@
mod constants;
mod dto;
#[cfg(feature = "service-actix")]
mod actix;
#[cfg(feature = "service-actix")]

View file

@ -20,17 +20,13 @@ use crate::db::DB;
use crate::index;
use crate::lastfm;
use crate::playlist;
use crate::service::constants::*;
use crate::service::dto;
use crate::thumbnails;
use crate::user;
use crate::utils;
use crate::vfs::VFSSource;
const CURRENT_MAJOR_VERSION: i32 = 4;
const CURRENT_MINOR_VERSION: i32 = 0;
const COOKIE_SESSION: &str = "session";
const COOKIE_USERNAME: &str = "username";
const COOKIE_ADMIN: &str = "admin";
pub fn get_routes() -> Vec<rocket::Route> {
routes![
version,
@ -207,15 +203,9 @@ impl From<VFSPathBuf> for PathBuf {
}
}
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Version {
pub major: i32,
pub minor: i32,
}
#[get("/version")]
fn version() -> Json<Version> {
let current_version = Version {
fn version() -> Json<dto::Version> {
let current_version = dto::Version {
major: CURRENT_MAJOR_VERSION,
minor: CURRENT_MINOR_VERSION,
};

View file

@ -8,6 +8,7 @@ use super::api;
use crate::config;
use crate::ddns;
use crate::index;
use crate::service::dto;
use crate::vfs;
use super::test::get_test_environment;
@ -57,8 +58,8 @@ fn version() {
assert_eq!(response.status(), Status::Ok);
let response_body = response.body_string().unwrap();
let response_json: api::Version = serde_json::from_str(&response_body).unwrap();
assert_eq!(response_json, api::Version { major: 4, minor: 0 });
let response_json: dto::Version = serde_json::from_str(&response_body).unwrap();
assert_eq!(response_json, dto::Version { major: 4, minor: 0 });
}
#[test]