Skip to main content

execgo_runtime/
server.rs

1use axum::{
2    extract::{Path, State},
3    response::IntoResponse,
4    routing::{get, post},
5    Json, Router,
6};
7
8use crate::{
9    error::AppError,
10    runtime::RuntimeService,
11    types::{HealthResponse, SubmitTaskRequest},
12};
13
14pub fn build_router(service: RuntimeService) -> Router {
15    Router::new()
16        .route("/api/v1/tasks", post(create_task))
17        .route("/api/v1/tasks/:id", get(get_task))
18        .route("/api/v1/tasks/:id/kill", post(kill_task))
19        .route("/api/v1/tasks/:id/events", get(get_events))
20        .route("/api/v1/runtime/info", get(runtime_info))
21        .route("/api/v1/runtime/capabilities", get(runtime_capabilities))
22        .route("/api/v1/runtime/config", get(runtime_config))
23        .route("/api/v1/runtime/resources", get(runtime_resources))
24        .route("/healthz", get(healthz))
25        .route("/readyz", get(readyz))
26        .route("/metrics", get(metrics))
27        .with_state(service)
28}
29
30async fn create_task(
31    State(service): State<RuntimeService>,
32    Json(payload): Json<SubmitTaskRequest>,
33) -> Result<impl IntoResponse, AppError> {
34    let response = service.submit_task(payload).await?;
35    Ok(Json(response))
36}
37
38async fn get_task(
39    State(service): State<RuntimeService>,
40    Path(task_id): Path<String>,
41) -> Result<impl IntoResponse, AppError> {
42    let response = service.get_task_status(&task_id).await?;
43    Ok(Json(response))
44}
45
46async fn kill_task(
47    State(service): State<RuntimeService>,
48    Path(task_id): Path<String>,
49) -> Result<impl IntoResponse, AppError> {
50    let response = service.kill_task(&task_id).await?;
51    Ok(Json(response))
52}
53
54async fn get_events(
55    State(service): State<RuntimeService>,
56    Path(task_id): Path<String>,
57) -> Result<impl IntoResponse, AppError> {
58    let response = service.get_events(&task_id).await?;
59    Ok(Json(response))
60}
61
62async fn healthz() -> Json<HealthResponse> {
63    Json(HealthResponse {
64        status: "ok",
65        version: env!("CARGO_PKG_VERSION"),
66    })
67}
68
69async fn runtime_info(
70    State(service): State<RuntimeService>,
71) -> Result<impl IntoResponse, AppError> {
72    Ok(Json(service.runtime_info().await))
73}
74
75async fn runtime_capabilities(
76    State(service): State<RuntimeService>,
77) -> Result<impl IntoResponse, AppError> {
78    Ok(Json(service.runtime_capabilities().await))
79}
80
81async fn runtime_config(
82    State(service): State<RuntimeService>,
83) -> Result<impl IntoResponse, AppError> {
84    Ok(Json(service.runtime_config().await))
85}
86
87async fn runtime_resources(
88    State(service): State<RuntimeService>,
89) -> Result<impl IntoResponse, AppError> {
90    Ok(Json(service.runtime_resources().await?))
91}
92
93async fn readyz(State(service): State<RuntimeService>) -> Result<impl IntoResponse, AppError> {
94    service.ready().await?;
95    Ok(Json(HealthResponse {
96        status: "ready",
97        version: env!("CARGO_PKG_VERSION"),
98    }))
99}
100
101async fn metrics(State(service): State<RuntimeService>) -> Result<impl IntoResponse, AppError> {
102    Ok(service.metrics().await)
103}