Skip to content

Commit

Permalink
webapp: Implement favicon endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Nov 22, 2024
1 parent 5b24ccd commit 8db0a10
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions repology-webapp/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub enum Endpoint {
// Misc
#[strum(props(path = "/log/:run_id"))]
Log,
#[strum(props(path = "/favicon.ico"))]
Favicon,

// API
#[strum(props(path = "/api/v1/projects/"))]
Expand Down Expand Up @@ -257,8 +259,6 @@ pub enum Endpoint {
// Misc
#[strum(props(path = "/link/*url"))]
Link,
#[strum(props(path = "/favicon.ico"))]
Favicon,

// API
#[strum(props(path = "/api/v1/repository/:repository_name/problems"))]
Expand Down
1 change: 1 addition & 0 deletions repology-webapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub async fn create_app(pool: PgPool) -> Result<Router, Error> {
.route(DocsBots.path(), get(views::docs_bots))
.route(DocsNotSupported.path(), get(views::docs_not_supported))
.route(DocsRequirements.path(), get(views::docs_requirements))
.route(Favicon.path(), get(views::favicon))
.route(GraphTotalPackages.path(), get(views::graph_total_packages))
.route(GraphTotalProjects.path(), get(views::graph_total_projects))
.route(GraphTotalMaintainers.path(), get(views::graph_total_maintainers))
Expand Down
17 changes: 13 additions & 4 deletions repology-webapp/src/views/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ impl HttpCacheMode {
}
}

#[cfg_attr(not(feature = "coverage"), tracing::instrument)]
pub async fn static_file(Path(file_name): Path<String>, headers: HeaderMap) -> EndpointResult {
let (file, cache_mode) = if let Some(file) = STATIC_FILES.by_hashed_name(&file_name) {
pub async fn static_file_generic(file_name: &str, headers: HeaderMap) -> EndpointResult {
let (file, cache_mode) = if let Some(file) = STATIC_FILES.by_hashed_name(file_name) {
(file, HttpCacheMode::Infinite)
} else if let Some(file) = STATIC_FILES.by_orig_name(&file_name) {
} else if let Some(file) = STATIC_FILES.by_orig_name(file_name) {
(file, HttpCacheMode::ShortLived)
} else {
return Ok((StatusCode::NOT_FOUND, "not found".to_owned()).into_response());
Expand Down Expand Up @@ -83,3 +82,13 @@ pub async fn static_file(Path(file_name): Path<String>, headers: HeaderMap) -> E
.into_response()
})
}

#[cfg_attr(not(feature = "coverage"), tracing::instrument)]
pub async fn static_file(Path(file_name): Path<String>, headers: HeaderMap) -> EndpointResult {
static_file_generic(&file_name, headers).await
}

#[cfg_attr(not(feature = "coverage"), tracing::instrument)]
pub async fn favicon(headers: HeaderMap) -> EndpointResult {
static_file_generic("repology.v1.ico", headers).await
}
2 changes: 2 additions & 0 deletions repology-webapp/tests/static_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ async fn test_mime_types(pool: PgPool) {
check_response!(pool, "/static/repology.v21.css", status OK, content_type "text/css");
check_response!(pool, "/static/repology40x40.v1.png", status OK, content_type "image/png");
check_response!(pool, "/static/vulnerable.v1.svg", status OK, content_type "image/svg+xml");

check_response!(pool, "/favicon.ico", status OK, content_type "image/x-icon");
}

0 comments on commit 8db0a10

Please sign in to comment.