chore: add app_env
This commit is contained in:
parent
72e80cb2c4
commit
e5bcd91cc8
10
app_env/.gitignore
vendored
Normal file
10
app_env/.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
/target
|
||||
target
|
||||
Cargo.lock
|
||||
.cache
|
||||
.temp
|
||||
.env
|
||||
*.log
|
||||
.DS_Store
|
||||
logs
|
||||
tmp
|
33
app_env/Cargo.toml
Normal file
33
app_env/Cargo.toml
Normal file
@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "app_env"
|
||||
version = "0.1.0"
|
||||
authors = ["JesusPerez <jpl@jesusperez.pro>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
anyhow = "1.0.40"
|
||||
##
|
||||
base64 = "0.13.0"
|
||||
dotenv = "0.15.0"
|
||||
envmnt = "0.9.0"
|
||||
json = "0.12.4"
|
||||
once_cell = "1.7.2"
|
||||
parking_lot = "0.11.1"
|
||||
rand = "0.8.3"
|
||||
regex = "1.4.3"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_derive = "1.0.125"
|
||||
serde_json = "1.0.64"
|
||||
serde_yaml = "0.8.17"
|
||||
tera = "1.8.0"
|
||||
toml = "0.5.8"
|
||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||
app_tools = { version = "0.1.0", path = "../../utils/app_tools" }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "0.7.2"
|
||||
# test-case = "1.1.0"
|
14
app_env/README.md
Normal file
14
app_env/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
# App Env library
|
||||
|
||||
This is multiple **types** collection library
|
||||
|
||||
## appenv
|
||||
|
||||
**AppEnv** collects application evironment like:
|
||||
|
||||
- info
|
||||
- config
|
||||
- collections
|
||||
- modules
|
||||
|
||||
Basic info about application in **AppInfo**
|
9
app_env/TODO.md
Normal file
9
app_env/TODO.md
Normal file
@ -0,0 +1,9 @@
|
||||
# App Env library
|
||||
|
||||
- [ ] Implement tests for **config**
|
||||
|
||||
- [ ] Extend **config** to support other **data stores**
|
||||
|
||||
- [ ] Add encryption to **config**
|
||||
|
||||
- [ ] Complete implementation for this types by moving some code here.
|
103
app_env/src/appdata.rs
Normal file
103
app_env/src/appdata.rs
Normal file
@ -0,0 +1,103 @@
|
||||
//
|
||||
// Copyright 2021, Jesús Pérez Lorenzo
|
||||
//
|
||||
// use core::fmt;
|
||||
// use futures::lock::Mutex;
|
||||
// use std::sync::Arc;
|
||||
|
||||
// use lazy_static::lazy_static;
|
||||
// use once_cell::sync::Lazy;
|
||||
// use std::sync::Arc;
|
||||
// // use tokio::sync::Mutex;
|
||||
// use parking_lot::RwLock;
|
||||
|
||||
use std::collections::HashMap;
|
||||
// use serde::{Deserialize,Serialize};
|
||||
|
||||
use app_tools::{hash_from_data};
|
||||
|
||||
use crate::appenv::AppEnv;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AppData {
|
||||
// pub schema: Arc<Mutex<RootSchema>>,
|
||||
pub env: AppEnv,
|
||||
// pub vault: ServerVault,
|
||||
pub tera: tera::Tera,
|
||||
pub ctx: tera::Context,
|
||||
}
|
||||
impl AppData {
|
||||
/// Schema creation for `AppEnv`
|
||||
#[must_use]
|
||||
pub fn new(env: AppEnv) -> Self {
|
||||
let templates_path = env.config.templates_path.to_owned();
|
||||
let default_module = env.config.default_module.to_owned();
|
||||
// let arc_schema = Arc::new(Mutex::from(schema));
|
||||
let mut tera = tera::Tera::default();
|
||||
let mut ctx = tera::Context::new();
|
||||
if env.info.can_do("templates") {
|
||||
let templates = format!("{}/**/*", &templates_path);
|
||||
tera = match tera::Tera::new(templates.as_str()) {
|
||||
Ok(t) => {
|
||||
println!("WebServices Templates loaded from: {}", &templates_path);
|
||||
t
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error loading Templates {}: {} ", &templates, e);
|
||||
::std::process::exit(1);
|
||||
// tera::Tera::default()
|
||||
}
|
||||
};
|
||||
tera.autoescape_on(vec!["html", ".sql"]);
|
||||
let mut data_hash: HashMap<String, String> = HashMap::new();
|
||||
// let lang = String::from("es");
|
||||
let data_path = format!("{}/defaults.toml", &templates_path);
|
||||
match hash_from_data(&data_path, &mut ctx, &mut data_hash, true) {
|
||||
Ok(_) => {
|
||||
for (key, value) in &data_hash { //.iter() {
|
||||
ctx.insert(key, value);
|
||||
}
|
||||
println!("Default WebServices templates module loaded from: {}", &data_path);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error parsing data {}:{}", &data_path, e);
|
||||
}
|
||||
};
|
||||
AppData::load_modules(&env, &default_module,&mut ctx);
|
||||
}
|
||||
Self {
|
||||
env,
|
||||
tera,
|
||||
ctx,
|
||||
}
|
||||
}
|
||||
/// Load module info for templating if exists
|
||||
pub fn load_modules(env: &AppEnv,target: &str, ctx: &mut tera::Context) {
|
||||
if let Some(module) = env.modules.get(target) {
|
||||
ctx.insert("name",&module.name);
|
||||
ctx.insert("title",&module.title);
|
||||
ctx.insert("key",&module.key);
|
||||
ctx.insert("dflt_lang",&module.dflt_lang);
|
||||
ctx.insert("template_path",&module.template_path);
|
||||
ctx.insert("logo_url",&module.logo_url);
|
||||
ctx.insert("css_url",&module.css_url);
|
||||
ctx.insert("bg_url",&module.bg_url);
|
||||
ctx.insert("color",&module.color);
|
||||
ctx.insert("main_color",&module.main_color);
|
||||
ctx.insert("captcha_color",&module.captcha_color);
|
||||
ctx.insert("main_bg",&module.main_bg);
|
||||
ctx.insert("manifesto",&module.manifesto);
|
||||
}
|
||||
// if let Some(tbl_def) = module.as_table() {
|
||||
// for (ky, val) in tbl_def {
|
||||
// if let Some(value) = val.as_str() {
|
||||
// // Insert into Tera ctx
|
||||
// // println!("ky: [{}] -> val: [{}] ", ky, value);
|
||||
// ctx.insert(ky, value);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
83
app_env/src/appenv.rs
Normal file
83
app_env/src/appenv.rs
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Copyright 2021, Jesús Pérez Lorenzo
|
||||
//
|
||||
use std::collections::HashMap;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
module::Module,
|
||||
appinfo::AppInfo,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
pub struct DataCollUsers {
|
||||
pub fields: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppEnv {
|
||||
pub info: AppInfo,
|
||||
pub config: Config,
|
||||
pub debug_level: String,
|
||||
pub appkey: String,
|
||||
pub checked: bool,
|
||||
pub collections: HashMap<String, toml::Value>,
|
||||
pub modules: HashMap<String, Module>,
|
||||
// pub modules: HashMap<String, toml::Value>,
|
||||
}
|
||||
|
||||
impl Default for AppEnv {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
info: AppInfo::default(),
|
||||
config: Config::default(),
|
||||
debug_level: String::from(""),
|
||||
appkey: String::from(""),
|
||||
checked: true,
|
||||
collections: HashMap::new(),
|
||||
modules: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl AppEnv {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn new(
|
||||
config: Config,
|
||||
debug_level: &str,
|
||||
info: AppInfo,
|
||||
appkey: &str,
|
||||
checked: bool,
|
||||
collections: HashMap<String, toml::Value>,
|
||||
modules: HashMap<String, Module>,
|
||||
// jwt_sign: Option<JwtSignature>,
|
||||
) -> Self {
|
||||
let akey = appkey.to_string();
|
||||
Self {
|
||||
info,
|
||||
config,
|
||||
debug_level: debug_level.to_string(),
|
||||
appkey: akey,
|
||||
checked,
|
||||
collections,
|
||||
modules,
|
||||
// jwt_sign,
|
||||
}
|
||||
}
|
||||
pub async fn set_appkey(&mut self, app_key: &str, key: &str) {
|
||||
if key.is_empty() {
|
||||
self.appkey = app_key.to_string();
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn has_appkey(&self) -> bool {
|
||||
self.appkey.as_str() == ""
|
||||
}
|
||||
pub fn get_module(&self, key_module: &str) -> Module {
|
||||
if let Some(module) = self.modules.get(key_module) {
|
||||
module.to_owned()
|
||||
} else {
|
||||
Module::default()
|
||||
}
|
||||
}
|
||||
}
|
70
app_env/src/appinfo.rs
Normal file
70
app_env/src/appinfo.rs
Normal file
@ -0,0 +1,70 @@
|
||||
//
|
||||
// Copyright 2021, Jesús Pérez Lorenzo
|
||||
//
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
module::Module,
|
||||
AppRunMode,
|
||||
};
|
||||
|
||||
use AppRunMode::{Pro,Premium};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AppInfo {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub author: String,
|
||||
pub about: String,
|
||||
pub usedata: String,
|
||||
pub appmode: AppRunMode,
|
||||
}
|
||||
impl AppInfo {
|
||||
pub async fn new(app_name: &str, version: String, author: String) -> Self {
|
||||
let usedata = match std::fs::read_to_string(format!("{}.use",&envmnt::get_or("APP_HOME", ""))) {
|
||||
Ok(res) => res,
|
||||
// Err(e) => {
|
||||
// println!("Error usedata: {}",e);
|
||||
Err(_) => String::from(""),
|
||||
};
|
||||
Self {
|
||||
name: format!("{} Server",&app_name),
|
||||
version,
|
||||
author,
|
||||
about: format!("{}: Boot app",&app_name),
|
||||
usedata,
|
||||
appmode: AppRunMode::default(),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn can_do(&self, target: &str) -> bool {
|
||||
match target {
|
||||
"encrypt" => matches!(self.appmode, Pro | Premium),
|
||||
"files" => matches!(self.appmode, Pro | Premium),
|
||||
"user_forms" => matches!(self.appmode, Pro | Premium),
|
||||
"templates" => matches!(self.appmode, Pro | Premium),
|
||||
"session_state" => matches!(self.appmode, Pro | Premium),
|
||||
"support" => matches!(self.appmode, Pro | Premium),
|
||||
"update" => matches!(self.appmode, Pro | Premium),
|
||||
"deploy" => matches!(self.appmode, Pro | Premium),
|
||||
"supervisor" => matches!(self.appmode, Premium),
|
||||
_ => {
|
||||
println!("Target can do: {} undefined", &target);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppEnv {
|
||||
pub info: AppInfo,
|
||||
pub config: Config,
|
||||
pub debug_level: String,
|
||||
pub appkey: String,
|
||||
pub checked: bool,
|
||||
pub collections: HashMap<String, toml::Value>,
|
||||
pub modules: HashMap<String, Module>,
|
||||
// pub modules: HashMap<String, toml::Value>,
|
||||
}
|
198
app_env/src/config.rs
Normal file
198
app_env/src/config.rs
Normal file
@ -0,0 +1,198 @@
|
||||
//
|
||||
// Copyright 2021, Jesús Pérez Lorenzo
|
||||
//
|
||||
use serde::{Deserialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct Notifier {
|
||||
pub name: String,
|
||||
pub settings: String,
|
||||
pub command: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct StoreSettings {
|
||||
host: String,
|
||||
port: u32,
|
||||
user: String,
|
||||
pass: String,
|
||||
pub database: String,
|
||||
pub max_conn: u32,
|
||||
}
|
||||
impl StoreSettings {
|
||||
#[must_use]
|
||||
#[allow(unused_variables)]
|
||||
pub fn url(&self, store: &str, key: &str) -> String {
|
||||
let mut user_pass = String::from("");
|
||||
let user: String;
|
||||
let pass: String;
|
||||
user = self.user.to_owned();
|
||||
pass = self.pass.to_owned();
|
||||
if user.as_str() != "" || pass.as_str() != "" {
|
||||
user_pass = format!("{}:{}", &user, &pass);
|
||||
}
|
||||
let host = &self.host;
|
||||
let port = &self.port;
|
||||
let database = &self.database;
|
||||
format!(
|
||||
"{}://{}@{}:{}/{}",
|
||||
store, &user_pass, &host, &port, &database
|
||||
)
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct StoreKeyValue {
|
||||
host: String,
|
||||
port: u32,
|
||||
pub prefix: String,
|
||||
pub max_conn: u32,
|
||||
}
|
||||
impl StoreKeyValue {
|
||||
#[must_use]
|
||||
pub fn url(&self, store: &str) -> String {
|
||||
let host = &self.host;
|
||||
let port = &self.port;
|
||||
format!("{}://{}:{}", store, &host, &port)
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct StoreLocal {
|
||||
pub database: String,
|
||||
}
|
||||
impl StoreLocal {
|
||||
#[must_use]
|
||||
pub fn url(&self, store: &str) -> String {
|
||||
format!("{}.{}", store, &self.database)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct Config {
|
||||
pub run_mode: String,
|
||||
pub resources_path: String,
|
||||
|
||||
pub certs_store_path: String,
|
||||
pub cert_file_sufix: String,
|
||||
|
||||
pub default_module: String,
|
||||
// Some paths
|
||||
pub templates_path: String,
|
||||
pub defaults_path: String,
|
||||
pub dist_path: String,
|
||||
pub html_path: String,
|
||||
pub upload_path: String,
|
||||
/// allow origin for localhost
|
||||
pub allow_origin: Vec<String>,
|
||||
// warp log name
|
||||
pub log_name: String,
|
||||
/// graphql schemas
|
||||
pub gql_schemas_path: String,
|
||||
/// graphql query targets
|
||||
pub gql_targets: String,
|
||||
/// graphql path for requests
|
||||
pub gql_req_path: String,
|
||||
/// graphQL path for request
|
||||
pub giql_req_path: String,
|
||||
pub auth_model_path: String,
|
||||
pub auth_policy_path: String,
|
||||
pub usrs_store: String,
|
||||
pub usrs_store_target: String,
|
||||
pub usrs_shadow_store: String,
|
||||
pub usrs_shadow_target: String,
|
||||
pub main_store: String,
|
||||
pub srv_host: String,
|
||||
pub srv_port: u16,
|
||||
pub srv_protocol: String,
|
||||
pub loop_duration: u64,
|
||||
pub run_cache: bool,
|
||||
pub cache_path: String,
|
||||
pub cache_lock_path: String,
|
||||
pub cache_lock_ext: String,
|
||||
pub run_check: bool,
|
||||
pub check_path: String,
|
||||
pub default_lang: String,
|
||||
pub langs: Vec<String>,
|
||||
pub signup_mode: String,
|
||||
pub password_rules: String,
|
||||
pub mapped_url_prefix: String,
|
||||
pub admin_key: String,
|
||||
pub logs_store: String,
|
||||
pub logs_format: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load_file_content(verbose: &str, cfg_path: &str) -> String {
|
||||
let config_path: String;
|
||||
if cfg_path.is_empty() {
|
||||
config_path = envmnt::get_or("APP_CONFIG_PATH", "config.toml");
|
||||
} else {
|
||||
config_path = cfg_path.to_string();
|
||||
}
|
||||
if verbose != "quiet" {
|
||||
println!("Config path: {}", &config_path);
|
||||
}
|
||||
std::fs::read_to_string(&config_path)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}",e);
|
||||
String::from("")
|
||||
})
|
||||
}
|
||||
pub fn new(content: String,verbose: &str) -> Self {
|
||||
match toml::from_str(&content) {
|
||||
Ok(cfg) => {
|
||||
if verbose != "quiet" {
|
||||
println!("Config Loaded successfully");
|
||||
}
|
||||
let app_home=envmnt::get_or("APP_HOME", "");
|
||||
if app_home.is_empty() {
|
||||
cfg
|
||||
} else {
|
||||
let mut app_cfg = cfg;
|
||||
app_cfg.certs_store_path=format!("{}{}",&app_home,&app_cfg.certs_store_path);
|
||||
app_cfg.resources_path=format!("{}{}",&app_home,&app_cfg.resources_path);
|
||||
app_cfg.templates_path=format!("{}{}",&app_home,&app_cfg.templates_path);
|
||||
app_cfg.defaults_path=format!("{}{}",&app_home,&app_cfg.defaults_path);
|
||||
app_cfg.html_path=format!("{}{}",&app_home,&app_cfg.html_path);
|
||||
app_cfg.dist_path=format!("{}{}",&app_home,&app_cfg.dist_path);
|
||||
app_cfg.upload_path=format!("{}{}",&app_home,&app_cfg.upload_path);
|
||||
app_cfg.auth_model_path=format!("{}{}",&app_home,&app_cfg.auth_model_path);
|
||||
app_cfg.auth_policy_path=format!("{}{}",&app_home,&app_cfg.auth_policy_path);
|
||||
app_cfg.usrs_store_target=format!("{}{}",&app_home,&app_cfg.usrs_store_target);
|
||||
app_cfg.usrs_shadow_target=format!("{}{}",&app_home,&app_cfg.usrs_shadow_target);
|
||||
app_cfg.cache_path=format!("{}{}",&app_home,&app_cfg.cache_path);
|
||||
app_cfg.cache_lock_path=format!("{}{}",&app_home,&app_cfg.cache_lock_path);
|
||||
app_cfg.check_path=format!("{}{}",&app_home,&app_cfg.check_path);
|
||||
app_cfg
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Config error: {}",e);
|
||||
Config::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_html_path(&self) -> &'static str {
|
||||
Box::leak(self.html_path.to_owned().into_boxed_str())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_auth_model_path(&self) -> &'static str {
|
||||
Box::leak(self.auth_model_path.to_owned().into_boxed_str())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_auth_policy_path(&self) -> &'static str {
|
||||
Box::leak(self.auth_policy_path.to_owned().into_boxed_str())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_log_name(&self) -> &'static str {
|
||||
Box::leak(self.log_name.to_owned().into_boxed_str())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_gql_req_path(&self) -> &'static str {
|
||||
Box::leak(self.gql_req_path.to_owned().into_boxed_str())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn st_giql_req_path(&self) -> &'static str {
|
||||
Box::leak(self.giql_req_path.to_owned().into_boxed_str())
|
||||
}
|
||||
}
|
137
app_env/src/lib.rs
Normal file
137
app_env/src/lib.rs
Normal file
@ -0,0 +1,137 @@
|
||||
//
|
||||
// Copyright 2021, Jesús Pérez Lorenzo
|
||||
//
|
||||
use core::fmt;
|
||||
// use futures::lock::Mutex;
|
||||
// use std::sync::Arc;
|
||||
|
||||
// use lazy_static::lazy_static;
|
||||
// use once_cell::sync::Lazy;
|
||||
use std::sync::Arc;
|
||||
// use tokio::sync::Mutex;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
pub mod appdata;
|
||||
pub mod appenv;
|
||||
pub mod appinfo;
|
||||
pub mod config;
|
||||
pub mod module;
|
||||
pub mod profile;
|
||||
|
||||
use crate::appdata::AppData;
|
||||
|
||||
use AppRunMode::*;
|
||||
|
||||
pub type BxDynResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppStore {
|
||||
pub app_data: Arc<RwLock<AppData>>,
|
||||
}
|
||||
|
||||
impl AppStore {
|
||||
pub fn new(app: AppData) -> Self {
|
||||
AppStore {
|
||||
app_data: Arc::new(RwLock::new(app)),
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
/// So we don't have to tackle how different database work, we'll just use
|
||||
/// a simple in-memory DB, a vector synchronized by a mutex.
|
||||
pub type Db = Arc<Mutex<Vec<AppData>>>;
|
||||
|
||||
#[must_use]
|
||||
pub fn blank_db() -> Db {
|
||||
Arc::new(Mutex::new(Vec::new())) // Vec::new()))
|
||||
//Arc::new(Mutex::new(vec!(AppData::new(AppEnv::default())))) // Vec::new()))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn appdata_db(env: AppEnv) -> Db {
|
||||
Arc::new(Mutex::new(vec!(AppData::new(env))))
|
||||
}
|
||||
|
||||
pub static DB: Lazy<Db> = Lazy::new(|| blank_db());
|
||||
|
||||
pub async fn get_db_appdata() -> AppData {
|
||||
let db_appdata = DB.lock().await;
|
||||
db_appdata[0].to_owned()
|
||||
}
|
||||
*/
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
/// `DataStore` options
|
||||
pub enum AppRunMode {
|
||||
Basic, // Free basic mode
|
||||
Pro, // Pro licensed mode
|
||||
Premium, // Premium licensed mode
|
||||
Unknown,
|
||||
}
|
||||
|
||||
pub type OptionAppRunMode = Option<AppRunMode>;
|
||||
|
||||
impl Default for AppRunMode {
|
||||
fn default() -> Self {
|
||||
Unknown
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::pattern_type_mismatch)]
|
||||
impl fmt::Display for AppRunMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Basic => write!(f, "basic"),
|
||||
Pro => write!(f, "pro"),
|
||||
Premium => write!(f, "premium"),
|
||||
Unknown => write!(f, "Unknown"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppRunMode {
|
||||
/// Get `DataStore`from String to enum
|
||||
#[must_use]
|
||||
pub fn get_from(str: &str) -> AppRunMode {
|
||||
match str {
|
||||
"basic" => Basic,
|
||||
"pro" => Pro,
|
||||
"premium" => Premium,
|
||||
_ => Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize)]
|
||||
pub struct DataCollUsers {
|
||||
pub fields: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Zterton {
|
||||
pub protocol: String,
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
}
|
||||
impl Default for Zterton {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
protocol: String::from("http"),
|
||||
host: String::from("127.0.0.1"),
|
||||
port: 8000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Zterton {
|
||||
#[must_use]
|
||||
/// New Zterton definition
|
||||
pub fn new(protocol: String, host: String, port: u16) -> Self {
|
||||
Zterton {
|
||||
protocol,
|
||||
host,
|
||||
port,
|
||||
}
|
||||
}
|
||||
}
|
71
app_env/src/module.rs
Normal file
71
app_env/src/module.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use serde::{Deserialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct Module {
|
||||
pub name: String,
|
||||
pub title: String,
|
||||
pub key: String,
|
||||
pub dflt_lang: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub template_path: String,
|
||||
pub logo_url: String,
|
||||
pub css_url: String,
|
||||
pub bg_url: String,
|
||||
pub color: String,
|
||||
pub main_color: String,
|
||||
pub main_bg: String,
|
||||
pub css_color: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub captcha_color: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub manifesto: String,
|
||||
pub stores: String,
|
||||
#[serde(default = "default_bool")]
|
||||
pub init_load: bool,
|
||||
#[serde(default = "default_empty")]
|
||||
pub store_root: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub store_path: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub store_frmt: String,
|
||||
#[serde(default = "default_empty")]
|
||||
pub upload_path: String,
|
||||
}
|
||||
|
||||
fn default_empty() -> String {
|
||||
"".to_string()
|
||||
}
|
||||
fn default_bool() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub fn load_fs_content(path: &std::path::PathBuf) -> String {
|
||||
std::fs::read_to_string(path)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}",e);
|
||||
String::from("")
|
||||
})
|
||||
}
|
||||
pub fn new(content: String) -> Self {
|
||||
// let modul_def: toml::Value = toml::from_str(&content)?;
|
||||
// if let Some(name) = modul_def["name"].as_str() {
|
||||
match toml::from_str(&content) {
|
||||
Ok(cfg) => {
|
||||
println!("Module Loaded successfully");
|
||||
let app_home=envmnt::get_or("APP_HOME", "");
|
||||
if app_home.is_empty() {
|
||||
cfg
|
||||
} else {
|
||||
let mut module_cfg = cfg;
|
||||
module_cfg.store_root=format!("{}{}",&app_home,&module_cfg.store_root);
|
||||
module_cfg
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Module error: {}",e);
|
||||
Module::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
app_env/src/profile.rs
Normal file
33
app_env/src/profile.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Default)]
|
||||
pub struct Profile {
|
||||
pub name: String,
|
||||
pub key: String,
|
||||
}
|
||||
|
||||
impl Profile {
|
||||
pub fn load_fs_content(str_path: String) -> String {
|
||||
let file_path = std::path::Path::new(&str_path);
|
||||
if ! std::path::Path::new(&file_path).exists() {
|
||||
eprintln!("Path {} not found", &str_path);
|
||||
return String::from("");
|
||||
}
|
||||
std::fs::read_to_string(file_path).unwrap_or_else(|e| {
|
||||
eprintln!("{}", e);
|
||||
String::from("")
|
||||
})
|
||||
}
|
||||
pub fn to_yaml(content: String) -> serde_yaml::Value {
|
||||
match serde_yaml::from_str(&content) {
|
||||
Ok(profile) => {
|
||||
// println!("Profile Loaded successfully");
|
||||
profile
|
||||
}
|
||||
Err(_e) => {
|
||||
// println!("Profile error: {}", e);
|
||||
"".into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user