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::server::HttpServer;
11
12pub struct HttpPlugin {
13    config: HttpConfig,
14    active_connections: Arc<AtomicU64>,
15}
16
17impl HttpPlugin {
18    pub fn new(config: HttpConfig) -> Self {
19        Self {
20            config,
21            active_connections: Arc::new(AtomicU64::new(0)),
22        }
23    }
24}
25
26#[async_trait]
27impl ServerPlugin for HttpPlugin {
28    fn name(&self) -> &'static str {
29        "http"
30    }
31
32    async fn run(&self, ctx: PluginContext) -> Result<()> {
33        let server = HttpServer::new(
34            self.config.clone(),
35            ctx.executor.clone(),
36            self.active_connections.clone(),
37        );
38        info!(listen = %self.config.listen, "http plugin listening");
39        server.run(ctx.shutdown).await
40    }
41
42    fn rpc_methods(&self) -> Vec<RpcMethodDef> {
43        vec![RpcMethodDef::new(
44            "http.connections",
45            "current active connection count",
46        )]
47    }
48}