WIP Custom 404 page.

This is a proposed successor to #296 (rewritten for actix-web, rather
than iron).
This commit is contained in:
Owen Nelson 2018-06-24 12:08:33 -07:00
parent 17106be497
commit 7ab22528a5

View file

@ -22,8 +22,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use std::env; use std::env;
use std::fs::{File, remove_dir_all}; use std::fs::{remove_dir_all, File};
use std::io; use std::io::{self, Read};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::{Instant, Duration}; use std::time::{Instant, Duration};
@ -31,7 +31,8 @@ use std::thread;
use chrono::prelude::*; use chrono::prelude::*;
use actix; use actix;
use actix_web::{fs, server, App, HttpRequest, HttpResponse, Responder}; use actix_web::{self, fs, http, server, App, HttpRequest, HttpResponse, Responder};
use actix_web::middleware::{Middleware, Started, Response};
use notify::{Watcher, RecursiveMode, watcher}; use notify::{Watcher, RecursiveMode, watcher};
use ws::{WebSocket, Sender, Message}; use ws::{WebSocket, Sender, Message};
use ctrlc; use ctrlc;
@ -60,27 +61,30 @@ const LIVE_RELOAD: &'static str = include_str!("livereload.js");
struct ErrCatcher; struct ErrCatcher;
impl AfterMiddleware for ErrCatcher { impl<S> Middleware<S> for ErrCatcher {
fn start(&self, _req: &mut HttpRequest<S>) -> actix_web::Result<Started> {
Ok(Started::Done)
}
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> { fn response(
&self,
let err_status = err.response.status.unwrap_or(status::InternalServerError); _req: &mut HttpRequest<S>,
mut resp: HttpResponse,
// Search for e.g. "static/error/404.html" ) -> actix_web::Result<Response> {
let err_page = File::open( format!("static/error/{}.html", err_status.to_u16()) ); if http::StatusCode::NOT_FOUND == resp.status() {
let not_found_page = "static/error/404.html";
if err_page.is_ok() { if let Ok(mut fh) = File::open(&not_found_page) {
let mut err_res = Response::with(( err_status, err_page.unwrap() )); println!("Using {} to handle missing file.", &not_found_page);
err_res.headers.set(ContentType::html()); let mut buf: Vec<u8> = vec![];
let _ = fh.read_to_end(&mut buf)?;
return Ok(err_res); resp.replace_body(buf);
resp.headers_mut().insert(
http::header::CONTENT_TYPE,
http::header::HeaderValue::from_static("text/html"),
);
}
} }
Ok(Response::Done(resp))
// No custom error page, serve default
let mut err_res = Response::with(( err_status, format!("Error: {}.", err_status) ));
err_res.headers.set(ContentType::plaintext());
Ok(err_res)
} }
} }
@ -180,6 +184,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf
let sys = actix::System::new("http-server"); let sys = actix::System::new("http-server");
server::new(move || { server::new(move || {
App::new() App::new()
.middleware(ErrCatcher)
.resource(r"/livereload.js", |r| r.f(livereload_handler)) .resource(r"/livereload.js", |r| r.f(livereload_handler))
// Start a webserver that serves the `output_dir` directory // Start a webserver that serves the `output_dir` directory
.handler(r"/", fs::StaticFiles::new(&static_root) .handler(r"/", fs::StaticFiles::new(&static_root)