Skip to main content

folk_plugin_http/
plugin.rs

1use std::sync::Arc;
2use std::sync::atomic::AtomicU64;
3
4use anyhow::Result;
5use async_trait::async_trait;
6use folk_api::{PluginContext, RpcMethodDef, ServerPlugin};
7use tracing::info;
8
9use crate::config::HttpConfig;
10use crate::hooks::HookEngine;
11use crate::server::HttpServer;
12
13pub struct HttpPlugin {
14    config: HttpConfig,
15    active_connections: Arc<AtomicU64>,
16}
17
18impl HttpPlugin {
19    pub fn new(config: HttpConfig) -> Self {
20        Self {
21            config,
22            active_connections: Arc::new(AtomicU64::new(0)),
23        }
24    }
25}
26
27#[async_trait]
28impl ServerPlugin for HttpPlugin {
29    fn name(&self) -> &'static str {
30        "http"
31    }
32
33    async fn run(&self, ctx: PluginContext) -> Result<()> {
34        let hook_engine = if self.config.hooks.is_empty() {
35            None
36        } else {
37            Some(Arc::new(HookEngine::new(&self.config.hooks)))
38        };
39
40        let server = HttpServer::new(
41            self.config.clone(),
42            ctx.executor.clone(),
43            self.active_connections.clone(),
44            hook_engine,
45        );
46        info!(listen = %self.config.listen, "http plugin listening");
47        server.run(ctx.shutdown).await
48    }
49
50    fn rpc_methods(&self) -> Vec<RpcMethodDef> {
51        vec![RpcMethodDef::new(
52            "http.connections",
53            "current active connection count",
54        )]
55    }
56}