From 2d4532a35a8f1a97c987ccef1e9d9a5865f56edc Mon Sep 17 00:00:00 2001 From: jakubmanczak Date: Thu, 18 Jul 2024 17:00:05 +0200 Subject: [PATCH] move sqlite connection function to database.rs --- src/database.rs | 19 +++++++++++++++++++ src/main.rs | 1 + src/routes/splash.rs | 8 ++++---- src/setup.rs | 21 +++------------------ 4 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 src/database.rs diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..09f12a7 --- /dev/null +++ b/src/database.rs @@ -0,0 +1,19 @@ +use std::env; + +use sqlite::Connection; +use tracing::error; + +pub fn initialise_sqlite_connection() -> Connection { + let path = match env::var("DBPATH") { + Ok(env) => env, + Err(_) => "sqlite.db".to_owned(), + }; + let conn = match sqlite::open(path) { + Ok(conn) => conn, + Err(e) => { + error!("error establishing sqlite db connection: {e}"); + panic!(); + } + }; + return conn; +} diff --git a/src/main.rs b/src/main.rs index 9454082..afaabed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod database; mod routes; mod setup; use axum::Router; diff --git a/src/routes/splash.rs b/src/routes/splash.rs index 4e9a854..7e6871c 100644 --- a/src/routes/splash.rs +++ b/src/routes/splash.rs @@ -1,4 +1,4 @@ -use crate::setup; +use crate::database; use axum::{ extract::{Path, Query}, http::StatusCode, @@ -39,7 +39,7 @@ struct SplashGetParams { } async fn splash(Query(params): Query) -> Response { - let conn = setup::initialise_sqlite_connection(); + let conn = database::initialise_sqlite_connection(); let query = "SELECT * FROM splashes ORDER BY RANDOM() LIMIT 1"; let mut statement = conn.prepare(query).unwrap(); @@ -69,7 +69,7 @@ async fn splash(Query(params): Query) -> Response { } async fn splash_by_id(Path(id): Path) -> Response { - let conn = setup::initialise_sqlite_connection(); + let conn = database::initialise_sqlite_connection(); let query = "SELECT * FROM splashes WHERE id = :id"; let mut statement = conn.prepare(query).unwrap(); statement.bind((":id", id.as_str())).unwrap(); @@ -91,7 +91,7 @@ async fn splash_by_id(Path(id): Path) -> Response { } async fn splashes() -> Response { - let conn = setup::initialise_sqlite_connection(); + let conn = database::initialise_sqlite_connection(); let query = "SELECT * FROM splashes LIMIT :limit"; let mut statement = conn.prepare(query).unwrap(); statement.bind((":limit", 200)).unwrap(); diff --git a/src/setup.rs b/src/setup.rs index e7aca94..db7788f 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,11 +1,11 @@ -use sqlite::Connection; -use std::env; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use tokio::net::TcpListener; use tracing::Level; use tracing::{error, info, trace}; use tracing_subscriber::FmtSubscriber; +use crate::database; + pub fn initialise_logging() { let subscriber = FmtSubscriber::builder() .with_max_level(Level::TRACE) @@ -27,23 +27,8 @@ pub fn initialise_dotenv() { }; } -pub fn initialise_sqlite_connection() -> Connection { - let path = match env::var("DBPATH") { - Ok(env) => env, - Err(_) => "sqlite.db".to_owned(), - }; - let conn = match sqlite::open(path) { - Ok(conn) => conn, - Err(e) => { - error!("error establishing sqlite db connection: {e}"); - panic!(); - } - }; - return conn; -} - pub fn initialise_sqlite_db_tables() { - let conn = initialise_sqlite_connection(); + let conn = database::initialise_sqlite_connection(); conn.execute( "CREATE TABLE IF NOT EXISTS splashes ( id TEXT NOT NULL UNIQUE PRIMARY KEY,