use std::any::Any;
use std::future::Future;
use anyhow::Context as _;
use axum::Router;
use axum::body::Body;
use axum::extract::FromRequest;
use axum::extract::FromRequestParts;
use axum::extract::rejection::JsonRejection;
use axum::extract::rejection::PathRejection;
use axum::http;
use axum::http::HeaderName;
use axum::http::StatusCode;
use axum::http::header;
use axum::response::IntoResponse;
use axum::response::Response;
use axum::routing::get;
use bon::Builder;
use serde::Serialize;
use serde::Serializer;
use tokio::net::TcpListener;
use tower::ServiceBuilder;
use tower_http::LatencyUnit;
use tower_http::compression::CompressionLayer;
use tower_http::sensitive_headers::SetSensitiveRequestHeadersLayer;
use tower_http::sensitive_headers::SetSensitiveResponseHeadersLayer;
use tower_http::trace::DefaultMakeSpan;
use tower_http::trace::DefaultOnResponse;
use tower_http::trace::TraceLayer;
use tracing::Span;
use tracing::debug;
use tracing::error;
use tracing::info;
#[cfg(feature = "templating")]
pub mod templating;
pub const DEFAULT_ADDRESS: &str = "0.0.0.0";
pub const DEFAULT_PORT: u16 = 8080;
const SENSITIVE_HEADERS: [HeaderName; 2] = [header::AUTHORIZATION, header::COOKIE];
fn handle_panic(err: Box<dyn Any + Send + 'static>) -> Response {
if let Some(s) = err.downcast_ref::<String>() {
error!("server panicked: {s}");
} else if let Some(s) = err.downcast_ref::<&str>() {
error!("server panicked: {s}");
} else {
error!("server panicked: unknown panic message");
};
Error::internal().into_response()
}
#[derive(FromRequest)]
#[from_request(via(axum::Json), rejection(Error))]
pub struct Json<T>(pub T);
impl<T> IntoResponse for Json<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
axum::Json(self.0).into_response()
}
}
fn serialize_status<S>(status: &StatusCode, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u16(status.as_u16())
}
#[derive(Serialize, Debug)]
pub struct Error {
#[serde(serialize_with = "serialize_status")]
pub status: StatusCode,
pub message: String,
}
impl Error {
pub fn not_found() -> Self {
Self {
status: StatusCode::NOT_FOUND,
message: "the requested resource was not found".to_string(),
}
}
pub fn bad_request(message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
message: message.into(),
}
}
pub fn forbidden() -> Self {
Self {
status: StatusCode::FORBIDDEN,
message: StatusCode::FORBIDDEN.to_string(),
}
}
pub fn internal() -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: StatusCode::INTERNAL_SERVER_ERROR.to_string(),
}
}
pub fn not_cancelable() -> Self {
Self {
status: StatusCode::BAD_REQUEST,
message: "task is not in a cancelable state".into(),
}
}
}
impl From<anyhow::Error> for Error {
fn from(e: anyhow::Error) -> Self {
tracing::error!("{e:#}");
Self::internal()
}
}
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
tracing::error!("{e:#}");
Self::internal()
}
}
#[cfg(feature = "postgres")]
impl From<planetary_db::postgres::Error> for Error {
fn from(e: planetary_db::postgres::Error) -> Self {
use planetary_db::postgres::Error::*;
let (status, message) = match &e {
TaskNotFound(_) => (StatusCode::NOT_FOUND, e.to_string()),
Pool(e) => {
tracing::error!("database connection error: {e:#}");
return Self::internal();
}
Diesel(e) => {
tracing::error!("database error: {e:#}");
return Self::internal();
}
};
Self { status, message }
}
}
impl From<planetary_db::Error> for Error {
fn from(e: planetary_db::Error) -> Self {
let (status, message) = match e {
planetary_db::Error::InvalidPageToken(_) => (StatusCode::BAD_REQUEST, e.to_string()),
#[cfg(feature = "postgres")]
planetary_db::Error::Postgres(e) => return e.into(),
planetary_db::Error::Other(e) => return e.into(),
};
Self { status, message }
}
}
impl From<JsonRejection> for Error {
fn from(rejection: JsonRejection) -> Self {
Self {
status: rejection.status(),
message: rejection.body_text(),
}
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
(self.status, axum::Json(self)).into_response()
}
}
pub type ServerResponse<T> = Result<T, Error>;
#[derive(FromRequestParts)]
#[from_request(via(axum::extract::Path), rejection(Error))]
pub struct Path<T>(pub T);
impl From<PathRejection> for Error {
fn from(rejection: PathRejection) -> Self {
Self {
status: rejection.status(),
message: rejection.body_text(),
}
}
}
#[derive(FromRequestParts)]
#[from_request(via(axum_extra::extract::Query), rejection(Error))]
pub struct Query<T>(pub T);
impl From<axum_extra::extract::QueryRejection> for Error {
fn from(rejection: axum_extra::extract::QueryRejection) -> Self {
Self {
status: rejection.status(),
message: rejection.body_text(),
}
}
}
#[derive(Clone, Builder)]
pub struct Server<S> {
#[builder(into, default = DEFAULT_ADDRESS)]
address: String,
#[builder(into, default = DEFAULT_PORT)]
port: u16,
#[builder(into, default)]
routers: Vec<Router<S>>,
}
impl<S> Server<S>
where
S: Clone + Send + Sync + 'static,
{
pub async fn run<F>(self, state: S, shutdown: F) -> anyhow::Result<()>
where
F: Future<Output = ()> + Send + 'static,
{
let middleware = ServiceBuilder::new()
.layer(SetSensitiveRequestHeadersLayer::new(SENSITIVE_HEADERS))
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().include_headers(true))
.on_request(|request: &http::Request<Body>, _span: &Span| {
debug!(
"{method} {path}",
method = request.method(),
path = request.uri().path()
)
})
.on_response(
DefaultOnResponse::new()
.level(tracing::Level::DEBUG)
.latency_unit(LatencyUnit::Micros),
),
)
.layer(CompressionLayer::new())
.layer(SetSensitiveResponseHeadersLayer::new(SENSITIVE_HEADERS));
let mut router = Router::new().route("/ping", get(|| async {}));
for merge in self.routers {
router = router.merge(merge);
}
let addr = format!("{address}:{port}", address = self.address, port = self.port);
let listener = TcpListener::bind(&addr)
.await
.context("binding to the provided address")?;
info!("listening at {addr}");
axum::serve(
listener,
router
.fallback(async || Error::not_found())
.layer(middleware)
.layer(tower_http::catch_panic::CatchPanicLayer::custom(
handle_panic,
))
.with_state(state),
)
.with_graceful_shutdown(shutdown)
.await
.context("failed to run server")?;
Ok(())
}
}