fix preact routes by serving index.html for any 404

This commit is contained in:
Vivian Lim 2020-07-19 17:28:58 -07:00
parent da384ceeb8
commit 35a912bd7a
1 changed files with 12 additions and 2 deletions

View File

@ -15,7 +15,11 @@ pub mod models;
pub mod schema;
use chrono::prelude::*;
use rocket::{http::RawStr, State};
use rocket::{
http::RawStr,
response::{NamedFile, Redirect},
State,
};
use std::{
collections::BTreeMap,
fmt::Display,
@ -77,6 +81,11 @@ fn log_get(device_name: &RawStr, conn: DbConn) -> Result<String, anyhow::Error>
Ok(lines)
}
#[catch(404)]
fn not_found() -> Option<NamedFile> {
NamedFile::open("frontend/build/index.html").ok()
}
fn main() {
rocket::ignite()
.attach(DbConn::fairing())
@ -84,7 +93,8 @@ fn main() {
"/",
rocket_contrib::serve::StaticFiles::from("frontend/build"),
)
.mount("/", routes![log_new, log_get])
.mount("/api", routes![log_new, log_get])
.register(catchers![not_found])
.launch();
println!("after");