refactor wip
This commit is contained in:
		@@ -1,4 +1,6 @@
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
use hero_supervisor::Supervisor;
 | 
			
		||||
use crate::{Server, TlsConfigError, ServerConfig};
 | 
			
		||||
 | 
			
		||||
/// ServerBuilder for constructing Server instances with a fluent API
 | 
			
		||||
@@ -12,6 +14,7 @@ pub struct ServerBuilder {
 | 
			
		||||
    tls_port: Option<u16>,
 | 
			
		||||
    enable_auth: bool,
 | 
			
		||||
    enable_webhooks: bool,
 | 
			
		||||
    supervisor: Option<Arc<Supervisor>>,
 | 
			
		||||
 | 
			
		||||
    circles: HashMap<String, Vec<String>>,
 | 
			
		||||
}
 | 
			
		||||
@@ -28,6 +31,7 @@ impl ServerBuilder {
 | 
			
		||||
            tls_port: None,
 | 
			
		||||
            enable_auth: false,
 | 
			
		||||
            enable_webhooks: false,
 | 
			
		||||
            supervisor: None,
 | 
			
		||||
 | 
			
		||||
            circles: HashMap::new(),
 | 
			
		||||
        }
 | 
			
		||||
@@ -76,6 +80,12 @@ impl ServerBuilder {
 | 
			
		||||
        self.circles = circles;
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Set the supervisor instance for job dispatching
 | 
			
		||||
    pub fn with_supervisor(mut self, supervisor: Arc<Supervisor>) -> Self {
 | 
			
		||||
        self.supervisor = Some(supervisor);
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /// Load configuration from a ServerConfig instance
 | 
			
		||||
    pub fn from_config(mut self, config: ServerConfig) -> Self {
 | 
			
		||||
@@ -87,7 +97,6 @@ impl ServerBuilder {
 | 
			
		||||
        self.cert_path = config.cert;
 | 
			
		||||
        self.key_path = config.key;
 | 
			
		||||
        self.tls_port = config.tls_port;
 | 
			
		||||
        self.enable_webhooks = config.webhooks;
 | 
			
		||||
        self.circles = config.circles;
 | 
			
		||||
        self
 | 
			
		||||
    }
 | 
			
		||||
@@ -109,7 +118,7 @@ impl ServerBuilder {
 | 
			
		||||
            circles: self.circles,
 | 
			
		||||
            nonce_store: HashMap::new(),
 | 
			
		||||
            authenticated_pubkey: None,
 | 
			
		||||
            supervisor: None,
 | 
			
		||||
            supervisor: self.supervisor,
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -34,10 +34,6 @@ pub struct ServerConfig {
 | 
			
		||||
    /// Separate port for TLS connections
 | 
			
		||||
    pub tls_port: Option<u16>,
 | 
			
		||||
    
 | 
			
		||||
    /// Enable webhook handling
 | 
			
		||||
    #[serde(default)]
 | 
			
		||||
    pub webhooks: bool,
 | 
			
		||||
    
 | 
			
		||||
    /// Circles configuration - maps circle names to lists of member public keys
 | 
			
		||||
    #[serde(default)]
 | 
			
		||||
    pub circles: HashMap<String, Vec<String>>,
 | 
			
		||||
@@ -54,7 +50,6 @@ impl Default for ServerConfig {
 | 
			
		||||
            cert: None,
 | 
			
		||||
            key: None,
 | 
			
		||||
            tls_port: None,
 | 
			
		||||
            webhooks: false,
 | 
			
		||||
            circles: HashMap::new(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -123,7 +118,6 @@ impl ServerConfig {
 | 
			
		||||
            cert: Some("cert.pem".to_string()),
 | 
			
		||||
            key: Some("key.pem".to_string()),
 | 
			
		||||
            tls_port: Some(8444),
 | 
			
		||||
            webhooks: false,
 | 
			
		||||
            circles,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
use crate::Server;
 | 
			
		||||
use actix::prelude::*;
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
use hero_supervisor::{Supervisor, ScriptType};
 | 
			
		||||
use hero_supervisor::ScriptType;
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
@@ -426,7 +426,7 @@ impl Server {
 | 
			
		||||
            supervisor
 | 
			
		||||
                .new_job()
 | 
			
		||||
                .context_id(&circle_pk)
 | 
			
		||||
                .script_type(ScriptType::RhaiSAL)
 | 
			
		||||
                .script_type(ScriptType::SAL)
 | 
			
		||||
                .script(&script_content)
 | 
			
		||||
                .timeout(TASK_TIMEOUT_DURATION)
 | 
			
		||||
                .await_response()
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,7 @@ use serde_json::Value; // Removed unused json
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::fs::File;
 | 
			
		||||
use std::io::BufReader;
 | 
			
		||||
use std::sync::Arc;
 | 
			
		||||
use std::sync::Mutex; // Removed unused Arc
 | 
			
		||||
use std::time::{SystemTime, UNIX_EPOCH};
 | 
			
		||||
use tokio::task::JoinHandle;
 | 
			
		||||
@@ -211,7 +212,7 @@ pub struct Server {
 | 
			
		||||
    pub circles: HashMap<String, Vec<String>>,
 | 
			
		||||
    nonce_store: HashMap<String, NonceResponse>,
 | 
			
		||||
    authenticated_pubkey: Option<String>,
 | 
			
		||||
    pub supervisor: Option<Supervisor>,
 | 
			
		||||
    pub supervisor: Option<Arc<Supervisor>>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Server {
 | 
			
		||||
@@ -240,7 +241,7 @@ impl Server {
 | 
			
		||||
        let server_config_data = web::Data::new(self.clone());
 | 
			
		||||
    
 | 
			
		||||
        let http_server = HttpServer::new(move || {
 | 
			
		||||
            let mut app = App::new()
 | 
			
		||||
            let app = App::new()
 | 
			
		||||
                .app_data(server_config_data.clone())
 | 
			
		||||
                .route("/{circle_pk}", web::get().to(ws_handler));
 | 
			
		||||
            
 | 
			
		||||
@@ -351,21 +352,6 @@ impl Server {
 | 
			
		||||
        client_rpc_id: Value,
 | 
			
		||||
        ctx: &mut ws::WebsocketContext<Self>,
 | 
			
		||||
    ) {
 | 
			
		||||
        if !self.enable_auth {
 | 
			
		||||
            let err_resp = JsonRpcResponse {
 | 
			
		||||
                jsonrpc: "2.0".to_string(),
 | 
			
		||||
                result: None,
 | 
			
		||||
                error: Some(JsonRpcError {
 | 
			
		||||
                    code: -32000,
 | 
			
		||||
                    message: "Authentication is disabled on this server.".to_string(),
 | 
			
		||||
                    data: None,
 | 
			
		||||
                }),
 | 
			
		||||
                id: client_rpc_id,
 | 
			
		||||
            };
 | 
			
		||||
            ctx.text(serde_json::to_string(&err_resp).unwrap());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        match serde_json::from_value::<AuthCredentials>(params) {
 | 
			
		||||
            Ok(auth_params) => {
 | 
			
		||||
                let nonce_response = self.nonce_store.get(&auth_params.pubkey);
 | 
			
		||||
@@ -550,23 +536,40 @@ impl Server {
 | 
			
		||||
                let public_key = self.authenticated_pubkey.clone();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                let supervisor_ref = self.supervisor.clone();
 | 
			
		||||
                let fut = async move {
 | 
			
		||||
                    let caller_id = public_key.unwrap_or_else(|| "anonymous".to_string());
 | 
			
		||||
                    match SupervisorBuilder::new()
 | 
			
		||||
                        .redis_url(&redis_url_clone)
 | 
			
		||||
                        .caller_id(&caller_id)
 | 
			
		||||
                        .build() {
 | 
			
		||||
                        Ok(hero_supervisor) => {
 | 
			
		||||
                            hero_supervisor
 | 
			
		||||
                    let _caller_id = public_key.unwrap_or_else(|| "anonymous".to_string());
 | 
			
		||||
                    
 | 
			
		||||
                    // Use the passed supervisor if available, otherwise create a new one
 | 
			
		||||
                    match supervisor_ref {
 | 
			
		||||
                        Some(supervisor) => {
 | 
			
		||||
                            supervisor
 | 
			
		||||
                                .new_job()
 | 
			
		||||
                                    .context_id(&circle_pk_clone)
 | 
			
		||||
                                    .script_type(hero_supervisor::ScriptType::RhaiSAL)
 | 
			
		||||
                                    .script_type(hero_supervisor::ScriptType::SAL)
 | 
			
		||||
                                    .script(&script_content)
 | 
			
		||||
                                    .timeout(TASK_TIMEOUT_DURATION)
 | 
			
		||||
                                    .await_response()
 | 
			
		||||
                                    .await
 | 
			
		||||
                        }
 | 
			
		||||
                        Err(e) => Err(e),
 | 
			
		||||
                        None => {
 | 
			
		||||
                            // Fallback: create a new supervisor if none was provided
 | 
			
		||||
                            match SupervisorBuilder::new()
 | 
			
		||||
                                .redis_url(&redis_url_clone)
 | 
			
		||||
                                .build().await {
 | 
			
		||||
                                Ok(hero_supervisor) => {
 | 
			
		||||
                                    hero_supervisor
 | 
			
		||||
                                        .new_job()
 | 
			
		||||
                                            .context_id(&circle_pk_clone)
 | 
			
		||||
                                            .script_type(hero_supervisor::ScriptType::SAL)
 | 
			
		||||
                                            .script(&script_content)
 | 
			
		||||
                                            .timeout(TASK_TIMEOUT_DURATION)
 | 
			
		||||
                                            .await_response()
 | 
			
		||||
                                            .await
 | 
			
		||||
                                }
 | 
			
		||||
                                Err(e) => Err(e),
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user