[refactor] removed unused api.rs module to streamline code structure

This commit is contained in:
2025-08-20 08:28:22 +02:00
parent af304266a4
commit 7c6724800f
13 changed files with 823 additions and 64 deletions

7
backend-rust/crates/api/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "api"
version = "0.1.0"

View File

@@ -0,0 +1,28 @@
[package]
name = "owly-news-api"
version.workspace = true
edition.workspace = true
[lib]
path = "src/lib.rs"
[dependencies]
anyhow = { workspace = true }
tokio = { workspace = true, features = ["full"] }
axum = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sqlx = { workspace = true, features = ["runtime-tokio", "tls-native-tls", "sqlite", "macros", "migrate", "chrono", "json"] }
dotenv = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
toml = { workspace = true }
unicode-segmentation = { workspace = true }
sha2 = { workspace = true }
hex = { workspace = true }
readability = { workspace = true }
scraper = { workspace = true }
[dev-dependencies]
tokio-test = { workspace = true }
axum-test = { workspace = true }

View File

@@ -0,0 +1,3 @@
pub mod handlers;
pub mod middleware;
pub mod routes;

View File

@@ -0,0 +1,41 @@
use axum::Json;
use axum::extract::State;
use serde_json::Value;
use sqlx::SqlitePool;
pub async fn get_articles(State(pool): State<SqlitePool>) -> Result<Json<Value>, AppError> {
// TODO: Article logic
Ok(Json(serde_json::json!({"articles": []})))
}
pub async fn get_summaries(State(pool): State<SqlitePool>) -> Result<Json<Value>, AppError> {
// TODO: Summaries logic
Ok(Json(serde_json::json!({"summaries": []})))
}
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
pub struct AppError(anyhow::Error);
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, message) = match self.0.downcast_ref::<sqlx::Error>() {
Some(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Database error occurred"),
None => (StatusCode::INTERNAL_SERVER_ERROR, "An error occurred"),
};
tracing::error!("API Error: {:?}", self.0);
(status, message).into_response()
}
}
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>, {
fn from(err: E) -> Self {
Self(err.into())
}
}

View File

@@ -0,0 +1,11 @@
use axum::Router;
use axum::routing::get;
use sqlx::SqlitePool;
use crate::api::handlers;
pub fn routes() -> Router<SqlitePool> {
Router::new()
.route("/articles", get(handlers::get_articles))
.route("/summaries", get(handlers::get_summaries))
// Add more routes as needed
}

View File

@@ -0,0 +1,2 @@
pub mod api;
pub use api::*;