diff --git a/.env b/.env deleted file mode 100644 index 5ec33c0..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -# This is usually used in development by sqlx. It is not used in production -DATABASE_URL=sqlite://emgauwa-core.sqlite diff --git a/.gitignore b/.gitignore index 13b0cab..734f922 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,8 @@ /tests/testing_bak/ /tests/testing_latest/ -emgauwa-core.conf.d -emgauwa-core.sqlite -emgauwa-core.sqlite-* +emgauwa-*.sqlite +emgauwa-*.sqlite-* # Added by cargo diff --git a/Cargo.lock b/Cargo.lock index 5c6963e..84a38da 100644 Binary files a/Cargo.lock and b/Cargo.lock differ diff --git a/Cargo.toml b/Cargo.toml index a1f6586..f8e9722 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,30 +1,6 @@ -[package] -name = "emgauwa-core" -version = "0.1.0" -edition = "2018" -authors = ["Tobias Reisinger "] - -[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" +[workspace] +resolver = "2" +members = [ + "emgauwa-core", + "emgauwa-lib", +] diff --git a/build.rs b/build.rs deleted file mode 100644 index 827c260..0000000 --- a/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("cargo:rerun-if-changed=migrations"); -} diff --git a/emgauwa-core/Cargo.toml b/emgauwa-core/Cargo.toml new file mode 100644 index 0000000..47c0eaa --- /dev/null +++ b/emgauwa-core/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "emgauwa-core" +version = "0.1.0" +edition = "2021" +authors = ["Tobias Reisinger "] + +[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" diff --git a/emgauwa-core/build.rs b/emgauwa-core/build.rs new file mode 100644 index 0000000..41015be --- /dev/null +++ b/emgauwa-core/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-core.sqlite") +} diff --git a/src/main.rs b/emgauwa-core/src/main.rs similarity index 90% rename from src/main.rs rename to emgauwa-core/src/main.rs index 067ab86..352dcd6 100644 --- a/src/main.rs +++ b/emgauwa-core/src/main.rs @@ -2,20 +2,15 @@ use std::str::FromStr; use actix_web::middleware::TrailingSlash; use actix_web::{middleware, web, App, HttpServer}; +use emgauwa_lib::handlers; use log::{trace, LevelFilter}; use simple_logger::SimpleLogger; -mod db; -mod handlers; -mod return_models; mod settings; -mod types; -mod utils; #[actix_web::main] async fn main() -> std::io::Result<()> { - settings::init(); - let settings = settings::get(); + let settings = settings::init(); let log_level: LevelFilter = log::LevelFilter::from_str(&settings.logging.level) .unwrap_or_else(|_| panic!("Error parsing log level.")); @@ -26,7 +21,7 @@ async fn main() -> std::io::Result<()> { .init() .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 || { App::new() diff --git a/src/settings.rs b/emgauwa-core/src/settings.rs similarity index 72% rename from src/settings.rs rename to emgauwa-core/src/settings.rs index ccfa057..cff1e7a 100644 --- a/src/settings.rs +++ b/emgauwa-core/src/settings.rs @@ -1,7 +1,4 @@ -use std::sync::RwLock; - use config::Config; -use lazy_static::lazy_static; use serde_derive::Deserialize; #[derive(Clone, Debug, Deserialize)] @@ -42,12 +39,8 @@ impl Default for Logging { } } -lazy_static! { - static ref SETTINGS: RwLock = RwLock::new(Settings::default()); -} - -pub fn init() { - let settings = Config::builder() +pub fn init() -> Settings { + Config::builder() .add_source(config::File::with_name("emgauwa-core")) .add_source( config::Environment::with_prefix("EMGAUWA") @@ -57,11 +50,5 @@ pub fn init() { .build() .unwrap() .try_deserialize::() - .unwrap_or_else(|_| panic!("Error reading settings.")); - - *SETTINGS.write().unwrap() = settings; -} - -pub fn get() -> Settings { - SETTINGS.read().unwrap().clone() + .unwrap_or_else(|_| panic!("Error reading settings.")) } diff --git a/emgauwa-lib/Cargo.toml b/emgauwa-lib/Cargo.toml new file mode 100644 index 0000000..eecb0fb --- /dev/null +++ b/emgauwa-lib/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "emgauwa-lib" +version = "0.1.0" +edition = "2021" +authors = ["Tobias Reisinger "] + +[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" diff --git a/emgauwa-lib/build.rs b/emgauwa-lib/build.rs new file mode 100644 index 0000000..e9edec5 --- /dev/null +++ b/emgauwa-lib/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=migrations"); + println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-dev.sqlite") +} diff --git a/migrations/20231120000000_init.down.sql b/emgauwa-lib/migrations/20231120000000_init.down.sql similarity index 100% rename from migrations/20231120000000_init.down.sql rename to emgauwa-lib/migrations/20231120000000_init.down.sql diff --git a/migrations/20231120000000_init.up.sql b/emgauwa-lib/migrations/20231120000000_init.up.sql similarity index 100% rename from migrations/20231120000000_init.up.sql rename to emgauwa-lib/migrations/20231120000000_init.up.sql diff --git a/src/db/errors.rs b/emgauwa-lib/src/db/errors.rs similarity index 100% rename from src/db/errors.rs rename to emgauwa-lib/src/db/errors.rs diff --git a/src/db/mod.rs b/emgauwa-lib/src/db/mod.rs similarity index 79% rename from src/db/mod.rs rename to emgauwa-lib/src/db/mod.rs index 496d3e7..649664c 100644 --- a/src/db/mod.rs +++ b/emgauwa-lib/src/db/mod.rs @@ -1,19 +1,20 @@ use log::{info, trace}; use sqlx::migrate::Migrator; -use sqlx::sqlite::SqlitePoolOptions; +use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; use sqlx::{Pool, Sqlite}; +use std::str::FromStr; use crate::db::errors::DatabaseError; use crate::db::model_utils::Period; use crate::db::schedules::{Periods, Schedule}; -use crate::types::EmgauwaUid; - -pub mod errors; -pub mod models; -pub mod schedules; -pub mod tag; +use crate::db::types::EmgauwaUid; +pub(crate) mod errors; mod model_utils; +mod models; +pub(crate) mod schedules; +pub(crate) mod tag; +pub mod types; static MIGRATOR: Migrator = sqlx::migrate!(); // defaults to "./migrations" @@ -52,12 +53,16 @@ async fn init_schedule( } pub async fn init(db: &str) -> Pool { + let options = SqliteConnectOptions::from_str(db) + .expect("Error parsing database path") + .create_if_missing(true); + let pool: Pool = SqlitePoolOptions::new() .acquire_timeout(std::time::Duration::from_secs(1)) .max_connections(5) - .connect(db) + .connect_with(options) .await - .expect("Error connecting to database."); + .expect("Error connecting to database"); run_migrations(&pool).await; diff --git a/src/db/model_utils.rs b/emgauwa-lib/src/db/model_utils.rs similarity index 100% rename from src/db/model_utils.rs rename to emgauwa-lib/src/db/model_utils.rs diff --git a/src/db/models.rs b/emgauwa-lib/src/db/models.rs similarity index 100% rename from src/db/models.rs rename to emgauwa-lib/src/db/models.rs diff --git a/src/db/schedules.rs b/emgauwa-lib/src/db/schedules.rs similarity index 99% rename from src/db/schedules.rs rename to emgauwa-lib/src/db/schedules.rs index 2800ff0..afe136e 100644 --- a/src/db/schedules.rs +++ b/emgauwa-lib/src/db/schedules.rs @@ -8,7 +8,7 @@ use sqlx::Sqlite; use crate::db::errors::DatabaseError; use crate::db::model_utils::Period; use crate::db::tag::Tag; -use crate::types::EmgauwaUid; +use crate::db::types::EmgauwaUid; #[derive(Debug, Serialize, Clone)] pub struct Schedule { diff --git a/src/db/tag.rs b/emgauwa-lib/src/db/tag.rs similarity index 100% rename from src/db/tag.rs rename to emgauwa-lib/src/db/tag.rs diff --git a/src/types/emgauwa_uid.rs b/emgauwa-lib/src/db/types/emgauwa_uid.rs similarity index 98% rename from src/types/emgauwa_uid.rs rename to emgauwa-lib/src/db/types/emgauwa_uid.rs index 7fd124e..6287ad4 100644 --- a/src/types/emgauwa_uid.rs +++ b/emgauwa-lib/src/db/types/emgauwa_uid.rs @@ -10,7 +10,12 @@ use sqlx::sqlite::{SqliteTypeInfo, SqliteValueRef}; use sqlx::{Decode, Encode, Sqlite, Type}; use uuid::Uuid; -use crate::types::EmgauwaUid; +#[derive(Clone)] +pub enum EmgauwaUid { + Off, + On, + Any(Uuid), +} impl EmgauwaUid { const OFF_STR: &'static str = "off"; diff --git a/emgauwa-lib/src/db/types/mod.rs b/emgauwa-lib/src/db/types/mod.rs new file mode 100644 index 0000000..04e5e86 --- /dev/null +++ b/emgauwa-lib/src/db/types/mod.rs @@ -0,0 +1,2 @@ +pub mod emgauwa_uid; +pub use emgauwa_uid::EmgauwaUid; diff --git a/src/handlers/errors.rs b/emgauwa-lib/src/handlers/errors.rs similarity index 100% rename from src/handlers/errors.rs rename to emgauwa-lib/src/handlers/errors.rs diff --git a/src/handlers/mod.rs b/emgauwa-lib/src/handlers/mod.rs similarity index 100% rename from src/handlers/mod.rs rename to emgauwa-lib/src/handlers/mod.rs diff --git a/src/handlers/v1/mod.rs b/emgauwa-lib/src/handlers/v1/mod.rs similarity index 100% rename from src/handlers/v1/mod.rs rename to emgauwa-lib/src/handlers/v1/mod.rs diff --git a/src/handlers/v1/schedules.rs b/emgauwa-lib/src/handlers/v1/schedules.rs similarity index 97% rename from src/handlers/v1/schedules.rs rename to emgauwa-lib/src/handlers/v1/schedules.rs index 1835f25..c615c3a 100644 --- a/src/handlers/v1/schedules.rs +++ b/emgauwa-lib/src/handlers/v1/schedules.rs @@ -1,6 +1,3 @@ -use std::borrow::Borrow; -use std::convert::TryFrom; - use actix_web::{delete, get, post, put, web, HttpResponse}; use serde::{Deserialize, Serialize}; use sqlx::pool::PoolConnection; @@ -9,9 +6,9 @@ use sqlx::{Pool, Sqlite}; use crate::db::errors::DatabaseError; use crate::db::schedules::*; use crate::db::tag::Tag; +use crate::db::types::EmgauwaUid; use crate::handlers::errors::ApiError; use crate::return_models::ReturnSchedule; -use crate::types::EmgauwaUid; use crate::utils::vec_has_error; #[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 - .update(&mut pool_conn, data.name.as_str(), data.periods.borrow()) + .update(&mut pool_conn, data.name.as_str(), &data.periods) .await?; schedule diff --git a/src/handlers/v1/ws/controllers.rs b/emgauwa-lib/src/handlers/v1/ws/controllers.rs similarity index 100% rename from src/handlers/v1/ws/controllers.rs rename to emgauwa-lib/src/handlers/v1/ws/controllers.rs diff --git a/src/handlers/v1/ws/mod.rs b/emgauwa-lib/src/handlers/v1/ws/mod.rs similarity index 100% rename from src/handlers/v1/ws/mod.rs rename to emgauwa-lib/src/handlers/v1/ws/mod.rs diff --git a/emgauwa-lib/src/lib.rs b/emgauwa-lib/src/lib.rs new file mode 100644 index 0000000..fc678b3 --- /dev/null +++ b/emgauwa-lib/src/lib.rs @@ -0,0 +1,4 @@ +pub mod db; +pub mod handlers; +pub mod return_models; +pub mod utils; diff --git a/src/return_models.rs b/emgauwa-lib/src/return_models.rs similarity index 100% rename from src/return_models.rs rename to emgauwa-lib/src/return_models.rs diff --git a/src/utils.rs b/emgauwa-lib/src/utils.rs similarity index 100% rename from src/utils.rs rename to emgauwa-lib/src/utils.rs diff --git a/src/types.rs b/src/types.rs deleted file mode 100644 index f75f885..0000000 --- a/src/types.rs +++ /dev/null @@ -1,10 +0,0 @@ -use uuid::Uuid; - -pub mod emgauwa_uid; - -#[derive(PartialEq, Clone)] -pub enum EmgauwaUid { - Off, - On, - Any(Uuid), -}