Skip to content
Permalink
Browse files

Use http caching for frontend resources:

- use rocket-cache-response to implement http caching
- remove unused code
  • Loading branch information...
schrieveslaach committed Feb 20, 2019
1 parent 86ec33e commit e3d051b09bd28427d03b70c1395edea52c681cf0
Showing with 24 additions and 35 deletions.
  1. +10 −0 api/Cargo.lock
  2. +1 −0 api/Cargo.toml
  3. +13 −35 api/src/main.rs

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -26,6 +26,7 @@ toml = "0.4"
regex = "1.0"
rocket = "0.4"
rocket_contrib = "0.4"
rocket-cache-response = "0.5"
url = "1.7"

[dependencies.shiplift]
@@ -38,56 +38,34 @@ extern crate serde_derive;
use crate::models::request_info::RequestInfo;
use crate::services::config_service::Config;
use rocket::response::NamedFile;
use rocket_cache_response::CacheResponse;
use serde_yaml::{from_reader, to_string, Value};
use shiplift::{ContainerListOptions, Docker};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process;
use tokio::prelude::Future;
use tokio::runtime::Runtime;

mod apps;
mod commands;
mod models;
mod services;
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("/")]
fn index() -> Option<NamedFile> {
NamedFile::open(Path::new("frontend/index.html")).ok()
fn index() -> CacheResponse<Option<NamedFile>> {
CacheResponse::Public {
responder: NamedFile::open(Path::new("frontend/index.html")).ok(),
max_age: 60 * 60, // cached for seconds
must_revalidate: false,
}
}

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

#[get("/")]

0 comments on commit e3d051b

Please sign in to comment.
You can’t perform that action at this time.