use std::time::Duration;
use poem::{
Endpoint, EndpointExt, IntoEndpoint, Route, Server, listener::TcpListener, middleware::Tracing,
};
use poem_openapi::{OpenApi, OpenApiService};
use crate::{Config, Error, health, health::ReadinessProbe};
pub const SHUTDOWN_DEADLINE: Duration = Duration::from_secs(8);
const BANNER: &str = r#"
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
*********** ++++++++++++ =============
%#*******##%%%#**********#%%%%++++++++++*%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
****###########%%%%%%%%%%%%%%%%%%%%%%%
***************************** ****
*********** ***********************
* ***************
*****
P U E R T O"#;
pub struct App {
config: Config,
}
impl App {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn serve<F, Fut, E>(self, build: F) -> Result<(), Error>
where
F: FnOnce(Config) -> Fut,
Fut: Future<Output = E>,
E: IntoEndpoint + 'static,
E::Endpoint: 'static,
{
let _ = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.try_init();
if self.config.banner() {
println!("{BANNER}");
println!(" puerto v{}\n", env!("CARGO_PKG_VERSION"));
}
let port = self.config.port();
let endpoint = wrap(&self.config, build(self.config.clone()).await);
Server::new(TcpListener::bind(format!("0.0.0.0:{port}")))
.run_with_graceful_shutdown(endpoint, shutdown_signal(), Some(SHUTDOWN_DEADLINE))
.await?;
Ok(())
}
}
fn wrap<E: IntoEndpoint>(config: &Config, endpoint: E) -> impl Endpoint + use<E>
where
E::Endpoint: 'static,
{
endpoint.into_endpoint().with(Tracing).with(config.cors())
}
pub fn routes<T: OpenApi + 'static>(
api: OpenApiService<T, ()>,
config: &Config,
ready: ReadinessProbe,
) -> Route {
let api = api.server(format!("http://localhost:{}/api", config.port()));
let ui = api.swagger_ui();
health::mount(Route::new(), ready)
.nest("/api", api)
.nest("/", ui)
}
async fn shutdown_signal() {
let ctrl_c = async {
let _ = tokio::signal::ctrl_c().await;
};
#[cfg(unix)]
let terminate = async {
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
Ok(mut signal) => {
signal.recv().await;
}
Err(_) => std::future::pending::<()>().await,
}
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
tracing::info!("shutdown signal received, draining in-flight requests");
}
#[cfg(test)]
mod tests {
use poem::{handler, http::StatusCode, test::TestClient};
use super::*;
use crate::health::{always_ready, probe};
#[handler]
fn hello() -> &'static str {
"hello"
}
fn allowing(origin: &str) -> Config {
Config::default().with_cors_allowed_origins(vec![origin.to_string()])
}
#[tokio::test]
async fn should_allow_a_preflight_when_the_origin_is_configured() {
let client = TestClient::new(wrap(
&allowing("http://localhost:5173"),
Route::new().at("/api/x", poem::get(hello)),
));
let response = client
.options("/api/x")
.header("Origin", "http://localhost:5173")
.header("Access-Control-Request-Method", "GET")
.send()
.await;
response.assert_status_is_ok();
response.assert_header("access-control-allow-origin", "http://localhost:5173");
}
#[tokio::test]
async fn should_refuse_a_preflight_when_the_origin_is_not_configured() {
let client = TestClient::new(wrap(
&allowing("http://localhost:5173"),
Route::new().at("/api/x", poem::get(hello)),
));
let response = client
.options("/api/x")
.header("Origin", "https://evil.example.com")
.header("Access-Control-Request-Method", "GET")
.send()
.await;
response.assert_status(StatusCode::FORBIDDEN);
response.assert_header_is_not_exist("access-control-allow-origin");
}
#[tokio::test]
async fn should_serve_the_health_probes_next_to_the_api() {
let routes = routes(
OpenApiService::new((), "app", "1.0"),
&Config::default(),
probe(|| async { false }),
);
let client = TestClient::new(routes);
let live = client.get("/health").send().await;
let ready = client.get("/health/ready").send().await;
live.assert_status_is_ok();
ready.assert_status(StatusCode::SERVICE_UNAVAILABLE);
}
#[tokio::test]
async fn should_serve_the_swagger_ui_at_the_root() {
let client = TestClient::new(routes(
OpenApiService::new((), "app", "1.0"),
&Config::default(),
always_ready(),
));
let response = client.get("/").send().await;
response.assert_status_is_ok();
response.assert_content_type("text/html; charset=utf-8");
}
}