I have a trillium server that is backed by create-react-app.
In dev mode I would like to proxy directly to the server created by create-react-app, while in release mode I would like to serve the file that is embedded in the binary. Working sample of this can be found at https://github.com/prabirshrestha/objstor/pull/16.
Here is the main snippet:
use trillium::Handler;
#[cfg(not(debug_assertions))]
lazy_static_include::lazy_static_include_str! {
INDEX_HTML => "../client/build/index.html",
}
#[cfg(debug_assertions)]
pub fn spa() -> impl Handler {
use trillium_rustls::RustlsConnector;
use trillium_tokio::TcpConnector;
type Proxy = trillium_proxy::Proxy<RustlsConnector<TcpConnector>>;
Proxy::new("http://localhost:3000")
}
#[cfg(not(debug_assertions))]
pub fn spa() -> impl Handler {
use trillium_static_compiled::static_compiled;
let handler = static_compiled!("../client/build").with_index_file("index.html");
(handler, serve_index_file)
}
#[cfg(not(debug_assertions))]
async fn serve_index_file(conn: trillium::Conn) -> trillium::Conn {
conn.with_header("content-type", "text/html; charset=UTF-8")
.ok(INDEX_HTML.as_bytes())
}
I had to introduce lazy_static_include
library and do a bit of custom code. It would be great if this was simplified as it is a common pattern for single page applications where there is usually just a single html file with most of the code logic in js. This could then be simplified as following.
#[cfg(not(debug_assertions))]
pub fn spa() -> impl Handler {
trillium_static_compiled::static_compiled!
("../client/build").with_index_file("index.html").with_fallback_file("index.html")
}
The other option would be to introduce static_compiled_file
but then the file contents would be duplicated.
enhancement