From 8a094e367f58cfd6c4f86e990c29f9f572d5f14e Mon Sep 17 00:00:00 2001 From: theMackabu <theMackabu@gmail.com> Date: Sun, 17 Dec 2023 19:34:22 -0800 Subject: [PATCH] start redis --- Cargo.lock | 2 +- Cargo.toml | 2 +- Maidfile.toml | 2 +- app.routes | 2 +- src/config/structs.rs | 26 ++++++++++++++++---------- src/database/kv.rs | 14 ++------------ src/main.rs | 12 +++++++++++- 7 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87f39fb..087162f 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 2b2dc81..93f9eb2 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 933b42d..8064702 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 5848660..0f61c31 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 56cbec9..da3b66c 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 3e2ad6d..28f2343 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 0a3b467..4edc037 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)] -- GitLab