Skip to content

Commit

Permalink
Use http caching for frontend resources:
Browse files Browse the repository at this point in the history
- use rocket-cache-response to implement http caching
- remove unused code
  • Loading branch information
schrieveslaach committed Feb 20, 2019
1 parent 86ec33e commit e3d051b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
10 changes: 10 additions & 0 deletions api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/Cargo.toml
Expand Up @@ -26,6 +26,7 @@ toml = "0.4"
regex = "1.0" regex = "1.0"
rocket = "0.4" rocket = "0.4"
rocket_contrib = "0.4" rocket_contrib = "0.4"
rocket-cache-response = "0.5"
url = "1.7" url = "1.7"


[dependencies.shiplift] [dependencies.shiplift]
Expand Down
48 changes: 13 additions & 35 deletions api/src/main.rs
Expand Up @@ -38,56 +38,34 @@ extern crate serde_derive;
use crate::models::request_info::RequestInfo; use crate::models::request_info::RequestInfo;
use crate::services::config_service::Config; use crate::services::config_service::Config;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket_cache_response::CacheResponse;
use serde_yaml::{from_reader, to_string, Value}; use serde_yaml::{from_reader, to_string, Value};
use shiplift::{ContainerListOptions, Docker};
use std::fs::File; use std::fs::File;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process; use std::process;
use tokio::prelude::Future;
use tokio::runtime::Runtime;


mod apps; mod apps;
mod commands; mod commands;
mod models; mod models;
mod services; mod services;
mod webhooks; mod webhooks;


#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct AppsStatus {
root_url: String,
swagger_ui_available: bool,
portainer_available: bool,
}

fn is_container_available(container_image_pattern: &'static str) -> bool {
let future = Docker::new()
.containers()
.list(&ContainerListOptions::builder().build())
.map(move |containers| {
containers
.iter()
.any(|c| c.image.starts_with(container_image_pattern))
});

let mut runtime = Runtime::new().unwrap();
match runtime.block_on(future) {
Err(e) => {
error!("Cannot list running containers: {}", e);
false
}
Ok(available) => available,
}
}

#[get("/")] #[get("/")]
fn index() -> Option<NamedFile> { fn index() -> CacheResponse<Option<NamedFile>> {
NamedFile::open(Path::new("frontend/index.html")).ok() CacheResponse::Public {
responder: NamedFile::open(Path::new("frontend/index.html")).ok(),
max_age: 60 * 60, // cached for seconds
must_revalidate: false,
}
} }


#[get("/<path..>")] #[get("/<path..>")]
fn files(path: PathBuf) -> Option<NamedFile> { fn files(path: PathBuf) -> CacheResponse<Option<NamedFile>> {
NamedFile::open(Path::new("frontend/").join(path)).ok() CacheResponse::Public {
responder: NamedFile::open(Path::new("frontend/").join(path)).ok(),
max_age: 60 * 60, // cached for seconds
must_revalidate: false,
}
} }


#[get("/")] #[get("/")]
Expand Down

0 comments on commit e3d051b

Please sign in to comment.