Compare commits
3 Commits
328b23f305
...
c4cba925a7
| Author | SHA1 | Date | |
|---|---|---|---|
|
c4cba925a7
|
|||
|
dcbac8a9ba
|
|||
|
5e498195ce
|
@@ -18,3 +18,6 @@ script = []
|
|||||||
# Javascript code file
|
# Javascript code file
|
||||||
# serve: [dev-server] only
|
# serve: [dev-server] only
|
||||||
script = []
|
script = []
|
||||||
|
|
||||||
|
[web]
|
||||||
|
pre_compress = true
|
||||||
|
|||||||
@@ -4,16 +4,65 @@ use dioxus::{
|
|||||||
server::axum::Extension,
|
server::axum::Extension,
|
||||||
};
|
};
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tower_http::services::ServeFile;
|
use tower_http::services::{ServeDir, ServeFile};
|
||||||
|
|
||||||
use crate::App;
|
use crate::App;
|
||||||
use crate::app::LOGO_ICO;
|
use crate::app::LOGO_ICO;
|
||||||
use crate::server::{
|
use crate::server::{
|
||||||
auth::build_auth_layer, config::Config, database, key_val_store,
|
auth::build_auth_layer,
|
||||||
|
config::Config,
|
||||||
|
database::{self, DbPool},
|
||||||
|
key_val_store::{self, KeyValPool},
|
||||||
require_auth_mw::require_auth_middleware,
|
require_auth_mw::require_auth_middleware,
|
||||||
};
|
};
|
||||||
use crate::util::error::{Contextualize, Error, ErrorType, Result};
|
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
|
||||||
|
let asset_path = LOGO_ICO.resolve();
|
||||||
|
|
||||||
|
// If the asset path starts with "/", strip it. Otherwise it behaves as an "absolute path"
|
||||||
|
// and replaces the base path in the join operation. This is necessary because Dioxus will
|
||||||
|
// produce a path like "/assets/" in the call to `resolve`
|
||||||
|
let asset_path_rel = asset_path.strip_prefix("/").unwrap_or(&asset_path);
|
||||||
|
|
||||||
|
config.server.public_path.join(asset_path_rel)
|
||||||
|
};
|
||||||
|
|
||||||
|
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)
|
||||||
|
.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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn main() -> Result<std::convert::Infallible> {
|
pub async fn main() -> Result<std::convert::Infallible> {
|
||||||
if let Err(e) = dotenvy::dotenv() {
|
if let Err(e) = dotenvy::dotenv() {
|
||||||
tracing::warn!("Error reading .env: {e}");
|
tracing::warn!("Error reading .env: {e}");
|
||||||
@@ -27,14 +76,6 @@ pub async fn main() -> Result<std::convert::Infallible> {
|
|||||||
tracing::debug!("Loaded configuration: {config:#?}");
|
tracing::debug!("Loaded configuration: {config:#?}");
|
||||||
config.log_warnings();
|
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())
|
let db_pool = database::setup(config.database.connection_uri())
|
||||||
.await
|
.await
|
||||||
.err_context("Failed database setup")?;
|
.err_context("Failed database setup")?;
|
||||||
@@ -43,33 +84,10 @@ pub async fn main() -> Result<std::convert::Infallible> {
|
|||||||
.await
|
.await
|
||||||
.err_context("Failed key-value store setup")?;
|
.err_context("Failed key-value store setup")?;
|
||||||
|
|
||||||
let favicon_path = {
|
|
||||||
// Resolve the favicon path
|
|
||||||
let asset_path = LOGO_ICO.resolve();
|
|
||||||
|
|
||||||
// If the asset path starts with "/", strip it. Otherwise it behaves as an "absolute path"
|
|
||||||
// and replaces the base path in the join operation. This is necessary because Dioxus will
|
|
||||||
// produce a path like "/assets/" in the call to `resolve`
|
|
||||||
let asset_path_rel = asset_path.strip_prefix("/").unwrap_or(&asset_path);
|
|
||||||
|
|
||||||
config.server.public_path.join(asset_path_rel)
|
|
||||||
};
|
|
||||||
|
|
||||||
tracing::debug!("Favicon path: {}", favicon_path.display());
|
|
||||||
|
|
||||||
let auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
|
|
||||||
|
|
||||||
let addr = config.server.serve_addr();
|
let addr = config.server.serve_addr();
|
||||||
|
|
||||||
tracing::info!("Setup complete, building router...");
|
tracing::info!("Setup complete, building router...");
|
||||||
|
let router = build_router(config, db_pool, key_val_pool);
|
||||||
let router = Router::new()
|
|
||||||
.serve_dioxus_application(ServeConfig::new(), App)
|
|
||||||
.layer(from_fn(require_auth_middleware))
|
|
||||||
.layer(Extension(config))
|
|
||||||
.layer(Extension(db_pool))
|
|
||||||
.layer(auth_layer)
|
|
||||||
.nest_service("/favicon.ico", ServeFile::new(favicon_path));
|
|
||||||
|
|
||||||
tracing::info!("Listening on {addr}...");
|
tracing::info!("Listening on {addr}...");
|
||||||
let listener = TcpListener::bind(addr)
|
let listener = TcpListener::bind(addr)
|
||||||
|
|||||||
@@ -7,12 +7,6 @@ use dioxus::prelude::*;
|
|||||||
|
|
||||||
use crate::server::auth::AuthSession;
|
use crate::server::auth::AuthSession;
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
const ALLOWED_PATH_PREFIX: [&str; 1] = ["/assets/"];
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
const ALLOWED_PATH_PREFIX: [&str; 2] = ["/assets/", "/wasm/"];
|
|
||||||
|
|
||||||
const ALLOWED_PATHS: [&str; 6] = [
|
const ALLOWED_PATHS: [&str; 6] = [
|
||||||
"/login",
|
"/login",
|
||||||
"/signup",
|
"/signup",
|
||||||
@@ -29,11 +23,7 @@ pub async fn require_auth_middleware(
|
|||||||
) -> Result<Response<Body>, (StatusCode, &'static str)> {
|
) -> Result<Response<Body>, (StatusCode, &'static str)> {
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
|
|
||||||
if ALLOWED_PATH_PREFIX
|
if ALLOWED_PATHS.contains(&path) {
|
||||||
.iter()
|
|
||||||
.any(|prefix| path.starts_with(prefix))
|
|
||||||
|| ALLOWED_PATHS.contains(&path)
|
|
||||||
{
|
|
||||||
let response = next.run(req).await;
|
let response = next.run(req).await;
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user