refactor(handlers): Embed static files into binary

This commit is contained in:
Vincent Ambo 2018-05-19 00:11:10 +02:00 committed by Vincent Ambo
parent cda66c4cd3
commit 98f9c5dd94
2 changed files with 26 additions and 13 deletions

View file

@ -24,13 +24,15 @@
//! project root. //! project root.
use actix::prelude::*; use actix::prelude::*;
use actix_web;
use actix_web::*; use actix_web::*;
use actix_web::http::Method;
use actix_web::middleware::identity::RequestIdentity; use actix_web::middleware::identity::RequestIdentity;
use actix_web::middleware::{Started, Middleware}; use actix_web::middleware::{Started, Middleware};
use actix_web;
use db::*; use db::*;
use errors::ConverseError; use errors::ConverseError;
use futures::Future; use futures::Future;
use mime_guess::guess_mime_type;
use models::*; use models::*;
use oidc::*; use oidc::*;
use render::*; use render::*;
@ -296,6 +298,25 @@ pub fn callback(state: State<AppState>,
.responder() .responder()
} }
/// This is an extension trait to enable easy serving of embedded
/// static content.
///
/// It is intended to be called with `include_bytes!()` when setting
/// up the actix-web application.
pub trait EmbeddedFile {
fn static_file(self, path: &'static str, content: &'static [u8]) -> Self;
}
impl EmbeddedFile for App<AppState> {
fn static_file(self, path: &'static str, content: &'static [u8]) -> Self {
self.route(path, Method::GET, move |_: HttpRequest<_>| {
let mime = format!("{}", guess_mime_type(path));
HttpResponse::Ok()
.content_type(mime.as_str())
.body(content)
})
}
}
/// Middleware used to enforce logins unceremoniously. /// Middleware used to enforce logins unceremoniously.
pub struct RequireLogin; pub struct RequireLogin;

View file

@ -36,6 +36,7 @@ extern crate env_logger;
extern crate futures; extern crate futures;
extern crate hyper; extern crate hyper;
extern crate md5; extern crate md5;
extern crate mime_guess;
extern crate r2d2; extern crate r2d2;
extern crate rand; extern crate rand;
extern crate reqwest; extern crate reqwest;
@ -78,7 +79,6 @@ use oidc::OidcExecutor;
use rand::{OsRng, Rng}; use rand::{OsRng, Rng};
use render::Renderer; use render::Renderer;
use std::env; use std::env;
use std::path::PathBuf;
use tera::Tera; use tera::Tera;
fn config(name: &str) -> String { fn config(name: &str) -> String {
@ -174,15 +174,6 @@ fn start_http_server(base_url: String,
let bind_host = config_default("CONVERSE_BIND_HOST", "127.0.0.1:4567"); let bind_host = config_default("CONVERSE_BIND_HOST", "127.0.0.1:4567");
let key = gen_session_key(); let key = gen_session_key();
let require_login = config_default("REQUIRE_LOGIN", "true".into()) == "true"; let require_login = config_default("REQUIRE_LOGIN", "true".into()) == "true";
let static_dir = env::var("CONVERSE_STATIC_DIR")
.map(PathBuf::from)
.or_else(|_| env::current_dir().map(|mut p| {
p.push("static");
p
}))
.expect("Could not determine static file directory");
info!("Serving static files from {:?}", static_dir);
server::new(move || { server::new(move || {
let state = AppState { let state = AppState {
@ -201,7 +192,6 @@ fn start_http_server(base_url: String,
let app = App::with_state(state) let app = App::with_state(state)
.middleware(Logger::default()) .middleware(Logger::default())
.middleware(identity) .middleware(identity)
.handler("/static", fs::StaticFiles::new(static_dir.clone()))
.resource("/", |r| r.method(Method::GET).with(forum_index)) .resource("/", |r| r.method(Method::GET).with(forum_index))
.resource("/thread/new", |r| r.method(Method::GET).with(new_thread)) .resource("/thread/new", |r| r.method(Method::GET).with(new_thread))
.resource("/thread/submit", |r| r.method(Method::POST).with3(submit_thread)) .resource("/thread/submit", |r| r.method(Method::POST).with3(submit_thread))
@ -211,7 +201,9 @@ fn start_http_server(base_url: String,
.resource("/post/edit", |r| r.method(Method::POST).with3(edit_post)) .resource("/post/edit", |r| r.method(Method::POST).with3(edit_post))
.resource("/search", |r| r.method(Method::GET).with2(search_forum)) .resource("/search", |r| r.method(Method::GET).with2(search_forum))
.resource("/oidc/login", |r| r.method(Method::GET).with(login)) .resource("/oidc/login", |r| r.method(Method::GET).with(login))
.resource("/oidc/callback", |r| r.method(Method::POST).with3(callback)); .resource("/oidc/callback", |r| r.method(Method::POST).with3(callback))
.static_file("/static/highlight.css", include_bytes!("../static/highlight.css"))
.static_file("/static/highlight.js", include_bytes!("../static/highlight.js"));
if require_login { if require_login {
app.middleware(RequireLogin) app.middleware(RequireLogin)