parent
3b33cba0d4
commit
03bc88e7c3
66
src/main.rs
66
src/main.rs
|
@ -1,28 +1,25 @@
|
||||||
mod api;
|
mod api;
|
||||||
mod error;
|
mod error;
|
||||||
mod matrix;
|
|
||||||
mod metrics;
|
mod metrics;
|
||||||
|
mod server;
|
||||||
|
|
||||||
pub use error::Result;
|
pub use error::Result;
|
||||||
|
|
||||||
use std::{future::ready, net::SocketAddr};
|
use std::{net::SocketAddr, path::PathBuf};
|
||||||
|
|
||||||
use axum::{
|
|
||||||
extract::Extension,
|
|
||||||
middleware,
|
|
||||||
response::Redirect,
|
|
||||||
routing::{any, get},
|
|
||||||
Router,
|
|
||||||
};
|
|
||||||
use tower_http::{
|
|
||||||
services::ServeDir, trace::TraceLayer, validate_request::ValidateRequestHeaderLayer,
|
|
||||||
};
|
|
||||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
/// Name of the directory where static sites are stored inside the data directory
|
/// Name of the directory where static sites are stored inside the data directory
|
||||||
const STATIC_DIR_NAME: &str = "static";
|
const STATIC_DIR_NAME: &str = "static";
|
||||||
/// Name of the subdir of STATIC_DIR_NAME where the default (fallback) site is located
|
|
||||||
const DEFAULT_STATIC_SITE: &str = "default";
|
const REDIRECTS: [(&str, &str); 6] = [
|
||||||
|
("/github", "https://github.com/ChewingBever"),
|
||||||
|
("/gitea", "https://git.rustybever.be/Chewing_Bever"),
|
||||||
|
("/gitlab", "https://gitlab.com/Chewing_Bever"),
|
||||||
|
("/codeberg", "https://codeberg.org/Chewing_Bever"),
|
||||||
|
("/matrix", "https://matrix.to/#/@jef:rustybever.be"),
|
||||||
|
("/aur", "https://aur.archlinux.org/account/Chewing_Bever"),
|
||||||
|
];
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -43,16 +40,7 @@ async fn main() {
|
||||||
|
|
||||||
// Initialize metrics
|
// Initialize metrics
|
||||||
let recorder_handle = metrics::setup_metrics_recorder();
|
let recorder_handle = metrics::setup_metrics_recorder();
|
||||||
|
let app = server::app(PathBuf::from(static_dir), &REDIRECTS);
|
||||||
let mut app = Router::new()
|
|
||||||
// Handle Matrix .well-known files
|
|
||||||
.nest("/", matrix::router())
|
|
||||||
// Routes under /api path
|
|
||||||
.nest(
|
|
||||||
"/api",
|
|
||||||
api::router().layer(ValidateRequestHeaderLayer::bearer(&api_key)),
|
|
||||||
)
|
|
||||||
.route("/metrics", get(move || ready(recorder_handle.render())));
|
|
||||||
|
|
||||||
// Each static site gets mounted explicitely so that the default site can be used as fallback
|
// Each static site gets mounted explicitely so that the default site can be used as fallback
|
||||||
// Each entry is of the form (route, static dir name)
|
// Each entry is of the form (route, static dir name)
|
||||||
|
@ -62,36 +50,6 @@ async fn main() {
|
||||||
("/man/vieter", "man-vieter"),
|
("/man/vieter", "man-vieter"),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (path, dir) in sites {
|
|
||||||
let full_path = format!("{}/{}", static_dir, dir);
|
|
||||||
|
|
||||||
app = app.nest_service(path, ServeDir::new(full_path));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define some redirects
|
|
||||||
let redirects = [
|
|
||||||
("/github", "https://github.com/ChewingBever"),
|
|
||||||
("/gitea", "https://git.rustybever.be/Chewing_Bever"),
|
|
||||||
("/gitlab", "https://gitlab.com/Chewing_Bever"),
|
|
||||||
("/codeberg", "https://codeberg.org/Chewing_Bever"),
|
|
||||||
("/matrix", "https://matrix.to/#/@jef:rustybever.be"),
|
|
||||||
("/aur", "https://aur.archlinux.org/account/Chewing_Bever"),
|
|
||||||
];
|
|
||||||
|
|
||||||
for (path, url) in redirects {
|
|
||||||
app = app.route(path, any(|| async { Redirect::permanent(url) }))
|
|
||||||
}
|
|
||||||
|
|
||||||
app = app
|
|
||||||
// The fallback option is to serve the actual static files
|
|
||||||
.fallback_service(ServeDir::new(format!(
|
|
||||||
"{}/{}",
|
|
||||||
static_dir, DEFAULT_STATIC_SITE
|
|
||||||
)))
|
|
||||||
.layer(middleware::from_fn(metrics::track_metrics))
|
|
||||||
.layer(Extension(data_dir))
|
|
||||||
.layer(TraceLayer::new_for_http());
|
|
||||||
|
|
||||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||||
tracing::debug!("listening on {}", addr);
|
tracing::debug!("listening on {}", addr);
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
mod matrix;
|
||||||
|
|
||||||
|
use axum::{response::Redirect, routing::any, Extension, Router};
|
||||||
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn app(static_dir: PathBuf, redirects: &[(&'static str, &'static str)]) -> Router {
|
||||||
|
let mut app = Router::new().nest("/", matrix::router());
|
||||||
|
|
||||||
|
for (path, url) in redirects.iter() {
|
||||||
|
app = app.route(path, any(|| async { Redirect::permanent(url) }))
|
||||||
|
}
|
||||||
|
|
||||||
|
app.fallback_service(ServeDir::new(static_dir.clone()))
|
||||||
|
.layer(Extension(static_dir))
|
||||||
|
.layer(TraceLayer::new_for_http())
|
||||||
|
}
|
Loading…
Reference in New Issue