From 57805bdb01d04487d84dae596ea4e8a41a63de1d Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Mon, 5 Jul 2021 13:02:09 -0700 Subject: [PATCH] Wire up the API routes and start playing with those. Nothing actually _running_ yet of course. --- src/dao.rs | 2 ++ src/main.rs | 1 + src/routes/api.rs | 33 +++++++++++++++++++++++++++++++++ src/routes/mod.rs | 1 + 4 files changed, 37 insertions(+) create mode 100644 src/routes/api.rs diff --git a/src/dao.rs b/src/dao.rs index 378cdb6..86c4158 100644 --- a/src/dao.rs +++ b/src/dao.rs @@ -22,6 +22,8 @@ pub mod v1 { description: Option, /// The trigger for executing the project trigger: TriggerType, + /// Optional inline script, otherwise Geoffrey will look for a file in the repo + script: Option, } impl Project { diff --git a/src/main.rs b/src/main.rs index c18d5bc..9ed84a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ async fn main() -> Result<(), std::io::Error> { let state = AppState::new(); let mut app = tide::with_state(state); routes::views::register(&mut app); + routes::api::register(&mut app); if let Some(fd) = std::env::var("LISTEN_FD") .ok() diff --git a/src/routes/api.rs b/src/routes/api.rs new file mode 100644 index 0000000..96b8322 --- /dev/null +++ b/src/routes/api.rs @@ -0,0 +1,33 @@ +/** + * 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, +} + +pub fn register(app: &mut Server>) { + 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>) -> 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()) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 50ca83a..eb960bb 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,3 +1,4 @@ +pub mod api; /** * This module contains all the re-exported routes for different aspects of Geoffrey */