From 461ccaa4b9dcd33fe7478efb6aee5217d8ab1340 Mon Sep 17 00:00:00 2001 From: roaming97 Date: Sun, 13 Apr 2025 13:21:07 -0600 Subject: [PATCH] Better video ID handling + Instance::new improvements --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/instance.rs | 18 +++++------------- src/main.rs | 4 ++-- src/routes.rs | 34 +++++++++++++++++++++------------- src/video.rs | 6 ++---- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae69860..0940a72 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,7 +33,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" [[package]] -name = "almond" +name = "almond-api" version = "0.2.0" dependencies = [ "axum", diff --git a/Cargo.toml b/Cargo.toml index dc9e46a..6cd7df5 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "almond" +name = "almond-api" version = "0.2.0" edition = "2024" diff --git a/src/instance.rs b/src/instance.rs index c1306ba..3b42e44 100755 --- a/src/instance.rs +++ b/src/instance.rs @@ -4,13 +4,11 @@ use serde::{Deserialize, Serialize}; use sqlx::SqlitePool; use thiserror::Error; -use crate::video::Video; - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub host: String, pub port: u16, - pub password: Option, + pub password: String, pub videos_per_page: usize, } @@ -19,7 +17,7 @@ impl Default for Config { Self { host: "0.0.0.0".into(), port: 3000, - password: None, + password: "123456".into(), videos_per_page: 10, } } @@ -36,10 +34,10 @@ pub struct Instance { pub enum NewInstanceError { #[error("Failed to open TOML configuration file")] ConfigLoad(#[from] io::Error), - #[error("Could not parse TOML configuration: {0}")] + #[error("Could not parse TOML configuration")] ConfigParse(#[from] toml::de::Error), - #[error("Failed to fetch entries from database: {0}")] - Populate(#[from] sqlx::Error), + #[error("Failed to create connection pool")] + PoolConnect(#[from] sqlx::Error), } impl Instance { @@ -52,10 +50,4 @@ impl Instance { Ok(Self { config, pool }) } - - pub async fn fetch_videos(&self) -> Result, sqlx::Error> { - sqlx::query_as!(Video, "SELECT * FROM video") - .fetch_all(&self.pool) - .await - } } diff --git a/src/main.rs b/src/main.rs index b418b52..d5d559e 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use axum::{ routing::{get, post}, }; use instance::Instance; -use routes::{list_entries, upload_video}; +use routes::{list_videos, upload_video}; use tokio::signal; use tracing::info; @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box> { let address = format!("{}:{}", instance.config.host, instance.config.port); let almond = Router::new() - .route("/", get(list_entries)) + .route("/", get(list_videos)) .route("/upload", post(upload_video)) .with_state(instance); diff --git a/src/routes.rs b/src/routes.rs index 5e1edc2..1c54683 100755 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,5 +1,5 @@ use axum::{ - Json, debug_handler, + Json, extract::{Query, State}, http::StatusCode, }; @@ -12,12 +12,12 @@ use crate::{ }; #[derive(Debug, Deserialize)] -pub struct ListEntriesQuery { +pub struct ListVideosQuery { page: Option, } #[derive(Debug, Serialize)] -pub struct ListEntriesResponse { +pub struct ListVideosResponse { videos: Vec