refactor: remove metric collector; reorganize stuff; temporarily remove

api
main
Jef Roosens 2025-01-01 17:26:16 +01:00
parent 3b33cba0d4
commit 03bc88e7c3
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
3 changed files with 30 additions and 54 deletions

View File

@ -1,28 +1,25 @@
mod api;
mod error;
mod matrix;
mod metrics;
mod server;
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};
/// Name of the directory where static sites are stored inside the data directory
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]
async fn main() {
@ -43,16 +40,7 @@ async fn main() {
// Initialize metrics
let recorder_handle = metrics::setup_metrics_recorder();
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())));
let app = server::app(PathBuf::from(static_dir), &REDIRECTS);
// 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)
@ -62,36 +50,6 @@ async fn main() {
("/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));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)

18
src/server/mod.rs 100644
View File

@ -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())
}