Made backend redirect all urls to index.html

This commit is contained in:
Marko Korhonen 2020-04-21 13:31:31 +03:00
parent 1893085899
commit 3243f5d79b
No known key found for this signature in database
GPG key ID: 911B85FBC6003FE5

View file

@ -10,13 +10,14 @@ pub mod schema;
pub mod utils;
use actix_cors::Cors;
use actix_files::Files;
use actix_files::{Files, NamedFile};
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::{
get,
http::header,
middleware::Logger,
web::{delete, post, resource, scope},
App, HttpServer,
web::{delete, get, post, resource, scope},
App, Error, HttpResponse, HttpServer,
};
use chrono::Duration;
use db_connection::get_pool;
@ -37,6 +38,20 @@ pub fn get_env(var_name: &str) -> String {
};
}
async fn serve_index_html() -> Result<NamedFile, Error> {
Ok(NamedFile::open("./static/index.html")?)
}
#[get("/api")]
async fn api_404() -> HttpResponse {
HttpResponse::NotFound().finish()
}
#[get("/api/{unconfigured_routes:.*}")]
async fn api_404_unconfigured() -> HttpResponse {
HttpResponse::NotFound().finish()
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug,diesel=debug");
@ -73,6 +88,7 @@ async fn main() -> std::io::Result<()> {
.secure(false),
))
.data(get_pool())
.service(api_404)
.service(
scope("/api/auth")
.service(resource("/register").route(post().to(authentication::register)))
@ -80,7 +96,9 @@ async fn main() -> std::io::Result<()> {
.service(resource("/logout").route(post().to(authentication::logout)))
.service(resource("/delete").route(delete().to(authentication::delete))),
)
.service(api_404_unconfigured)
.service(Files::new("/", "./static").index_file("index.html"))
.default_service(get().to(serve_index_html))
})
.bind(address)?
.run()