geoffrey/src/routes/api.rs

34 lines
887 B
Rust

/**
* API routes
*/
use crate::state::*;
use log::*;
use serde::Deserialize;
use tide::{Request, Server};
#[derive(Clone, Debug, Deserialize)]
struct ApiParams {
#[serde(rename = "return")]
redirect: Option<String>,
}
pub fn register(app: &mut Server<AppState<'static>>) {
let mut api = tide::with_state(app.state().clone());
api.at("project/:slug/run").get(run_project);
app.at("/api").nest(api);
}
async fn run_project(req: Request<AppState<'static>>) -> tide::Result {
debug!("Running project: {:?}", req.param("slug"));
let qs: ApiParams = req.query()?;
if let Some(redirect) = &qs.redirect {
info!("Redirecting back to {}", redirect);
if !redirect.starts_with("/") {
warn!("Unsafe redirect, ignoring: {:?}", redirect);
}
return Ok(tide::Redirect::new(&redirect).into());
}
Ok("Run".into())
}