1
0
Fork 0
forked from TWS/kalkutago

Add static file server to rocket & SPA index redirect

This commit is contained in:
D. Scott Boggs 2023-06-11 08:56:20 -04:00
parent f794f5c974
commit 0b57f140cb
5 changed files with 35 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
**/target
**/*.pw
python-venv/
tickmate-backup-20230524.db

17
Makefile Normal file
View file

@ -0,0 +1,17 @@
all: start-server
client/dist/index.html:
cd client; yarn && yarn build
build-client: client/dist/index.html
server/public/: client/dist/index.html
rsync -a client/dist/ server/public/
start-server: server/public/
docker compose up --build -d
clean:
docker compose down
rm -r server/public/ client/dist/

View file

@ -8,4 +8,5 @@ ADD src/ src/
RUN rm dummy.rs &&\
sed -i "s:dummy.rs:src/main.rs:" Cargo.toml
RUN cargo build --release
ADD public/ public/
CMD ["target/release/kalkulog-server"]

View file

@ -6,18 +6,29 @@ mod tracks;
use std::default::default;
use std::net::{IpAddr, Ipv4Addr};
use rocket::fs::{FileServer, NamedFile};
use rocket::Config;
use sea_orm::DatabaseConnection;
use crate::error::Error;
use crate::rocket::{Build, Rocket};
pub(crate) use error::ErrorResponder;
use self::error::ApiResult;
#[get("/status")]
fn status() -> &'static str {
"Ok"
}
#[catch(404)]
async fn spa_index_redirect() -> ApiResult<NamedFile> {
Ok(NamedFile::open("/src/public/index.html")
.await
.map_err(Error::from)?)
}
pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
use groups::*;
use ticks::*;
@ -27,6 +38,7 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
..default()
})
.register("/", catchers![spa_index_redirect])
.manage(db)
.mount("/api/v1", routes![status])
.mount(
@ -41,4 +53,5 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
"/api/v1/groups",
routes![all_groups, group, insert_group, update_group, delete_group],
)
.mount("/", FileServer::from("/src/public"))
}

View file

@ -6,6 +6,8 @@ pub enum Error {
Builder(#[from] UninitializedFieldError),
#[error(transparent)]
SeaOrm(#[from] sea_orm::DbErr),
#[error(transparent)]
Io(#[from] std::io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;