// AUTO-GENERATED by puerto. Edit puerto.toml, then run `puerto generate bootstrap`.
{% if entities -%}
use std::sync::Arc;
{% for entity in entities -%}
{% for line in entity.uc_import_lines -%}
{{ line }}
{% endfor -%}
{% endfor %}
{% for entity in entities -%}
{{ entity.repo_trait_import }}
{{ entity.repo_impl_import }}
{% endfor %}
{% endif -%}
use poem::{EndpointExt, Route, get, middleware::{Cors, Tracing}};
use poem_openapi::OpenApiService;
{% for entity in entities -%}
{{ entity.api_import }}
{% endfor %}
pub async fn build_app() -> impl poem::Endpoint {
{%- if has_db %}
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool = infrastructure::db::create_postgres_pool(&database_url)
.await
.expect("Failed to connect to database");
infrastructure::db::run_migrations(&pool)
.await
.expect("Failed to run migrations");
{% endif %}
{%- for entity in entities %}
{%- if entity.needs_repo_var %}
{{ entity.repo_var_line }}
{%- endif %}
{%- for uc in entity.use_cases %}
let {{ uc.action }} = Arc::new({{ uc.uc_pascal }}UseCaseImpl { repository: {{ uc.repo_expr }} });
{%- endfor %}
let {{ entity.api_var }} = {{ entity.api_init }};
{% endfor %}
let port = std::env::var("SERVICE_PORT").unwrap_or_else(|_| "8080".to_string());
let api_service = OpenApiService::new(
{{ api_args }},
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
.server(format!("http://localhost:{port}/api"));
let ui = api_service.swagger_ui();
let cors_origins = std::env::var("CORS_ALLOWED_ORIGINS").unwrap_or_default();
let mut cors = Cors::new();
for origin in cors_origins.split(',').map(str::trim).filter(|s| !s.is_empty()) {
cors = cors.allow_origin(origin);
}
Route::new()
.at("/health", get(crate::health::liveness))
{%- if has_db %}
.at(
"/health/ready",
get(poem::endpoint::make(move |_| {
let pool = pool.clone();
async move { crate::health::readiness(infrastructure::db::ping(&pool).await) }
})),
)
{%- else %}
.at(
"/health/ready",
get(poem::endpoint::make_sync(|_| crate::health::readiness(true))),
)
{%- endif %}
.nest("/api", api_service)
.nest("/", ui)
.with(Tracing)
.with(cors)
}