Skip to content

Commit 674e8dd

Browse files
authored
add catch all for frontend and /api for be (#384)
* add catch all for frontend and /api for be * fix asset loading
1 parent 86cb4ce commit 674e8dd

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

frontend/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ <h3>Identity:</h3>
1717
<script>
1818
async function fetchIdentity() {
1919
try {
20-
const response = await fetch('/identity/detail');
20+
const response = await fetch('/api/identity/detail');
2121
if (!response.ok) {
2222
let resp = await response.json();
2323
throw new Error(`${response.status} - ${resp.error}/${resp.message}`);
@@ -31,5 +31,6 @@ <h3>Identity:</h3>
3131
}
3232
fetchIdentity();
3333
</script>
34+
<script src="./main.js"></script>
3435
</body>
3536
</html>

frontend/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello world');

src/web/handlers/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use super::data::{
55
use crate::{
66
constants::VALID_CURRENCIES,
77
service::{Error, Result, ServiceContext},
8+
CONFIG,
89
};
910
use bill::get_current_identity_node_id;
10-
use rocket::{get, post, serde::json::Json, Shutdown, State};
11+
use rocket::{fs::NamedFile, get, post, serde::json::Json, Shutdown, State};
12+
use std::path::Path;
1113

1214
pub mod bill;
1315
pub mod company;
@@ -17,6 +19,13 @@ pub mod middleware;
1719
pub mod notifications;
1820
pub mod quotes;
1921

22+
#[get("/<_..>", rank = 10)]
23+
pub async fn serve_frontend() -> Option<NamedFile> {
24+
NamedFile::open(Path::new(&CONFIG.frontend_serve_folder).join("index.html"))
25+
.await
26+
.ok()
27+
}
28+
2029
#[get("/")]
2130
pub async fn exit(shutdown: Shutdown, state: &State<ServiceContext>) {
2231
log::info!("Exit called - shutting down...");

src/web/mod.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::service::ServiceContext;
22
use api_docs::ApiDocs;
33
use log::info;
4-
use rocket::fs::FileServer;
54
use rocket::http::{Method, Status};
65
use rocket::{catch, catchers, routes, Build, Config, Request, Rocket};
76
use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions};
@@ -17,6 +16,7 @@ use crate::constants::MAX_FILE_SIZE_BYTES;
1716
use crate::CONFIG;
1817
use rocket::data::ByteUnit;
1918
use rocket::figment::Figment;
19+
use rocket::fs::FileServer;
2020
use rocket::serde::json::Json;
2121
use serde_json::json;
2222

@@ -75,16 +75,16 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
7575

7676
let rocket = rocket::custom(config)
7777
.attach(cors.clone())
78-
.mount("/", rocket_cors::catch_all_options_routes())
78+
.mount("/api/", rocket_cors::catch_all_options_routes())
7979
.register("/", catchers![default_catcher, not_found])
8080
.manage(context)
8181
.manage(cors)
82-
.mount("/exit", routes![handlers::exit])
83-
.mount("/currencies", routes![handlers::currencies])
84-
.mount("/overview", routes![handlers::overview])
85-
.mount("/search", routes![handlers::search])
82+
.mount("/api/exit", routes![handlers::exit])
83+
.mount("/api/currencies", routes![handlers::currencies])
84+
.mount("/api/overview", routes![handlers::overview])
85+
.mount("/api/search", routes![handlers::search])
8686
.mount(
87-
"/identity",
87+
"/api/identity",
8888
routes![
8989
handlers::identity::create_identity,
9090
handlers::identity::change_identity,
@@ -100,11 +100,7 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
100100
],
101101
)
102102
.mount(
103-
&CONFIG.frontend_url_path,
104-
FileServer::from(&CONFIG.frontend_serve_folder),
105-
)
106-
.mount(
107-
"/contacts",
103+
"/api/contacts",
108104
routes![
109105
handlers::contacts::new_contact,
110106
handlers::contacts::edit_contact,
@@ -116,7 +112,7 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
116112
],
117113
)
118114
.mount(
119-
"/company",
115+
"/api/company",
120116
routes![
121117
handlers::company::check_companies_in_dht,
122118
handlers::company::list,
@@ -131,7 +127,7 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
131127
],
132128
)
133129
.mount(
134-
"/bill",
130+
"/api/bill",
135131
routes![
136132
handlers::bill::all_bills_from_all_identities,
137133
handlers::bill::issue_bill,
@@ -166,14 +162,14 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
166162
],
167163
)
168164
.mount(
169-
"/quote",
165+
"/api/quote",
170166
routes![
171167
handlers::quotes::return_quote,
172168
handlers::quotes::accept_quote
173169
],
174170
)
175171
.mount(
176-
"/",
172+
"/api/",
177173
routes![
178174
handlers::notifications::list_notifications,
179175
handlers::notifications::mark_notification_done,
@@ -183,9 +179,15 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
183179
],
184180
)
185181
.mount(
186-
"/",
182+
"/api/",
187183
SwaggerUi::new("/swagger-ui/<_..>").url("/api-docs/openapi.json", ApiDocs::openapi()),
188-
);
184+
)
185+
.mount(
186+
&CONFIG.frontend_url_path,
187+
FileServer::from(&CONFIG.frontend_serve_folder).rank(5),
188+
)
189+
// TODO: fall back to index, but serve static files first
190+
.mount(&CONFIG.frontend_url_path, routes![handlers::serve_frontend]);
189191

190192
info!("HTTP Server Listening on {}", conf.http_listen_url());
191193

0 commit comments

Comments
 (0)