Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JijoBose committed Nov 26, 2023
1 parent 18bba45 commit ac3465c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/actions/home.rs → src/app/actions/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use actix_web::web::Json;
use diesel::prelude::*;
use uuid::Uuid;

use crate::model::home;
use crate::app::models::home;

type DbError = Box<dyn std::error::Error + Send + Sync>;

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/api/home.rs → src/app/api/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use actix_web::{web, get, post, Result, Responder, error, HttpResponse};
use diesel::{SqliteConnection, r2d2};
use uuid::Uuid;

use crate::actions;
use crate::model;
use crate::app::actions;
use crate::app::models;

type DbPool = r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>;

Expand Down Expand Up @@ -38,7 +38,7 @@ async fn find_home(pool: web::Data<DbPool>, home_id: web::Path<Uuid>) -> Result<
#[post("/home")]
async fn add_home(
pool: web::Data<DbPool>,
form: web::Json<model::home::NewHome>,
form: web::Json<models::home::NewHome>,
) -> Result<impl Responder> {
let home = web::block(move || {
let mut conn = pool.get()?;
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions src/app/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::error::Error;

use diesel::{r2d2, SqliteConnection, sqlite::Sqlite, Connection};
use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness};


pub type DbPool = r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>;

pub fn initialize_db_pool() -> DbPool {
let conn_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL should be set");
let manager = r2d2::ConnectionManager::<SqliteConnection>::new(conn_spec);
r2d2::Pool::builder()
.build(manager)
.expect("database URL should be valid path to SQLite DB file")
}

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");

pub fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
connection.run_pending_migrations(MIGRATIONS)?;
Ok(())
}

pub fn initial_migration() {
let sqlite_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL should be set");
let mut connection = SqliteConnection::establish(&sqlite_spec).expect("Failed to establish connection");
let _ = run_migrations(&mut connection);
}
7 changes: 7 additions & 0 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod api;
pub mod actions;
pub mod db;

pub mod models {
pub mod home;
}
File renamed without changes.
41 changes: 7 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
use std::error::Error;

use actix_web::{middleware::Logger, web::{self, ServiceConfig}, App, HttpServer};
use diesel::{prelude::*, r2d2, sqlite::Sqlite};
use shuttle_actix_web::ShuttleActixWeb;
use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness};

mod api;
mod actions;
mod model;
mod schema;
pub mod app;
pub mod schema;

use api::home::{
use app::api::home::{
all_homes,
add_home,
find_home
};

type DbPool = r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>;

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");

fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
connection.run_pending_migrations(MIGRATIONS)?;
Ok(())
}

fn initial_migration() {
let sqlite_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL should be set");
let mut connection = SqliteConnection::establish(&sqlite_spec).expect("Failed to establish connection");
let _ = run_migrations(&mut connection);
}
use app::db::{
initialize_db_pool,
initial_migration
};

#[cfg(debug_assertions)]
#[actix_web::main]
Expand Down Expand Up @@ -74,14 +58,3 @@ async fn main() -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Send + Clon
};
Ok(config.into())
}

/// Initialize database connection pool based on `DATABASE_URL` environment variable.
///
/// See more: <https://docs.rs/diesel/latest/diesel/r2d2/index.html>.
fn initialize_db_pool() -> DbPool {
let conn_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL should be set");
let manager = r2d2::ConnectionManager::<SqliteConnection>::new(conn_spec);
r2d2::Pool::builder()
.build(manager)
.expect("database URL should be valid path to SQLite DB file")
}
1 change: 0 additions & 1 deletion src/model/mod.rs

This file was deleted.

0 comments on commit ac3465c

Please sign in to comment.