site-backend/src/main.rs

100 lines
3.2 KiB
Rust
Raw Normal View History

2022-04-04 16:58:34 +02:00
use std::{future::ready, net::SocketAddr};
2022-04-02 21:00:38 +02:00
2022-04-04 16:58:34 +02:00
use axum::{
extract::Extension,
http::StatusCode,
middleware,
routing::{get, get_service},
Router,
};
2022-04-01 13:29:34 +02:00
use tower_http::{auth::RequireAuthorizationLayer, services::ServeDir, trace::TraceLayer};
2022-04-01 11:31:32 +02:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2022-04-02 20:56:51 +02:00
mod api;
mod matrix;
2022-04-04 16:58:34 +02:00
mod metrics;
2022-04-02 20:56:51 +02:00
/// 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";
2022-04-02 13:01:49 +02:00
2022-04-01 11:31:32 +02:00
#[tokio::main]
async fn main() {
2022-04-01 11:31:32 +02:00
// Enable tracing
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
2022-04-02 20:56:51 +02:00
std::env::var("RUST_LOG").unwrap_or_else(|_| "site=debug,tower_http=debug".into()),
2022-04-01 11:31:32 +02:00
))
.with(tracing_subscriber::fmt::layer())
.init();
2022-04-01 13:29:34 +02:00
// Get required variables from env vars
let api_key = std::env::var("API_KEY").expect("No API_KEY was provided.");
let data_dir = std::env::var("DATA_DIR").expect("No DATA_DIR was provided.");
let static_dir = format!("{}/{}", data_dir, STATIC_DIR_NAME);
2022-04-01 13:29:34 +02:00
2022-04-02 13:01:49 +02:00
std::fs::create_dir_all(&static_dir);
2022-04-01 13:29:34 +02:00
// Initialize metrics
2022-04-04 16:58:34 +02:00
let recorder_handle = metrics::setup_metrics_recorder();
let mut app = Router::new()
2022-04-02 20:56:51 +02:00
// Handle Matrix .well-known files
.nest("/", matrix::router())
// Routes under /api path
.nest(
"/api",
api::router().layer(RequireAuthorizationLayer::bearer(&api_key)),
2022-04-01 13:29:34 +02:00
)
.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 entry is of the form (route, static dir name)
2022-05-01 12:00:48 +02:00
let sites = [
("/docs/vieter", "docs-vieter"),
("/api-docs/vieter", "api-docs-vieter"),
];
for (path, dir) in sites {
let full_path = format!("{}/{}", static_dir, dir);
app = app.nest(
path,
get_service(ServeDir::new(full_path)).handle_error(
|error: std::io::Error| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
},
),
);
}
app = app
2022-04-01 13:29:34 +02:00
// The fallback option is to serve the actual static files
.fallback(
get_service(ServeDir::new(format!(
"{}/{}",
static_dir, DEFAULT_STATIC_SITE
)))
.handle_error(|error: std::io::Error| async move {
2022-04-02 13:01:49 +02:00
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
}),
)
2022-04-04 16:58:34 +02:00
.layer(middleware::from_fn(metrics::track_metrics))
.layer(Extension(data_dir))
.layer(TraceLayer::new_for_http());
2022-04-01 11:31:32 +02:00
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}