Add bridge code to the project

This commit is contained in:
2026-03-03 10:29:49 -05:00
parent 0b9b1c38d6
commit 2e779df78a
6 changed files with 2088 additions and 0 deletions

71
src/main.rs Normal file
View File

@@ -0,0 +1,71 @@
use axum::{Router, response::IntoResponse, routing::get};
use reqwest::StatusCode;
use serde_json::json;
use std::env;
// Define an asynchronous main function with the tokio runtime macro
#[tokio::main]
async fn main() {
// Build our application with one route: a GET request to "/"
let app = Router::new().route("/", get(trigger_runner));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
// Handler that returns a simple string
async fn trigger_runner() -> impl IntoResponse {
let token = match env::var("RAILWAY_TOKEN") {
Ok(t) => t,
Err(_) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
"Missing Railway project token",
);
}
};
let service_id = match env::var("RUNNER_SERVICE_ID") {
Ok(t) => t,
Err(_) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
"Missing Runner Service ID",
);
}
};
let env_id = match env::var("RAILWAY_ENVIRONMENT_ID") {
Ok(id) => id,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Missing Railway env ID"),
};
let client = reqwest::Client::new();
let query = json!({
"query": "mutation Wake($sid: String!, $eid: String!) { serviceInstanceRedeploy(serviceId: $sid, environmentId: $env) }",
"variables": {
"sid": service_id,
"eid": env_id
}
});
let res = client
.post("https://backboard.railway.com/graphql/v2")
.header("Project-Access-Token", token)
.json(&query)
.send()
.await;
match res {
Ok(res) if res.status().is_success() => (StatusCode::OK, "Runner is waking up"),
Ok(res) => {
let err_test = res.text().await.unwrap_or_default();
println!("Railway API error: {}", err_test);
(StatusCode::BAD_GATEWAY, "Railway API rejected the request")
}
Err(_) => (
StatusCode::SERVICE_UNAVAILABLE,
"Could not connect to Railway",
),
}
}