Refactor project into workspaces

This commit is contained in:
Tobias Reisinger 2023-11-22 20:06:20 +01:00
parent 131bdeec78
commit bacea1e3e9
31 changed files with 99 additions and 88 deletions

2
.env
View file

@ -1,2 +0,0 @@
# This is usually used in development by sqlx. It is not used in production
DATABASE_URL=sqlite://emgauwa-core.sqlite

5
.gitignore vendored
View file

@ -4,9 +4,8 @@
/tests/testing_bak/ /tests/testing_bak/
/tests/testing_latest/ /tests/testing_latest/
emgauwa-core.conf.d emgauwa-*.sqlite
emgauwa-core.sqlite emgauwa-*.sqlite-*
emgauwa-core.sqlite-*
# Added by cargo # Added by cargo

BIN
Cargo.lock generated

Binary file not shown.

View file

@ -1,30 +1,6 @@
[package] [workspace]
name = "emgauwa-core" resolver = "2"
version = "0.1.0" members = [
edition = "2018" "emgauwa-core",
authors = ["Tobias Reisinger <tobias@msrg.cc>"] "emgauwa-lib",
]
[dependencies]
actix = "0.13"
actix-web = "4.4"
actix-web-actors = "4.2"
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
dotenv = "0.15"
config = "0.13"
lazy_static = { version = "1.4.0", features = [] }
simple_logger = "4.2"
log = "0.4"
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "1.5", features = ["serde", "v4"] }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
libsqlite3-sys = { version = "*", features = ["bundled"] }
futures = "0.3.29"

View file

@ -1,3 +0,0 @@
fn main() {
println!("cargo:rerun-if-changed=migrations");
}

26
emgauwa-core/Cargo.toml Normal file
View file

@ -0,0 +1,26 @@
[package]
name = "emgauwa-core"
version = "0.1.0"
edition = "2021"
authors = ["Tobias Reisinger <tobias@msrg.cc>"]
[dependencies]
emgauwa-lib = { path = "../emgauwa-lib" }
actix = "0.13"
actix-web = "4.4"
actix-web-actors = "4.2"
config = "0.13"
simple_logger = "4.2"
log = "0.4"
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "1.5", features = ["serde", "v4"] }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
futures = "0.3.29"

3
emgauwa-core/build.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-core.sqlite")
}

View file

