Crates.io | artisan_middleware |
lib.rs | artisan_middleware |
version | 4.0.0 |
source | src |
created_at | 2024-10-01 06:48:01.05732 |
updated_at | 2024-11-30 18:18:12.222856 |
description | The main services of the artisan platform to allow communication and management of linux system services |
homepage | https://docs.artisanhosting.net |
repository | |
max_upload_size | |
id | 1392543 |
size | 172,187 |
artisan_middleware
)artisan_middleware
is a Rust-based library designed to provide essential middleware components for managing Git repositories, process control, state persistence, and more. This library is modular and reusable, allowing you to build applications that involve version control, monitoring, automation, and process management.
The primary goal of artisan_middleware
is to simplify the development of applications that need to interact with Git, manage background processes, or maintain an application state across sessions.
Add the library to your Cargo.toml
:
[dependencies]
artisan_middleware = { path = "../artisan_middleware" }
Replace the path
with the correct relative or absolute path to the library.
config.rs
Handles the loading of configuration settings from Settings.toml
. This configuration file includes information such as Git credentials paths, polling intervals, and other application-specific settings.
use artisan_middleware::config::AppConfig;
let config = AppConfig::new()?;
println!("Git credentials file: {}", config.git.credentials_file);
git_actions.rs
Provides utilities for interacting with Git repositories. This includes actions like cloning, pulling, and fetching updates.
GitAuth
: Represents Git repository credentials.GitAction
: Enum representing different Git actions (Clone, Pull, Push, etc.).GitServer
: Enum representing different Git servers (GitHub, GitLab, Custom).use artisan_middleware::git_actions::{GitAction, GitServer};
let git_action = GitAction::Clone {
repo_name: "my_repo".into(),
repo_owner: "my_user".into(),
destination: "path/to/clone".into(),
repo_branch: "main".into(),
server: GitServer::GitHub,
};
git_action.execute().await?;
state_persistence.rs
Handles saving and loading the application's state. This is useful for keeping track of the last commit hash or other runtime data.
AppState
: Struct to define the application state.StatePersistence
: Utility to save and load state to/from a file.use artisan_middleware::state_persistence::{AppState, StatePersistence};
use std::path::Path;
let app_state = AppState {
last_commit_hashes: HashMap::new(),
};
StatePersistence::save_state(&app_state, Path::new("app_state.enc"))?;
process_manager.rs
Provides functions to manage background processes, such as starting, stopping, and restarting.
spawn_process
: Starts a new process.kill_process
: Attempts to gracefully terminate a process, with the option to force kill if necessary.use artisan_middleware::process_manager::ProcessManager;
let child = ProcessManager::spawn_process("some_command", &["arg1", "arg2"])?;
ProcessManager::kill_process(child.id())?;
encryption.rs
Provides functions to encrypt and decrypt sensitive data. This is useful for handling sensitive credentials securely.
use artisan_middleware::encryption::{encrypt_text, decrypt_text};
let encrypted = encrypt_text("my_secret")?;
let decrypted = decrypt_text(encrypted)?;
notifications.rs
(For Future Implementation) This module is intended to handle notifications, such as sending an email when an event occurs (e.g., a new commit is detected).
Configuration:
Settings.toml
file to include all necessary settings such as Git credentials and polling intervals.[git]
credentials_file = "/path/to/artisan_middleware.cf"
[polling]
interval_seconds = 300 # Poll every 5 minutes.
[app_specific]
custom_value = "my_custom_setting"
max_retries = 5
Initialize the Application:
GitAction
to perform Git operations.State Management:
StatePersistence
to maintain the state of your application.Example Application Flow:
use artisan_middleware::config::AppConfig;
use artisan_middleware::git_actions::{GitAction, GitServer};
use artisan_middleware::state_persistence::{AppState, StatePersistence};
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load Configuration
let config = AppConfig::new()?;
// Load Application State
let state_path = Path::new("app_state.enc");
let mut app_state = StatePersistence::load_state(state_path)?;
// Perform Git Action
let git_action = GitAction::Clone {
repo_name: "my_repo".into(),
repo_owner: "my_user".into(),
destination: "/path/to/clone".into(),
repo_branch: "main".into(),
server: GitServer::GitHub,
};
git_action.execute().await?;
// Save Updated State
app_state.last_commit_hashes.insert("my_repo".into(), "new_commit_hash".into());
StatePersistence::save_state(&app_state, state_path)?;
Ok(())
}
This project is licensed under the MIT License.
For any questions or issues, feel free to open an issue on the repository.