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
26 changes: 15 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.11.2"
version = "0.12.0"
edition = "2024"
description = "Surfpool is where developers start their Solana journey."
license = "Apache-2.0"
Expand Down Expand Up @@ -96,6 +96,8 @@ ratatui = { version = "0.29.0", features = [
reqwest = { version = "0.12.23", default-features = false }
rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", rev = "ff71a526156e6c9409c450f71eccd6aced9bc339", package = "rmcp" }
rust-embed = "8.2.0"
schemars = { version = "0.8.22" }
schemars_derive = { version = "0.8.22" }
serde = { version = "1.0.226", default-features = false }
serde_derive = { version = "1.0.226", default-features = false } # must match the serde version, see https://github.com/serde-rs/serde/issues/2584#issuecomment-1685252251
serde_json = { version = "1.0.135", default-features = false }
Expand Down Expand Up @@ -160,8 +162,8 @@ surfpool-subgraph = { path = "crates/subgraph", default-features = false }
surfpool-types = { path = "crates/types", default-features = false }

txtx-addon-kit = "0.4.10"
txtx-addon-network-svm = { version = "0.3.16" }
txtx-addon-network-svm-types = { version = "0.3.15" }
txtx-addon-network-svm = { version = "0.3.17" }
txtx-addon-network-svm-types = { version = "0.3.16" }
txtx-cloud = { version = "0.1.13", features = [
"clap",
"toml",
Expand Down
124 changes: 117 additions & 7 deletions crates/cli/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(unused_imports, unused_variables)]
use std::{
collections::HashMap, error::Error as StdError, sync::RwLock, thread::JoinHandle,
collections::HashMap,
error::Error as StdError,
sync::{Arc, RwLock},
thread::JoinHandle,
time::Duration,
};

Expand All @@ -9,7 +12,7 @@ use actix_web::{
App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
dev::ServerHandle,
http::header::{self},
middleware,
middleware, post,
web::{self, Data, route},
};
use convert_case::{Case, Casing};
Expand All @@ -19,6 +22,7 @@ use juniper_graphql_ws::ConnectionConfig;
use log::{debug, error, info, trace, warn};
#[cfg(feature = "explorer")]
use rust_embed::RustEmbed;
use serde::{Deserialize, Serialize};
use surfpool_core::scenarios::TemplateRegistry;
use surfpool_gql::{
DynamicSchema,
Expand All @@ -29,8 +33,8 @@ use surfpool_gql::{
};
use surfpool_studio_ui::serve_studio_static_files;
use surfpool_types::{
DataIndexingCommand, OverrideTemplate, SanitizedConfig, SubgraphCommand, SubgraphEvent,
SurfpoolConfig,
DataIndexingCommand, OverrideTemplate, SanitizedConfig, Scenario, SubgraphCommand,
SubgraphEvent, SurfpoolConfig,
};
use txtx_core::kit::types::types::Value;
use txtx_gql::kit::uuid::Uuid;
Expand Down Expand Up @@ -68,6 +72,7 @@ pub async fn start_subgraph_and_explorer_server(

// Initialize template registry and load templates
let template_registry_wrapped = Data::new(RwLock::new(TemplateRegistry::new()));
let loaded_scenarios = Data::new(RwLock::new(LoadedScenarios::new()));

let subgraph_handle = start_subgraph_runloop(
subgraph_events_tx,
Expand All @@ -86,12 +91,12 @@ pub async fn start_subgraph_and_explorer_server(
.app_data(config_wrapped.clone())
.app_data(collections_metadata_lookup_wrapped.clone())
.app_data(template_registry_wrapped.clone())
.app_data(loaded_scenarios.clone())
.wrap(
Cors::default()
.allow_any_origin()
.allowed_methods(vec!["POST", "GET", "OPTIONS", "DELETE"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.allow_any_method()
.allow_any_header()
.supports_credentials()
.max_age(3600),
)
Expand All @@ -100,6 +105,10 @@ pub async fn start_subgraph_and_explorer_server(
.service(get_config)
.service(get_indexers)
.service(get_scenario_templates)
.service(post_scenarios)
.service(get_scenarios)
.service(delete_scenario)
.service(patch_scenario)
.service(
web::scope("/workspace")
.route("/v1/indexers", web::post().to(post_graphql))
Expand All @@ -109,6 +118,7 @@ pub async fn start_subgraph_and_explorer_server(
);

if enable_studio {
app = app.app_data(Arc::new(RwLock::new(LoadedScenarios::new())));
app = app.service(serve_studio_static_files);
}

Expand Down Expand Up @@ -191,6 +201,106 @@ async fn get_scenario_templates(
.body(response))
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LoadedScenarios {
pub scenarios: Vec<Scenario>,
}
impl LoadedScenarios {
pub fn new() -> Self {
Self {
scenarios: Vec::new(),
}
}
}

#[post("/v1/scenarios")]
async fn post_scenarios(
req: HttpRequest,
scenario: web::Json<Scenario>,
data: Data<RwLock<LoadedScenarios>>,
) -> Result<HttpResponse, Error> {
let mut loaded_scenarios = data
.write()
.map_err(|_| actix_web::error::ErrorInternalServerError("Failed to acquire write lock"))?;
let scenario_data = scenario.into_inner();
let scenario_id = scenario_data.id.clone();
loaded_scenarios.scenarios.push(scenario_data);
let response = serde_json::json!({"id": scenario_id});
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(response.to_string()))
}

#[actix_web::get("/v1/scenarios")]
async fn get_scenarios(data: Data<RwLock<LoadedScenarios>>) -> Result<HttpResponse, Error> {
let loaded_scenarios = data
.read()
.map_err(|_| actix_web::error::ErrorInternalServerError("Failed to acquire read lock"))?;
let response = serde_json::to_string(&loaded_scenarios.scenarios).map_err(|_| {
actix_web::error::ErrorInternalServerError("Failed to serialize loaded scenarios")
})?;

Ok(HttpResponse::Ok()
.content_type("application/json")
.body(response))
}

#[actix_web::delete("/v1/scenarios/{id}")]
async fn delete_scenario(
path: web::Path<String>,
data: Data<RwLock<LoadedScenarios>>,
) -> Result<HttpResponse, Error> {
let scenario_id = path.into_inner();
let mut loaded_scenarios = data
.write()
.map_err(|_| actix_web::error::ErrorInternalServerError("Failed to acquire write lock"))?;

let initial_len = loaded_scenarios.scenarios.len();
loaded_scenarios.scenarios.retain(|s| s.id != scenario_id);

if loaded_scenarios.scenarios.len() == initial_len {
return Ok(
HttpResponse::NotFound().body(format!("Scenario with id '{}' not found", scenario_id))
);
}

Ok(HttpResponse::Ok().body(format!("Scenario '{}' deleted", scenario_id)))
}

#[actix_web::patch("/v1/scenarios/{id}")]
async fn patch_scenario(
path: web::Path<String>,
scenario: web::Json<Scenario>,
data: Data<RwLock<LoadedScenarios>>,
) -> Result<HttpResponse, Error> {
let scenario_id = path.into_inner();
let mut loaded_scenarios = data
.write()
.map_err(|_| actix_web::error::ErrorInternalServerError("Failed to acquire write lock"))?;

let scenario_index = loaded_scenarios
.scenarios
.iter()
.position(|s| s.id == scenario_id);

match scenario_index {
Some(index) => {
loaded_scenarios.scenarios[index] = scenario.into_inner();
let response = serde_json::json!({"id": scenario_id});
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(response.to_string()))
}
None => {
loaded_scenarios.scenarios.push(scenario.into_inner());
let response = serde_json::json!({"id": scenario_id});
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(response.to_string()))
}
}
}

#[allow(dead_code)]
#[cfg(not(feature = "explorer"))]
fn handle_embedded_file(_path: &str) -> HttpResponse {
Expand Down
Loading