Wire up the API routes and start playing with those.

Nothing actually _running_ yet of course.
This commit is contained in:
R Tyler Croy 2021-07-05 13:02:09 -07:00
parent 6af9c32de4
commit 57805bdb01
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
4 changed files with 37 additions and 0 deletions

View File

@ -22,6 +22,8 @@ pub mod v1 {
description: Option<String>,
/// The trigger for executing the project
trigger: TriggerType,
/// Optional inline script, otherwise Geoffrey will look for a file in the repo
script: Option<String>,
}
impl Project {

View File

@ -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()

33
src/routes/api.rs Normal file
View File

@ -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<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())
}

View File

@ -1,3 +1,4 @@
pub mod api;
/**
* This module contains all the re-exported routes for different aspects of Geoffrey
*/