Skip to content

Commit

Permalink
Implement new database service and use it.
Browse files Browse the repository at this point in the history
This clears the way for some cleanups.

Signed-off-by: Elizabeth Myers <[email protected]>
  • Loading branch information
Elizafox committed Mar 15, 2024
1 parent 542958b commit 4658f55
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ sqlite = ["sea-orm/sqlx-sqlite"]

[dependencies]
entity = { path = "../entity" }
migration = {path = "../migration" }
ipnetwork = "0.20.0"
sea-orm = { version = "1.0.0-rc.1", features = ["debug-print", "runtime-tokio-native-tls"] }
tracing = { version = "0.1.40", features = ["async-await", "log"] }
39 changes: 39 additions & 0 deletions service/src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* SPDX-License-Identifier: CC0-1.0
*
* service/src/database.rs
*
* This file is a component of ShadyURL by Elizabeth Myers.
*
* To the extent possible under law, the person who associated CC0 with
* ShadyURL has waived all copyright and related or neighboring rights
* to ShadyURL.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

use sea_orm::{ConnectOptions, DbConn, DbErr};
use tracing::log::LevelFilter;

use migration::{Migrator, MigratorTrait};

pub struct Database;

impl Database {
pub async fn get_with_connect_options(opt: ConnectOptions) -> Result<DbConn, DbErr> {
let db = sea_orm::Database::connect(opt).await?;

Migrator::up(&db, None).await?;

Ok(db)
}

pub async fn get(url: &str) -> Result<DbConn, DbErr> {
let mut opt = ConnectOptions::new(url);
opt.sqlx_logging(false)
.sqlx_logging_level(LevelFilter::Warn);

let db = Self::get_with_connect_options(opt).await?;
Ok(db)
}
}
2 changes: 2 additions & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

mod database;
mod mutation;
mod query;

pub use database::*;
pub use mutation::*;
pub use query::*;

Expand Down
26 changes: 4 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ use dotenvy::dotenv;
use password_auth::generate_hash;
use proctitle::set_title;
use rpassword::prompt_password;
use sea_orm::{ConnectOptions, Database};
use tracing::log::LevelFilter;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

use crate::{env::Vars, web::App};

use migration::{Migrator, MigratorTrait};
use service::{Mutation, Query};
use service::{Database, Mutation, Query};

#[derive(Parser)]
struct Cli {
Expand Down Expand Up @@ -72,13 +70,7 @@ enum UserError {

async fn add_user_cli(username: &str) -> Result<(), Box<dyn std::error::Error>> {
let env = Vars::load_env()?;
let mut opt = ConnectOptions::new(&env.database_url);
opt.sqlx_logging(false)
.sqlx_logging_level(LevelFilter::Warn);

let db = Database::connect(opt).await?;

Migrator::up(&db, None).await?;
let db = Database::get(&env.database_url).await?;

let mut password = prompt_password("Password:")?;
if password != prompt_password("Repeat password:")? {
Expand All @@ -98,13 +90,7 @@ async fn add_user_cli(username: &str) -> Result<(), Box<dyn std::error::Error>>

async fn delete_user_cli(username: &str) -> Result<(), Box<dyn std::error::Error>> {
let env = Vars::load_env()?;
let mut opt = ConnectOptions::new(&env.database_url);
opt.sqlx_logging(false)
.sqlx_logging_level(LevelFilter::Warn);

let db = Database::connect(opt).await?;

Migrator::up(&db, None).await?;
let db = Database::get(&env.database_url).await?;

let mut response = String::new();
loop {
Expand Down Expand Up @@ -138,11 +124,7 @@ async fn delete_user_cli(username: &str) -> Result<(), Box<dyn std::error::Error

async fn change_password_cli(username: &str) -> Result<(), Box<dyn std::error::Error>> {
let env = Vars::load_env()?;
let mut opt = ConnectOptions::new(&env.database_url);
opt.sqlx_logging(false)
.sqlx_logging_level(LevelFilter::Warn);

let db = Database::connect(opt).await?;
let db = Database::get(&env.database_url).await?;

Migrator::up(&db, None).await?;

Expand Down
6 changes: 3 additions & 3 deletions src/web/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use axum::Router;
use axum_login::AuthManagerLayerBuilder;
use axum_messages::MessagesManagerLayer;
use csrf::ChaCha20Poly1305CsrfProtection;
use sea_orm::{ConnectOptions, Database};
use sea_orm::ConnectOptions;
use time::Duration;
use tokio::task::JoinHandle;
use tower::ServiceBuilder;
Expand All @@ -28,6 +28,7 @@ use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
use tracing::info;

use migration::{Migrator, MigratorTrait};
use service::Database;

use crate::{
auth::Backend,
Expand Down Expand Up @@ -59,8 +60,7 @@ impl App {
opt.max_connections(100)
.min_connections(5)
.sqlx_logging(false);

let db = Arc::new(Database::connect(opt).await?);
let db = Arc::new(Database::get_with_connect_options(opt).await?);

Migrator::up(db.as_ref(), None).await?;

Expand Down

0 comments on commit 4658f55

Please sign in to comment.