use axum::extract::{Path, State};
use axum::http::HeaderMap;
use axum::response::Response;
use axum::Json;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::{blocking, ok_seq, require_admin, Shared};
use crate::scripting::{self, HookDoc, HookResult, ScriptFile, HOOKS};
use crate::Error;
#[derive(Debug, Serialize)]
pub struct Listing {
pub dir: String,
pub files: Vec<ScriptFile>,
pub hooks: &'static [HookDoc],
}
pub async fn list(State(db): State<Shared>, headers: HeaderMap) -> Result<Json<Listing>, Error> {
require_admin(&db, &headers)?;
Ok(Json(Listing {
dir: db.scripts().dir().display().to_string(),
files: db.scripts().list(),
hooks: HOOKS,
}))
}
#[derive(Debug, Serialize)]
pub struct FileText {
pub path: String,
pub text: String,
}
pub async fn read(
State(db): State<Shared>,
headers: HeaderMap,
Path(path): Path<String>,
) -> Result<Json<FileText>, Error> {
require_admin(&db, &headers)?;
let text = db
.scripts()
.source(&path)?
.ok_or_else(|| Error::not_found("script"))?;
Ok(Json(FileText { path, text: text.to_string() }))
}
pub async fn write(
State(db): State<Shared>,
headers: HeaderMap,
Path(path): Path<String>,
body: String,
) -> Result<Response, Error> {
require_admin(&db, &headers)?;
editor_allowed(&db)?;
db.scripts().write(&path, &body)?;
db.record("script-write", script_entity(&path), format!("{} bytes", body.len()));
Ok(ok_seq(&db))
}
pub async fn remove(
State(db): State<Shared>,
headers: HeaderMap,
Path(path): Path<String>,
) -> Result<Response, Error> {
require_admin(&db, &headers)?;
editor_allowed(&db)?;
db.scripts().delete(&path)?;
db.record("script-delete", script_entity(&path), "");
Ok(ok_seq(&db))
}
#[derive(Debug, Deserialize)]
pub struct RunRequest {
pub path: String,
#[serde(default)]
pub source: Option<String>,
pub function: String,
#[serde(default)]
pub input: Value,
}
#[derive(Debug, Serialize)]
pub struct RunResult {
pub ok: bool,
pub value: Value,
pub error: Option<String>,
pub logs: Vec<String>,
pub millis: u128,
}
pub async fn run(
State(db): State<Shared>,
headers: HeaderMap,
Json(request): Json<RunRequest>,
) -> Result<Json<RunResult>, Error> {
let user = require_admin(&db, &headers)?;
editor_allowed(&db)?;
let function = request.function.trim().to_string();
if function.is_empty() || !function.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$') {
return Err(Error::bad_request("name the function to run"));
}
db.record(
"script-run",
script_entity(&request.path),
format!(
"{}() {}",
function,
if request.source.is_some() { "from the editor's text" } else { "from the saved file" }
),
);
let result = blocking(move || {
let started = std::time::Instant::now();
let outcome = match request.source {
Some(source) => {
db.scripts().resolve(&request.path)?;
scripting::run_source(&db, &user, &source, &request.path, &function, &request.input)
}
None => scripting::run(&db, &user, &request.path, &function, &request.input)?,
};
let millis = started.elapsed().as_millis();
Ok(match outcome {
HookResult::Absent => {
return Err(Error::not_found(format!("script '{}'", request.path)));
}
HookResult::Returned(success) => RunResult {
ok: true,
value: success.value,
error: None,
logs: success.logs,
millis,
},
HookResult::Refused(failure) => RunResult {
ok: false,
value: Value::Null,
error: Some(failure.message),
logs: failure.logs,
millis,
},
})
})
.await?;
Ok(Json(result))
}
fn editor_allowed(db: &crate::db::Db) -> Result<(), Error> {
if db.script_editor_allowed() {
return Ok(());
}
Err(Error::forbidden(if db.security().config.lock_script_editor {
"the in-browser script editor is locked off on this server — edit the scripts directory on the host"
} else {
"the in-browser script editor is turned off in Settings — edit the scripts directory on the host, or turn it back on"
}))
}
fn script_entity(path: &str) -> crate::model::EntityRef {
crate::model::EntityRef { kind: "script".into(), id: path.to_string(), label: path.to_string(), part_id: String::new() }
}