diff --git a/Cargo.lock b/Cargo.lock index 87f39fb59526d2c1bbbfc00ad0f9c8c5d5ebb17b..087162f4417893a466e5516cce56eb3aa015b1c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2259,7 +2259,7 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "script" -version = "0.2.3" +version = "0.3.0" dependencies = [ "actix-web", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 2b2dc81e1ce4d9f352b64ea38d03b386490c4469..93f9eb2d68c609999b491d410841e9aca3e88a14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "script" -version = "0.2.3" +version = "0.3.0" edition = "2021" license = "MIT" repository = "https://lab.themackabu.dev/self/script" diff --git a/Maidfile.toml b/Maidfile.toml index 933b42df938dc4bc877d5462403f6999ba514b81..8064702e3d8b1340f44e8488f481d3de9a4ffd41 100644 --- a/Maidfile.toml +++ b/Maidfile.toml @@ -1,6 +1,6 @@ [project] name = "script" -version = "0.2.3" +version = "0.3.0" [tasks] clean = { script = ["rm -rf bin", "mkdir bin"] } diff --git a/app.routes b/app.routes index 58486606c9aaed863db6468b2edb5e2a9b7c10dc..0f61c3126aece91504ea3b16b0e2d3f283564d57 100644 --- a/app.routes +++ b/app.routes @@ -13,7 +13,7 @@ example() { #[route("/db")] db() { - let db = kv::load("test.db"); + let db = db::kv::load("test.db"); db.set("some.key", json::dump(#{name: "John", id: 50})); let data = json::parse(db.get("some.key")); diff --git a/src/config/structs.rs b/src/config/structs.rs index 56cbec99c60c7fedce8633d446579100b95ad600..da3b66c856779fb1037744e1e4607ef911f41236 100644 --- a/src/config/structs.rs +++ b/src/config/structs.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use std::{collections::BTreeMap, path::PathBuf}; use toml::Value; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct Config { pub workers: Vec<PathBuf>, pub settings: Settings, @@ -10,43 +10,49 @@ pub struct Config { pub env: Option<BTreeMap<String, Value>>, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct Settings { pub address: String, pub port: u16, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct Database { pub kv: Option<KV>, - pub sqlite: Option<SQlite>, pub mongo: Option<Mongo>, + pub redis: Option<Redis>, + pub sqlite: Option<SQlite>, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct KV { pub method: String, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct SQlite { pub connect: String, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] +pub struct Redis { + pub server: String, +} + +#[derive(Clone, Serialize, Deserialize)] pub struct Mongo { - pub url: Option<String>, + pub server: Option<String>, pub advanced: Option<MongoAdvanced>, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct MongoAdvanced { pub port: u64, pub address: String, pub auth: Option<MongoAuth>, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct MongoAuth { pub username: String, pub password: String, diff --git a/src/database/kv.rs b/src/database/kv.rs index 3e2ad6d7d5f9a45a270240e13d3d87d2ef6633ae..28f2343325c3dbfed59e04185b073d944f63087c 100644 --- a/src/database/kv.rs +++ b/src/database/kv.rs @@ -1,18 +1,8 @@ -use macros_rs::string; +use crate::config; use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod}; -use crate::config::{ - self, - structs::{Database, KV}, -}; - pub fn load(path: String) -> PickleDb { - let config = config::read().database.unwrap_or(Database { - kv: Some(KV { method: string!("default") }), - mongo: None, - sqlite: None, - }); - + let config = config::read().database.unwrap(); let method = match config.kv.unwrap().method.as_str() { "json" | "default" => SerializationMethod::Json, "yaml" | "yml" => SerializationMethod::Yaml, diff --git a/src/main.rs b/src/main.rs index 0a3b4671a2dc686ff1e1505d72d25c069a13fa13..4edc0372e8326aba732b4597f5cf145244db3043 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,7 +223,7 @@ mod mongo { pub fn connect() -> Client { let config = config::read().database.unwrap(); - match MongoClient::with_uri_str(config.mongo.unwrap().url.unwrap_or("".to_string())) { + match MongoClient::with_uri_str(config.mongo.unwrap().server.unwrap_or("".to_string())) { Ok(client) => Client { client: Some(client) }, Err(_) => Client { client: None }, } @@ -420,6 +420,12 @@ mod kv { #[export_module] mod sqlite {} +#[export_module] +mod redis {} + +#[export_module] +mod sqlx {} + #[export_module] mod http { #[derive(Clone)] @@ -608,6 +614,10 @@ async fn handler(url: Path<String>, req: HttpRequest, config: Data<Config>) -> i let mongo = exported_module!(mongo); engine.register_static_module("mongo", mongo.into()); } + if let Some(_) = &database.redis { + let redis = exported_module!(redis); + engine.register_static_module("redis", redis.into()); + } } #[derive(Clone)]