pub mod assets;
pub mod config;
pub mod db;
pub mod guard;
pub mod handlers;
pub mod routes;
pub use config::StudioConfig;
pub use routes::create_router;
use std::sync::Arc;
use handlers::AppState;
#[must_use]
pub fn looks_like_production_url(url: &str) -> bool {
let lower = url.to_lowercase();
lower.contains("prod")
|| lower.contains("production")
|| lower.contains("aws.neon.tech")
|| lower.contains("turso.io")
}
#[must_use]
pub fn is_loopback_host(host: &str) -> bool {
let host = host.trim().trim_start_matches('[').trim_end_matches(']');
match host.parse::<std::net::IpAddr>() {
Ok(ip) => ip.is_loopback(),
Err(_) => host.eq_ignore_ascii_case("localhost"),
}
}
pub async fn run_studio(
config: StudioConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let schema_str = std::fs::read_to_string(&config.schema_path)?;
let schema_name = config.schema_path.to_string_lossy();
let schema = ruprizzle_parser::parse(&schema_name, &schema_str)
.map_err(|e| format!("Failed to parse schema: {e}"))?;
let db_url = config.database_url.as_deref().unwrap_or("");
if looks_like_production_url(db_url) && !config.yes_i_know {
return Err(
"The database URL contains `prod`, `aws.neon.tech` or `turso.io`, which usually means production. This is a name check, not a real safeguard — it misses any production database not named for the fact. Pass --yes-i-know to proceed."
.into(),
);
}
if !is_loopback_host(&config.host) && config.allow_writes && !config.yes_i_know {
return Err(format!(
"Refusing to bind Studio to {} with --allow-writes. Studio serves plain HTTP, so its session token crosses the network in cleartext and anyone who sees it can use the INSERT, UPDATE and DELETE routes on port {}. Drop --allow-writes, bind to 127.0.0.1, or pass --yes-i-know if the network is genuinely trusted.",
config.host, config.port
)
.into());
}
let pool = if db_url.is_empty() {
None
} else {
Some(
ruprizzle::connect(db_url)
.await
.map_err(|e| format!("Failed to connect to the database: {e}"))?,
)
};
let state = Arc::new(AppState::new(schema, config.clone(), pool));
let app = create_router(Arc::clone(&state));
let bind_addr = format!("{}:{}", config.host, config.port);
let listener = tokio::net::TcpListener::bind(&bind_addr).await?;
let url = format!("http://{bind_addr}/studio?token={}", state.session_token);
println!("⚡ Ruprizzle Studio running at: {url}");
println!(" Every request needs this token; the link above sets it as a cookie.");
if config.allow_writes {
println!(" Mode: read-write (--allow-writes)");
} else {
println!(" Mode: read-only (mutations disabled)");
}
if !is_loopback_host(&config.host) {
println!(
" ⚠ Bound to {} — Studio serves plain HTTP, so the token crosses the network in cleartext on port {}.",
config.host, config.port
);
}
if !config.no_browser {
let _ = opener::open_browser(&url);
}
axum::serve(listener, app).await?;
Ok(())
}