From b1e328e3186e6d8dc08cd4125e7b8ab92a8ca390 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Sat, 31 Aug 2024 19:55:36 -0700 Subject: [PATCH] migrate to global structs --- Cargo.lock | 5 +-- Cargo.toml | 25 ++++++------ src/{config/mod.rs => config.rs} | 14 ++++--- src/database/kv.rs | 1 - src/database/mongo.rs | 30 +++----------- src/database/redis.rs | 8 +--- src/file.rs | 35 ---------------- src/helpers.rs | 6 +++ src/helpers/file.rs | 22 ++++++++++ src/main.rs | 43 ++++++-------------- src/modules.rs | 2 + src/modules/file.rs | 11 +++++ src/modules/http.rs | 9 +--- src/modules/response.rs | 1 - src/structs.rs | 3 ++ src/{config/structs.rs => structs/config.rs} | 12 +++--- src/structs/modules.rs | 33 +++++++++++++++ src/structs/template.rs | 17 ++++++++ 18 files changed, 143 insertions(+), 134 deletions(-) rename src/{config/mod.rs => config.rs} (87%) delete mode 100644 src/file.rs create mode 100644 src/helpers/file.rs create mode 100644 src/modules/file.rs create mode 100644 src/structs.rs rename src/{config/structs.rs => structs/config.rs} (85%) create mode 100644 src/structs/modules.rs create mode 100644 src/structs/template.rs diff --git a/Cargo.lock b/Cargo.lock index ff38180..33998f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1651,9 +1651,9 @@ dependencies = [ [[package]] name = "macros-rs" -version = "1.2.2" +version = "1.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad2eda5e29889d2b7602fc6e2e8db2468fda0633aea0108f0469569bbac44b22" +checksum = "c58aecbe9f397279912daa8bec0c399df037ff111617d844470793f83cc8d850" dependencies = [ "termcolor", ] @@ -2430,7 +2430,6 @@ dependencies = [ "colored", "fancy-regex", "home", - "lazy_static", "macros-rs", "mime", "mongodb", diff --git a/Cargo.toml b/Cargo.toml index 594e6a9..facb826 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,26 +10,25 @@ description = "barebones http scripting" strip = true [dependencies] -actix-web = "4.9.0" -anyhow = "1.0.86" -askama = "0.12.1" -colored = "2.1.0" +peg = "0.8.4" home = "0.5.9" -lazy_static = "1.5.0" -macros-rs = "1.2.2" mime = "0.3.17" -peg = "0.8.4" +toml = "0.8.19" redis = "0.24.0" +tokio = "1.40.0" +anyhow = "1.0.86" +askama = "0.12.1" +colored = "2.1.0" rhai-fs = "0.1.3" rhai-url = "0.0.5" -serde_json = "1.0.127" -smartstring = "1.0.1" -termcolor = "1.4.1" -tokio = "1.40.0" -toml = "0.8.19" tracing = "0.1.40" -tracing-bunyan-formatter = "0.3.9" +actix-web = "4.9.0" +macros-rs = "1.2.5" +termcolor = "1.4.1" +smartstring = "1.0.1" +serde_json = "1.0.127" tracing-subscriber = "0.3.18" +tracing-bunyan-formatter = "0.3.9" [dependencies.mongodb] default-features = false diff --git a/src/config/mod.rs b/src/config.rs similarity index 87% rename from src/config/mod.rs rename to src/config.rs index ff7f517..873a3fe 100644 --- a/src/config/mod.rs +++ b/src/config.rs @@ -1,16 +1,18 @@ -pub mod structs; +use crate::{helpers::prelude::*, structs::config::*}; -use crate::file::{self, exists}; use colored::Colorize; -use macros_rs::fmt::{crashln, string}; use pickledb::SerializationMethod; use std::fs; -use structs::{Config, Settings}; + +use macros_rs::{ + fmt::{crashln, string}, + fs::file_exists, +}; pub fn read() -> Config { let config_path = format!("config.toml"); - if !exists::file(config_path.clone()).unwrap() { + if !file_exists!(&config_path) { let config = Config { env: None, database: None, @@ -32,7 +34,7 @@ pub fn read() -> Config { tracing::info!(path = config_path, created = true, "config"); } - file::read(config_path) + read_toml(config_path) } impl Config { diff --git a/src/database/kv.rs b/src/database/kv.rs index 350e03a..42867be 100644 --- a/src/database/kv.rs +++ b/src/database/kv.rs @@ -1,5 +1,4 @@ use crate::config; - use macros_rs::{fmt::string, fs::file_exists}; use pickledb::{PickleDb, PickleDbDumpPolicy}; use rhai::{plugin::*, FnNamespace}; diff --git a/src/database/mongo.rs b/src/database/mongo.rs index a1a8352..c630517 100644 --- a/src/database/mongo.rs +++ b/src/database/mongo.rs @@ -1,33 +1,15 @@ -use crate::{config, helpers::collection_exists}; - +use crate::{config, helpers::collection_exists, structs::modules::*}; use rhai::{plugin::*, serde::to_dynamic, Array, FnNamespace}; -use serde::{Deserialize, Serialize}; use std::sync::Arc; use mongodb::{ bson::{doc, Document}, results::{CollectionSpecification, DeleteResult, InsertOneResult, UpdateResult}, - sync::{Client as MongoClient, Collection, Cursor, Database}, + sync::{Client as MongoClient, Collection, Cursor}, }; #[export_module] pub mod mongo_db { - #[derive(Clone)] - pub struct Client { - pub client: Option, - } - - #[derive(Clone)] - pub struct Mongo { - pub db: Option, - } - - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct MongoDynamic(Dynamic); - - unsafe impl Send for MongoDynamic {} - unsafe impl Sync for MongoDynamic {} - trait MongoVec { fn into_vec(self) -> Vec; } @@ -41,14 +23,14 @@ pub mod mongo_db { fn into(self) -> MongoDynamic { MongoDynamic(self) } } - impl FromIterator for Array { - fn from_iter>(iter: I) -> Self { iter.into_iter().map(|m| m.0).collect() } - } - impl MongoVec for Array { fn into_vec(self) -> Vec { self.into_iter().map(|m| m.into_map()).collect() } } + impl FromIterator for Array { + fn from_iter>(iter: I) -> Self { iter.into_iter().map(|m| m.0).collect() } + } + impl MongoDocument for Dynamic { fn into_doc(self) -> Document { Document::from( diff --git a/src/database/redis.rs b/src/database/redis.rs index 99c2bba..f4a291a 100644 --- a/src/database/redis.rs +++ b/src/database/redis.rs @@ -1,5 +1,4 @@ -use crate::config; - +use crate::{config, structs::modules::*}; use macros_rs::fmt::string; use redis::{Client as RedisClient, Commands}; use rhai::{plugin::*, serde::to_dynamic, FnNamespace}; @@ -7,11 +6,6 @@ use std::collections::BTreeMap; #[export_module] pub mod redis_db { - #[derive(Clone)] - pub struct Redis { - pub client: Option, - } - pub fn connect() -> Redis { let config = config::read().database.unwrap(); match RedisClient::open(config.redis.unwrap().server) { diff --git a/src/file.rs b/src/file.rs deleted file mode 100644 index 3edf949..0000000 --- a/src/file.rs +++ /dev/null @@ -1,35 +0,0 @@ -use colored::Colorize; -use macros_rs::fmt::{crashln, str, string}; -use rhai::plugin::*; - -use std::{ - env, fs, - path::{Path, PathBuf}, -}; - -pub fn cwd() -> PathBuf { - match env::current_dir() { - Ok(path) => path, - Err(_) => crashln!("Unable to find current working directory"), - } -} - -#[export_module] -pub mod exists { - #[rhai_fn(global, return_raw, name = "folder")] - pub fn folder(dir_name: String) -> Result> { Ok(Path::new(str!(dir_name)).is_dir()) } - #[rhai_fn(global, return_raw, name = "file")] - pub fn file(file_name: String) -> Result> { Ok(Path::new(str!(file_name)).exists()) } -} - -pub fn read(path: String) -> T { - let contents = match fs::read_to_string(&path) { - Ok(contents) => contents, - Err(err) => crashln!("Cannot find {path}.\n{}", string!(err).white()), - }; - - match toml::from_str(&contents).map_err(|err| string!(err)) { - Ok(parsed) => parsed, - Err(err) => crashln!("Cannot parse {path}.\n{}", err.white()), - } -} diff --git a/src/helpers.rs b/src/helpers.rs index 6fa7b31..4ca456b 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,8 +1,14 @@ +pub mod file; + use actix_web::http::StatusCode; use mongodb::{bson::doc, sync::Database}; use regex::{Captures, Regex}; use rhai::{plugin::EvalAltResult, Engine, ParseError, AST}; +pub mod prelude { + pub use super::file::*; +} + pub fn rm_first(s: &str) -> &str { let mut chars = s.chars(); chars.next(); diff --git a/src/helpers/file.rs b/src/helpers/file.rs new file mode 100644 index 0000000..ea3e18b --- /dev/null +++ b/src/helpers/file.rs @@ -0,0 +1,22 @@ +use colored::Colorize; +use macros_rs::fmt::{crashln, string}; +use std::{env, fs, path::PathBuf}; + +pub fn cwd() -> PathBuf { + match env::current_dir() { + Ok(path) => path, + Err(_) => crashln!("Unable to find current working directory"), + } +} + +pub fn read_toml(path: String) -> T { + let contents = match fs::read_to_string(&path) { + Ok(contents) => contents, + Err(err) => crashln!("Cannot find {path}.\n{}", string!(err).white()), + }; + + match toml::from_str(&contents).map_err(|err| string!(err)) { + Ok(parsed) => parsed, + Err(err) => crashln!("Cannot parse {path}.\n{}", err.white()), + } +} diff --git a/src/main.rs b/src/main.rs index f0160a5..3e582f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ mod config; mod database; -mod file; mod helpers; mod modules; +mod structs; +use helpers::prelude::*; use modules::prelude::*; +use structs::{config::*, template::*}; -use askama::Template; -use config::structs::Config; -use lazy_static::lazy_static; use mime::Mime; use regex::{Captures, Error, Regex}; @@ -25,6 +24,7 @@ use rhai_url::UrlPackage; use macros_rs::{ exp::ternary, fmt::{crashln, str, string}, + obj::lazy_lock, os::set_env_sync, }; @@ -34,30 +34,13 @@ use actix_web::{ App, HttpRequest, HttpResponse, HttpServer, Responder, }; -#[derive(Template)] -#[template(path = "error.html")] -struct ServerError { - error: String, - context: Vec<(String, String)>, -} - -#[derive(Template)] -#[template(path = "message.html")] -struct Message<'a> { - code: u16, - note: &'a str, - error: &'a str, - message: String, -} - -// convert to peg -lazy_static! { - static ref R_INDEX: Result = Regex::new(r"index\s*\{"); - static ref R_ERR: Result = Regex::new(r"(\b\d{3})\s*\{"); - static ref R_FN: Result = Regex::new(r"(\w+)\((.*?)\)\s*\{"); - static ref R_DOT: Result = Regex::new(r"\.(\w+)\((.*?)\)\s*\{"); - static ref R_WILD: Result = Regex::new(r"\*\s*\{|wildcard\s*\{"); - static ref R_SLASH: Result = Regex::new(r"(?m)\/(?=.*\((.*?)\)\s*\{[^{]*$)"); +lazy_lock! { + static R_INDEX: Result = Regex::new(r"index\s*\{"); + static R_ERR: Result = Regex::new(r"(\b\d{3})\s*\{"); + static R_FN: Result = Regex::new(r"(\w+)\((.*?)\)\s*\{"); + static R_DOT: Result = Regex::new(r"\.(\w+)\((.*?)\)\s*\{"); + static R_WILD: Result = Regex::new(r"\*\s*\{|wildcard\s*\{"); + static R_SLASH: Result = Regex::new(r"(?m)\/(?=.*\((.*?)\)\s*\{[^{]*$)"); } pub fn response(data: String, content_type: String, status_code: i64) -> (String, ContentType, StatusCode) { @@ -169,7 +152,7 @@ async fn handler(req: HttpRequest, config: Data) -> impl Responder { let json = exported_module!(json); let http = exported_module!(http); - let exists = exported_module!(file::exists); + let exists = exported_module!(exists); let mut engine = Engine::new(); let mut scope = Scope::new(); @@ -232,8 +215,8 @@ async fn handler(req: HttpRequest, config: Data) -> impl Responder { scope.push("request", request.to_dynamic()); engine + .register_fn("cwd", cwd) .register_fn("proxy", proxy) - .register_fn("cwd", file::cwd) .register_fn("response", response) .register_fn("text", default::text) .register_fn("json", default::json) diff --git a/src/modules.rs b/src/modules.rs index 8b6d194..4c99863 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -1,8 +1,10 @@ +pub mod file; pub mod http; pub mod parse; pub mod response; pub mod prelude { + pub use super::file::*; pub use super::http::*; pub use super::parse::*; pub use super::response::*; diff --git a/src/modules/file.rs b/src/modules/file.rs new file mode 100644 index 0000000..8d58d08 --- /dev/null +++ b/src/modules/file.rs @@ -0,0 +1,11 @@ +use macros_rs::fmt::str; +use rhai::plugin::*; +use std::path::Path; + +#[export_module] +pub mod exists { + #[rhai_fn(global, return_raw, name = "folder")] + pub fn folder(dir_name: String) -> Result> { Ok(Path::new(str!(dir_name)).is_dir()) } + #[rhai_fn(global, return_raw, name = "file")] + pub fn file(file_name: String) -> Result> { Ok(Path::new(str!(file_name)).exists()) } +} diff --git a/src/modules/http.rs b/src/modules/http.rs index 5ae1bd2..0b40714 100644 --- a/src/modules/http.rs +++ b/src/modules/http.rs @@ -1,3 +1,4 @@ +use crate::structs::modules::*; use macros_rs::fmt::{str, string}; use reqwest::blocking::Client as ReqwestClient; use rhai::{plugin::*, FnNamespace, Map}; @@ -5,14 +6,6 @@ use smartstring::alias::String as SmString; #[export_module] pub mod http { - #[derive(Clone)] - pub struct Http { - pub length: Option, - pub status: u16, - pub err: Option, - pub body: Option, - } - impl From for Map { fn from(http: Http) -> Self { let mut map = Map::new(); diff --git a/src/modules/response.rs b/src/modules/response.rs index cfe9cba..5c78f24 100644 --- a/src/modules/response.rs +++ b/src/modules/response.rs @@ -1,5 +1,4 @@ use crate::helpers::convert_status; - use actix_web::http::{header::ContentType, StatusCode}; use rhai::plugin::*; diff --git a/src/structs.rs b/src/structs.rs new file mode 100644 index 0000000..5eb1755 --- /dev/null +++ b/src/structs.rs @@ -0,0 +1,3 @@ +pub mod config; +pub mod modules; +pub mod template; diff --git a/src/config/structs.rs b/src/structs/config.rs similarity index 85% rename from src/config/structs.rs rename to src/structs/config.rs index 7e5bc1d..26d0ee1 100644 --- a/src/config/structs.rs +++ b/src/structs/config.rs @@ -18,23 +18,23 @@ pub struct Settings { #[derive(Clone, Serialize, Deserialize)] pub struct Database { - pub kv: Option, - pub mongo: Option, - pub redis: Option, + pub kv: Option, + pub mongo: Option, + pub redis: Option, } #[derive(Clone, Serialize, Deserialize)] -pub struct KV { +pub struct KVConfig { pub method: String, } #[derive(Clone, Serialize, Deserialize)] -pub struct Redis { +pub struct RedisConfig { pub server: String, } #[derive(Clone, Serialize, Deserialize)] -pub struct Mongo { +pub struct MongoConfig { pub server: Option, pub advanced: Option, } diff --git a/src/structs/modules.rs b/src/structs/modules.rs new file mode 100644 index 0000000..22cab7f --- /dev/null +++ b/src/structs/modules.rs @@ -0,0 +1,33 @@ +use mongodb::sync::{Client as MongoClient, Database}; +use redis::Client as RedisClient; +use rhai::Dynamic; +use serde::{Deserialize, Serialize}; + +#[derive(Clone)] +pub struct Http { + pub length: Option, + pub status: u16, + pub err: Option, + pub body: Option, +} + +#[derive(Clone)] +pub struct Redis { + pub client: Option, +} + +#[derive(Clone)] +pub struct Client { + pub client: Option, +} + +#[derive(Clone)] +pub struct Mongo { + pub db: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct MongoDynamic(pub Dynamic); + +unsafe impl Send for MongoDynamic {} +unsafe impl Sync for MongoDynamic {} diff --git a/src/structs/template.rs b/src/structs/template.rs new file mode 100644 index 0000000..034d21b --- /dev/null +++ b/src/structs/template.rs @@ -0,0 +1,17 @@ +pub use askama::Template; + +#[derive(Template)] +#[template(path = "error.html")] +pub struct ServerError { + pub error: String, + pub context: Vec<(String, String)>, +} + +#[derive(Template)] +#[template(path = "message.html")] +pub struct Message<'a> { + pub code: u16, + pub note: &'a str, + pub error: &'a str, + pub message: String, +} -- GitLab