@ -2,20 +2,15 @@ use std::str::FromStr;
use actix_web::middleware::TrailingSlash; use actix_web::middleware::TrailingSlash;
use actix_web::{middleware, web, App, HttpServer}; use actix_web::{middleware, web, App, HttpServer};
use emgauwa_lib::handlers;
use log::{trace, LevelFilter}; use log::{trace, LevelFilter};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
mod db;
mod handlers;
mod return_models;
mod settings; mod settings;
mod types;
mod utils;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
settings::init(); let settings = settings::init();
let settings = settings::get();
let log_level: LevelFilter = log::LevelFilter::from_str(&settings.logging.level) let log_level: LevelFilter = log::LevelFilter::from_str(&settings.logging.level)
.unwrap_or_else(|_| panic!("Error parsing log level.")); .unwrap_or_else(|_| panic!("Error parsing log level."));
@ -26,7 +21,7 @@ async fn main() -> std::io::Result<()> {
.init() .init()
.unwrap_or_else(|_| panic!("Error initializing logger.")); .unwrap_or_else(|_| panic!("Error initializing logger."));
let pool = db::init(&settings.database).await; let pool = emgauwa_lib::db::init(&settings.database).await;
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()

View file

@ -1,7 +1,4 @@
use std::sync::RwLock;
use config::Config; use config::Config;
use lazy_static::lazy_static;
use serde_derive::Deserialize; use serde_derive::Deserialize;
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
@ -42,12 +39,8 @@ impl Default for Logging {
} }
} }
lazy_static! { pub fn init() -> Settings {
static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default()); Config::builder()
}
pub fn init() {
let settings = Config::builder()
.add_source(config::File::with_name("emgauwa-core")) .add_source(config::File::with_name("emgauwa-core"))
.add_source( .add_source(
config::Environment::with_prefix("EMGAUWA") config::Environment::with_prefix("EMGAUWA")
@ -57,11 +50,5 @@ pub fn init() {
.build() .build()
.unwrap() .unwrap()
.try_deserialize::<Settings>() .try_deserialize::<Settings>()
.unwrap_or_else(|_| panic!("Error reading settings.")); .unwrap_or_else(|_| panic!("Error reading settings."))
*SETTINGS.write().unwrap() = settings;
}
pub fn get() -> Settings {
SETTINGS.read().unwrap().clone()
} }

23
emgauwa-lib/Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "emgauwa-lib"
version = "0.1.0"
edition = "2021"
authors = ["Tobias Reisinger <tobias@msrg.cc>"]
[dependencies]
actix = "0.13"
actix-web = "4.4"
actix-web-actors = "4.2"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
chrono = { version = "0.4", features = ["serde"] }
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
libsqlite3-sys = { version = "*", features = ["bundled"] }
log = "0.4"
uuid = "1.6"
futures = "0.3"

4
emgauwa-lib/build.rs Normal file
View file

@ -0,0 +1,4 @@
fn main() {
println!("cargo:rerun-if-changed=migrations");
println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-dev.sqlite")
}

View file

@ -1,19 +1,20 @@
use log::{info, trace}; use log::{info, trace};
use sqlx::migrate::Migrator; use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use std::str::FromStr;
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period; use crate::db::model_utils::Period;
use crate::db::schedules::{Periods, Schedule}; use crate::db::schedules::{Periods, Schedule};
use crate::types::EmgauwaUid; use crate::db::types::EmgauwaUid;
pub mod errors;
pub mod models;
pub mod schedules;
pub mod tag;
pub(crate) mod errors;
mod model_utils; mod model_utils;
mod models;
pub(crate) mod schedules;
pub(crate) mod tag;
pub mod types;
static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations" static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations"
@ -52,12 +53,16 @@ async fn init_schedule(
} }
pub async fn init(db: &str) -> Pool<Sqlite> { pub async fn init(db: &str) -> Pool<Sqlite> {
let options = SqliteConnectOptions::from_str(db)
.expect("Error parsing database path")
.create_if_missing(true);
let pool: Pool<Sqlite> = SqlitePoolOptions::new() let pool: Pool<Sqlite> = SqlitePoolOptions::new()
.acquire_timeout(std::time::Duration::from_secs(1)) .acquire_timeout(std::time::Duration::from_secs(1))
.max_connections(5) .max_connections(5)
.connect(db) .connect_with(options)
.await .await
.expect("Error connecting to database."); .expect("Error connecting to database");
run_migrations(&pool).await; run_migrations(&pool).await;

View file

@ -8,7 +8,7 @@ use sqlx::Sqlite;
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::model_utils::Period; use crate::db::model_utils::Period;
use crate::db::tag::Tag; use crate::db::tag::Tag;
use crate::types::EmgauwaUid; use crate::db::types::EmgauwaUid;
#[derive(Debug, Serialize, Clone)] #[derive(Debug, Serialize, Clone)]
pub struct Schedule { pub struct Schedule {

View file

@ -10,7 +10,12 @@ use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef};
use sqlx::{Decode, Encode, Sqlite, Type}; use sqlx::{Decode, Encode, Sqlite, Type};
use uuid::Uuid; use uuid::Uuid;
use crate::types::EmgauwaUid; #[derive(Clone)]
pub enum EmgauwaUid {
Off,
On,
Any(Uuid),
}
impl EmgauwaUid { impl EmgauwaUid {
const OFF_STR: &'static str = "off"; const OFF_STR: &'static str = "off";

View file

@ -0,0 +1,2 @@
pub mod emgauwa_uid;
pub use emgauwa_uid::EmgauwaUid;

View file

@ -1,6 +1,3 @@
use std::borrow::Borrow;
use std::convert::TryFrom;
use actix_web::{delete, get, post, put, web, HttpResponse}; use actix_web::{delete, get, post, put, web, HttpResponse};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::pool::PoolConnection; use sqlx::pool::PoolConnection;
@ -9,9 +6,9 @@ use sqlx::{Pool, Sqlite};
use crate::db::errors::DatabaseError; use crate::db::errors::DatabaseError;
use crate::db::schedules::*; use crate::db::schedules::*;
use crate::db::tag::Tag; use crate::db::tag::Tag;
use crate::db::types::EmgauwaUid;
use crate::handlers::errors::ApiError; use crate::handlers::errors::ApiError;
use crate::return_models::ReturnSchedule; use crate::return_models::ReturnSchedule;
use crate::types::EmgauwaUid;
use crate::utils::vec_has_error; use crate::utils::vec_has_error;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -156,7 +153,7 @@ pub async fn update(
let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?; let schedule = Schedule::get_by_uid(&mut pool_conn, &emgauwa_uid).await?;
let schedule = schedule let schedule = schedule
.update(&mut pool_conn, data.name.as_str(), data.periods.borrow()) .update(&mut pool_conn, data.name.as_str(), &data.periods)
.await?; .await?;
schedule schedule

4
emgauwa-lib/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod db;
pub mod handlers;
pub mod return_models;
pub mod utils;

View file

@ -1,10 +0,0 @@
use uuid::Uuid;
pub mod emgauwa_uid;
#[derive(PartialEq, Clone)]
pub enum EmgauwaUid {
Off,
On,
Any(Uuid),
}