about summary refs log tree commit diff
path: root/src/handlers.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-05-18T22·11+0200
committerVincent Ambo <github@tazj.in>2018-05-18T22·52+0200
commit98f9c5dd9436a4e184c2989976a9c7db64780195 (patch)
tree26020aa5c99193185f606505ab721b40edb110e7 /src/handlers.rs
parentcda66c4cd3bf2f6f7a466a9e2246c95a5305bfa9 (diff)
refactor(handlers): Embed static files into binary
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
index df03940b1970..e5f21849fcf6 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -24,13 +24,15 @@
 //! project root.
 
 use actix::prelude::*;
-use actix_web;
 use actix_web::*;
+use actix_web::http::Method;
 use actix_web::middleware::identity::RequestIdentity;
 use actix_web::middleware::{Started, Middleware};
+use actix_web;
 use db::*;
 use errors::ConverseError;
 use futures::Future;
+use mime_guess::guess_mime_type;
 use models::*;
 use oidc::*;
 use render::*;
@@ -296,6 +298,25 @@ pub fn callback(state: State<AppState>,
         .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.
 pub struct RequireLogin;