Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions sandcrate-backend/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use serde::{Serialize, Deserialize};
use serde::Serialize;
use std::sync::Arc;
use std::fs;
use std::path::Path as FsPath;
Expand Down Expand Up @@ -58,7 +58,7 @@ struct ErrorResponse {
async fn get_plugins(
State(_config): State<Arc<AuthConfig>>,
) -> Json<ApiResponse<PluginList>> {
let plugins_dir = FsPath::new("assets/plugins");
let plugins_dir = FsPath::new("../assets/plugins");
let mut plugins = Vec::new();

if let Ok(entries) = fs::read_dir(plugins_dir) {
Expand Down Expand Up @@ -110,9 +110,10 @@ async fn get_plugins(
}

async fn get_plugin(
State(_config): State<Arc<AuthConfig>>,
Path(plugin_id): Path<String>,
) -> Response {
let plugins_dir = FsPath::new("assets/plugins");
let plugins_dir = FsPath::new("../assets/plugins");
let plugin_path = plugins_dir.join(format!("{}.wasm", plugin_id));

if !plugin_path.exists() {
Expand Down Expand Up @@ -176,13 +177,14 @@ async fn get_plugin(
}

async fn execute_plugin(
State(_config): State<Arc<AuthConfig>>,
Path(plugin_id): Path<String>,
Json(request): Json<PluginExecutionRequest>,
Json(request): Json<serde_json::Value>,
) -> Response {
let start_time = std::time::Instant::now();

// Find the plugin file
let plugins_dir = FsPath::new("assets/plugins");
let plugins_dir = FsPath::new("../assets/plugins");
let plugin_path = plugins_dir.join(format!("{}.wasm", plugin_id));

if !plugin_path.exists() {
Expand All @@ -196,12 +198,16 @@ async fn execute_plugin(
).into_response();
}

// Extract parameters from the request
let parameters = request.get("parameters").cloned();
let timeout = request.get("timeout").and_then(|v| v.as_u64());

// Execute the plugin with parameters and timeout
let plugin_path_str = plugin_path.to_str().unwrap_or("");
let execution_result = plugin::run_plugin_with_params(
plugin_path_str,
request.parameters,
request.timeout
parameters,
timeout
);

let execution_time = start_time.elapsed();
Expand Down Expand Up @@ -249,4 +255,5 @@ pub fn routes() -> Router<Arc<AuthConfig>> {
Router::new()
.route("/plugins", get(get_plugins))
.route("/plugins/:id", get(get_plugin))
.route("/plugins/:id/execute", post(execute_plugin))
}
47 changes: 47 additions & 0 deletions sandcrate-backend/src/bin/execute_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::env;
use std::path::Path;
use sandcrate_backend::plugin;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();

if args.len() != 2 {
println!("Usage: {} <plugin_name>", args[0]);
println!("Example: {} sandcrate-plugin", args[0]);
return Ok(());
}

let plugin_name = &args[1];
let plugin_path = format!("assets/plugins/{}.wasm", plugin_name);

if !Path::new(&plugin_path).exists() {
println!("❌ Plugin '{}' not found at {}", plugin_name, plugin_path);
println!("Available plugins:");

let plugins = plugin::list_plugins();
if plugins.is_empty() {
println!(" No plugins found in assets/plugins directory");
} else {
for plugin in plugins {
println!(" - {}", plugin);
}
}
return Ok(());
}

println!("🚀 Executing plugin: {}", plugin_name);
println!("📁 Path: {}", plugin_path);
println!("---");

match plugin::run_plugin(&plugin_path) {
Ok(result) => {
println!("✅ Plugin executed successfully!");
println!("📋 Result: {}", result);
}
Err(e) => {
println!("❌ Plugin execution failed: {}", e);
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion sandcrate-backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod api;
mod auth;
mod plugin;
pub mod plugin;

use std::net::SocketAddr;
use std::sync::Arc;
Expand Down
Loading
Loading