Skip to content

Commit

Permalink
move sqlite connection function to database.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmanczak committed Jul 18, 2024
1 parent f23a2d1 commit 2d4532a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
19 changes: 19 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod database;
mod routes;
mod setup;
use axum::Router;
Expand Down
8 changes: 4 additions & 4 deletions src/routes/splash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::setup;
use crate::database;
use axum::{
extract::{Path, Query},
http::StatusCode,
Expand Down Expand Up @@ -39,7 +39,7 @@ struct SplashGetParams {
}

async fn splash(Query(params): Query<SplashGetParams>) -> 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();

Expand Down Expand Up @@ -69,7 +69,7 @@ async fn splash(Query(params): Query<SplashGetParams>) -> Response {
}

async fn splash_by_id(Path(id): Path<String>) -> 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();
Expand All @@ -91,7 +91,7 @@ async fn splash_by_id(Path(id): Path<String>) -> 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();
Expand Down
21 changes: 3 additions & 18 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 2d4532a

Please sign in to comment.