added settings crate, creating settings on web startup
This commit is contained in:
parent
9c2b43ec3c
commit
a85c1431c8
7 changed files with 148 additions and 7 deletions
40
Cargo.lock
generated
40
Cargo.lock
generated
|
|
@ -220,8 +220,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
|
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"time 0.1.45",
|
||||||
|
"wasm-bindgen",
|
||||||
"winapi 0.3.9",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -281,7 +284,7 @@ dependencies = [
|
||||||
"rand",
|
"rand",
|
||||||
"sha2",
|
"sha2",
|
||||||
"subtle",
|
"subtle",
|
||||||
"time",
|
"time 0.3.20",
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -523,6 +526,7 @@ dependencies = [
|
||||||
"rocket",
|
"rocket",
|
||||||
"rocket_db_pools",
|
"rocket_db_pools",
|
||||||
"rocket_dyn_templates",
|
"rocket_dyn_templates",
|
||||||
|
"settings",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -740,7 +744,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if 1.0.0",
|
"cfg-if 1.0.0",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1238,7 +1242,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"windows-sys 0.45.0",
|
"windows-sys 0.45.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -1782,7 +1786,7 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"state",
|
"state",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"time",
|
"time 0.3.20",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
|
|
@ -1864,7 +1868,7 @@ dependencies = [
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"stable-pattern",
|
"stable-pattern",
|
||||||
"state",
|
"state",
|
||||||
"time",
|
"time 0.3.20",
|
||||||
"tokio",
|
"tokio",
|
||||||
"uncased",
|
"uncased",
|
||||||
]
|
]
|
||||||
|
|
@ -1976,6 +1980,15 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "settings"
|
||||||
|
version = "0.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"database",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha2"
|
name = "sha2"
|
||||||
version = "0.10.6"
|
version = "0.10.6"
|
||||||
|
|
@ -2280,6 +2293,17 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "time"
|
||||||
|
version = "0.1.45"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"wasi 0.10.0+wasi-snapshot-preview1",
|
||||||
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.20"
|
version = "0.3.20"
|
||||||
|
|
@ -2663,6 +2687,12 @@ dependencies = [
|
||||||
"try-lock",
|
"try-lock",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,5 @@ rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] }
|
||||||
infer = { version = "0.12.0", default-features = false }
|
infer = { version = "0.12.0", default-features = false }
|
||||||
|
|
||||||
# local crates
|
# local crates
|
||||||
database_pool = { path = "../database_pool" }
|
database_pool = { path = "../database_pool" }
|
||||||
|
settings = { path = "../settings" }
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use database_pool::run_migrations;
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::AdHoc;
|
||||||
use rocket::{error, fairing, info, Build, Rocket};
|
use rocket::{error, fairing, info, Build, Rocket};
|
||||||
use rocket_db_pools::{sqlx, Database as RocketDatabase};
|
use rocket_db_pools::{sqlx, Database as RocketDatabase};
|
||||||
|
use settings::Settings;
|
||||||
|
|
||||||
#[derive(RocketDatabase)]
|
#[derive(RocketDatabase)]
|
||||||
#[database("ezidam")]
|
#[database("ezidam")]
|
||||||
|
|
@ -25,10 +26,26 @@ impl Database {
|
||||||
match run_migrations(&db.0).await {
|
match run_migrations(&db.0).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
info!("Migrations ran successfully");
|
info!("Migrations ran successfully");
|
||||||
Ok(rocket)
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to run migrations: {}", e);
|
error!("Failed to run migrations: {}", e);
|
||||||
|
return Err(rocket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize settings
|
||||||
|
match Settings::init(&db.0).await {
|
||||||
|
Ok(insertion) => {
|
||||||
|
if insertion.is_some() {
|
||||||
|
// This should happen only once in the lifetime of the database
|
||||||
|
info!("Created settings in database");
|
||||||
|
} else {
|
||||||
|
info!("Found existing settings in database");
|
||||||
|
}
|
||||||
|
Ok(rocket)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to interact with settings: {}", e);
|
||||||
Err(rocket)
|
Err(rocket)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
crates/settings/Cargo.toml
Normal file
9
crates/settings/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "settings"
|
||||||
|
version = "0.0.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
database = { path = "../database" }
|
||||||
|
thiserror = { workspace = true }
|
||||||
|
chrono = { workspace = true }
|
||||||
74
crates/settings/src/lib.rs
Normal file
74
crates/settings/src/lib.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use database::sqlx::SqliteExecutor;
|
||||||
|
use database::Settings as DatabaseSettings;
|
||||||
|
|
||||||
|
// error
|
||||||
|
#[derive(thiserror::Error)]
|
||||||
|
// the rest
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Database: {0}")]
|
||||||
|
Database(#[from] database::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
// the rest
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Settings {
|
||||||
|
created_at: DateTime<Utc>,
|
||||||
|
updated_at: DateTime<Utc>,
|
||||||
|
business_name: String,
|
||||||
|
business_logo: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_BUSINESS_NAME: &str = "ezidam";
|
||||||
|
const DEFAULT_BUSINESS_LOGO: &[u8] = include_bytes!("../../../logo/ezidam.png");
|
||||||
|
|
||||||
|
impl From<DatabaseSettings> for Settings {
|
||||||
|
fn from(db: DatabaseSettings) -> Self {
|
||||||
|
Self {
|
||||||
|
created_at: db.created_at,
|
||||||
|
updated_at: db.updated_at,
|
||||||
|
business_name: db
|
||||||
|
.business_name
|
||||||
|
.unwrap_or_else(|| DEFAULT_BUSINESS_NAME.into()),
|
||||||
|
business_logo: db
|
||||||
|
.business_logo
|
||||||
|
.unwrap_or_else(|| DEFAULT_BUSINESS_LOGO.to_vec()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Settings {
|
||||||
|
pub fn business_logo(&self) -> &[u8] {
|
||||||
|
self.business_logo.as_slice()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Settings {
|
||||||
|
pub async fn init(conn: impl SqliteExecutor<'_>) -> Result<Option<()>, Error> {
|
||||||
|
Ok(DatabaseSettings::init(conn).await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get(conn: impl SqliteExecutor<'_>) -> Result<Self, Error> {
|
||||||
|
Ok(DatabaseSettings::get(conn).await.map(Self::from)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_business_name(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
business_name: &str,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
DatabaseSettings::set_business_name(conn, Some(business_name)).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// **Warning**: No checks are performed, make sure the bytes are safe!
|
||||||
|
pub async fn set_business_logo(
|
||||||
|
conn: impl SqliteExecutor<'_>,
|
||||||
|
business_logo: &[u8],
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
DatabaseSettings::set_business_logo(conn, Some(business_logo)).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
logo/ezidam.png
Normal file
BIN
logo/ezidam.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
10
logo/readme.md
Normal file
10
logo/readme.md
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# logo
|
||||||
|
|
||||||
|
## colors
|
||||||
|
|
||||||
|
- the blue is `#3a88fe`
|
||||||
|
|
||||||
|
## `logo.png`
|
||||||
|
|
||||||
|
this file is used when building the project, compilation will fail if this file is not present
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue