[#44] Added frontend hosting using feature

pull/53/head
Jef Roosens 2021-04-29 11:34:35 +02:00
parent e0ac8ddd24
commit 5a95ee5270
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 21 additions and 5 deletions

View File

@ -4,6 +4,10 @@ version = "1.0.2"
authors = ["Jef Roosens <roosensjef@gmail.com>"]
edition = "2018"
[features]
# Enables hosting of the frontend
frontend = []
[lib]
name = "fej"
path = "src/fej/lib.rs"
@ -41,4 +45,4 @@ diesel_migrations = "1.4.0"
[dependencies.rocket_contrib]
version = "0.4.7"
default-features = false
features = ["json", "diesel_postgres_pool"]
features = ["json", "diesel_postgres_pool", "serve"]

View File

@ -19,7 +19,8 @@ COPY --chown=builder:builder migrations/ ./migrations/
RUN cargo install \
--path . \
--root /app/output \
--target x86_64-unknown-linux-musl
--target x86_64-unknown-linux-musl \
--features frontend
FROM node:15-alpine3.13 AS frontend-builder

View File

@ -20,6 +20,8 @@ use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response, Rocket};
use rocket_contrib::databases::diesel;
#[cfg(feature = "frontend")]
use rocket_contrib::serve::StaticFiles;
pub struct CORS;
@ -60,12 +62,21 @@ fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
// This needs to be muted for the frontend feature
let mut rocket = rocket::ignite()
.attach(CORS)
.attach(FejDbConn::fairing())
.attach(AdHoc::on_attach("Database Migrations", run_db_migrations))
.mount("/ivago", routes::ivago())
.register(catchers![catchers::not_found])
.mount("/api/ivago", routes::ivago()) // /api being hardcoded is temporary
.register(catchers![catchers::not_found]);
// TODO make all of this not hard-coded
#[cfg(feature = "frontend")]
{
rocket = rocket.mount("/", StaticFiles::from("/app/dist"));
}
rocket
}
fn main() {