|
|
|
|
@@ -4,20 +4,16 @@ use dioxus::{
|
|
|
|
|
server::axum::Extension,
|
|
|
|
|
};
|
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
|
use tower_http::services::{ServeDir, ServeFile};
|
|
|
|
|
use tower_http::services::ServeFile;
|
|
|
|
|
|
|
|
|
|
use crate::App;
|
|
|
|
|
use crate::app::LOGO_ICO;
|
|
|
|
|
use crate::server::{
|
|
|
|
|
auth::build_auth_layer,
|
|
|
|
|
config::Config,
|
|
|
|
|
database::{self, DbPool},
|
|
|
|
|
key_val_store::{self, KeyValPool},
|
|
|
|
|
auth::build_auth_layer, config::Config, database::{self, DbPool}, key_val_store::{self, KeyValPool},
|
|
|
|
|
require_auth_mw::require_auth_middleware,
|
|
|
|
|
};
|
|
|
|
|
use crate::util::error::{Contextualize, Error, ErrorType, Result};
|
|
|
|
|
|
|
|
|
|
// Build the `axum::Router` and attach config, database, and key/val store as extensions
|
|
|
|
|
pub fn build_router(config: Config, db_pool: DbPool, key_val_pool: KeyValPool) -> Router {
|
|
|
|
|
let favicon_path = {
|
|
|
|
|
// Resolve the favicon path
|
|
|
|
|
@@ -33,34 +29,13 @@ pub fn build_router(config: Config, db_pool: DbPool, key_val_pool: KeyValPool) -
|
|
|
|
|
|
|
|
|
|
let auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
|
|
|
|
|
|
|
|
|
|
let public_path = config.server.public_path.clone();
|
|
|
|
|
|
|
|
|
|
let serve_assets = ServeDir::new(public_path.join("assets"));
|
|
|
|
|
let serve_index = ServeFile::new(public_path.join("index.html"));
|
|
|
|
|
let serve_favicon = ServeFile::new(favicon_path);
|
|
|
|
|
|
|
|
|
|
let router = Router::new()
|
|
|
|
|
.serve_api_application(ServeConfig::new(), App)
|
|
|
|
|
Router::new()
|
|
|
|
|
.serve_dioxus_application(ServeConfig::new(), App)
|
|
|
|
|
.layer(from_fn(require_auth_middleware))
|
|
|
|
|
.layer(Extension(config))
|
|
|
|
|
.layer(Extension(db_pool))
|
|
|
|
|
.layer(auth_layer);
|
|
|
|
|
|
|
|
|
|
// In release mode, serve precompressed assets. In debug mode, serve WASM patch files.
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
let serve_wasm = ServeDir::new(public_path.join("wasm"));
|
|
|
|
|
|
|
|
|
|
router
|
|
|
|
|
.nest_service("/wasm", serve_wasm)
|
|
|
|
|
.nest_service("/assets", serve_assets)
|
|
|
|
|
.nest_service("/index.html", serve_index)
|
|
|
|
|
.nest_service("/favicon.ico", serve_favicon)
|
|
|
|
|
} else {
|
|
|
|
|
router
|
|
|
|
|
.nest_service("/assets", serve_assets.precompressed_br())
|
|
|
|
|
.nest_service("/index.html", serve_index)
|
|
|
|
|
.nest_service("/favicon.ico", serve_favicon.precompressed_br())
|
|
|
|
|
}
|
|
|
|
|
.layer(auth_layer)
|
|
|
|
|
.nest_service("/favicon.ico", ServeFile::new(favicon_path))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn main() -> Result<std::convert::Infallible> {
|
|
|
|
|
@@ -76,6 +51,14 @@ pub async fn main() -> Result<std::convert::Infallible> {
|
|
|
|
|
tracing::debug!("Loaded configuration: {config:#?}");
|
|
|
|
|
config.log_warnings();
|
|
|
|
|
|
|
|
|
|
// Dioxus doesn't expose a way to configure the public path other than this environment
|
|
|
|
|
// variable, and also doesn't provide a way to read what the public path is. As a workaround we
|
|
|
|
|
// expose a config option and set this variable that Dioxus expects.
|
|
|
|
|
// SAFETY: "This function is safe to call in a single-threaded program."
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::set_var("DIOXUS_PUBLIC_PATH", config.server.public_path.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let db_pool = database::setup(config.database.connection_uri())
|
|
|
|
|
.await
|
|
|
|
|
.err_context("Failed database setup")?;
|
|
|
|
|
|