//! The front page, explore, owner pages and small endpoints. use super::{filters, Error, Page, Shell}; use crate::{db, Shared}; use askama::Template; use axum::extract::{Path, State}; use axum::http::header; use axum::response::{IntoResponse, Response}; use hub_api::Visibility; #[derive(Template)] #[template(path = "home.html")] pub struct HomePage { shell: Shell, description: String, repos: Vec, owner: Option, } pub async fn index(State(app): State) -> Result, Error> { let shell = Shell::new(&app, "/"); let repos = db::repos::list(&app.db).await.map_err(|e| Error::from_db(shell.clone(), e))?; let repos = repos.into_iter().filter(|r| r.visibility() == Visibility::Public).collect(); Ok(Page(HomePage { description: app.config.site.description.clone(), shell, repos, owner: None })) } pub async fn explore(State(app): State) -> Result, Error> { let mut page = index(State(app.clone())).await?; page.0.shell = Shell::new(&app, "/explore"); Ok(page) } pub async fn owner(State(app): State, Path(owner): Path) -> Result, Error> { let shell = Shell::new(&app, &format!("/{owner}")); if !crate::repos::names::valid_owner(&owner) { return Err(Error::not_found(shell)); } let repos = db::repos::list_by_owner(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; let repos: Vec<_> = repos.into_iter().filter(|r| r.visibility() == Visibility::Public).collect(); if repos.is_empty() { return Err(Error::not_found(shell)); } Ok(Page(HomePage { description: String::new(), shell, repos, owner: Some(owner) })) } pub async fn health(State(app): State) -> Response { let body = serde_json::json!({ "ok": true, "version": env!("CARGO_PKG_VERSION"), "uptime_s": app.started.elapsed().as_secs(), }); axum::Json(body).into_response() } /// The hub delegate identity object (hub spec ยง15.5), as `oxy identity /// export` writes one, so policy holders can admit it. pub async fn hub_key(State(app): State) -> Response { ( [(header::CONTENT_TYPE, "application/oxy-identity"), (header::CONTENT_DISPOSITION, "attachment; filename=\"hub.identity\"")], app.hub_key.bytes.clone(), ) .into_response() }