-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows to reuse app in tests
- Loading branch information
Showing
2 changed files
with
52 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#![feature(iterator_try_collect)] | ||
|
||
mod badges; | ||
mod font; | ||
mod package; | ||
mod query; | ||
mod repository_data; | ||
mod result; | ||
mod state; | ||
mod views; | ||
mod xmlwriter; | ||
|
||
use anyhow::{Context, Error}; | ||
use axum::{routing::get, Router}; | ||
use sqlx::PgPool; | ||
|
||
use crate::font::FontMeasurer; | ||
use crate::repository_data::RepositoryDataCache; | ||
use crate::state::AppState; | ||
|
||
pub async fn create_app(pool: PgPool) -> Result<Router, Error> { | ||
let font_measurer = FontMeasurer::new(); | ||
|
||
let repository_data_cache = RepositoryDataCache::new(pool.clone()); | ||
repository_data_cache | ||
.update() | ||
.await | ||
.context("error getting repository metadata")?; | ||
|
||
let state = AppState::new(pool, font_measurer, repository_data_cache); | ||
|
||
Ok(Router::new() | ||
.route("/api/v1/project/:project_name", get(views::api_v1_project)) | ||
.route( | ||
"/badge/tiny-repos/:project_name.svg", | ||
get(views::badge_tiny_repos), | ||
) | ||
.route( | ||
"/badge/version-for-repo/:repository_name/:project_name.svg", | ||
get(views::badge_version_for_repo), | ||
) | ||
.route( | ||
"/badge/vertical-allrepos/:project_name.svg", | ||
get(views::badge_vertical_allrepos), | ||
) | ||
.with_state(state)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,14 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#![feature(iterator_try_collect)] | ||
|
||
mod badges; | ||
mod config; | ||
mod font; | ||
mod package; | ||
mod query; | ||
mod repository_data; | ||
mod result; | ||
mod state; | ||
mod views; | ||
mod xmlwriter; | ||
|
||
use anyhow::{Context, Error}; | ||
use axum::{routing::get, Router}; | ||
use clap::Parser; | ||
use sqlx::PgPool; | ||
|
||
use crate::config::Config; | ||
use crate::font::FontMeasurer; | ||
use crate::repository_data::RepositoryDataCache; | ||
use crate::state::AppState; | ||
use repology_webapp::create_app; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
|
@@ -32,31 +18,7 @@ async fn main() -> Result<(), Error> { | |
.await | ||
.context("error creating PostgreSQL connection pool")?; | ||
|
||
let font_measurer = FontMeasurer::new(); | ||
|
||
let repository_data_cache = RepositoryDataCache::new(pool.clone()); | ||
repository_data_cache | ||
.update() | ||
.await | ||
.context("error getting repository metadata")?; | ||
|
||
let state = AppState::new(pool, font_measurer, repository_data_cache); | ||
|
||
let app = Router::new() | ||
.route("/api/v1/project/:project_name", get(views::api_v1_project)) | ||
.route( | ||
"/badge/tiny-repos/:project_name.svg", | ||
get(views::badge_tiny_repos), | ||
) | ||
.route( | ||
"/badge/version-for-repo/:repository_name/:project_name.svg", | ||
get(views::badge_version_for_repo), | ||
) | ||
.route( | ||
"/badge/vertical-allrepos/:project_name.svg", | ||
get(views::badge_vertical_allrepos), | ||
) | ||
.with_state(state); | ||
let app = create_app(pool).await?; | ||
|
||
let listener = tokio::net::TcpListener::bind(&config.listen).await.unwrap(); | ||
axum::serve(listener, app) | ||
|