Skip to main content

greentic_runner_host/runner/
mod.rs

1pub mod adapt_events_email;
2pub mod adapt_timer;
3pub mod contract_cache;
4pub mod contract_introspection;
5pub mod engine;
6pub mod flow_adapter;
7pub mod i18n;
8pub mod invocation;
9pub mod mocks;
10pub mod operator;
11pub mod schema_validator;
12pub mod templating;
13
14use std::net::SocketAddr;
15use std::sync::Arc;
16
17use anyhow::Result;
18use axum::routing::{get, post};
19use axum::{Router, serve};
20use tokio::net::TcpListener;
21
22use crate::http::{self, admin, auth::AdminAuth, health::HealthState};
23use crate::routing::TenantRouting;
24use crate::runtime::ActivePacks;
25use crate::watcher::PackReloadHandle;
26
27pub struct HostServer {
28    addr: SocketAddr,
29    router: Router,
30    _state: ServerState,
31}
32
33impl HostServer {
34    pub fn new(
35        port: u16,
36        active: Arc<ActivePacks>,
37        routing: TenantRouting,
38        health: Arc<HealthState>,
39        reload: Option<PackReloadHandle>,
40        admin: AdminAuth,
41    ) -> Result<Self> {
42        let addr = SocketAddr::from(([0, 0, 0, 0], port));
43        let state = ServerState {
44            active,
45            routing,
46            health,
47            reload,
48            admin,
49        };
50        let router = Router::new()
51            .route("/operator/op/invoke", post(operator::invoke))
52            .route("/healthz", get(http::health::handler))
53            .route("/admin/packs/status", get(admin::status))
54            .route("/admin/packs/reload", post(admin::reload))
55            .with_state(state.clone());
56        Ok(Self {
57            addr,
58            router,
59            _state: state,
60        })
61    }
62
63    pub async fn serve(self) -> Result<()> {
64        tracing::info!(addr = %self.addr, "starting host server");
65        let listener = TcpListener::bind(self.addr).await?;
66        serve(
67            listener,
68            self.router
69                .into_make_service_with_connect_info::<SocketAddr>(),
70        )
71        .await?;
72        Ok(())
73    }
74}
75
76#[derive(Clone)]
77pub struct ServerState {
78    pub active: Arc<ActivePacks>,
79    pub routing: TenantRouting,
80    pub health: Arc<HealthState>,
81    pub reload: Option<PackReloadHandle>,
82    pub admin: AdminAuth,
83}