folk-plugin-http 0.2.1

HTTP plugin for Folk — accepts connections via hyper and dispatches to PHP workers
Documentation
use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use anyhow::Result;
use async_trait::async_trait;
use folk_api::{PluginContext, RpcMethodDef, ServerPlugin};
use tracing::info;

use crate::config::HttpConfig;
use crate::server::HttpServer;

pub struct HttpPlugin {
    config: HttpConfig,
    active_connections: Arc<AtomicU64>,
}

impl HttpPlugin {
    pub fn new(config: HttpConfig) -> Self {
        Self {
            config,
            active_connections: Arc::new(AtomicU64::new(0)),
        }
    }
}

#[async_trait]
impl ServerPlugin for HttpPlugin {
    fn name(&self) -> &'static str {
        "http"
    }

    async fn run(&self, ctx: PluginContext) -> Result<()> {
        let server = HttpServer::new(
            self.config.clone(),
            ctx.executor.clone(),
            self.active_connections.clone(),
        );
        info!(listen = %self.config.listen, "http plugin listening");
        server.run(ctx.shutdown).await
    }

    fn rpc_methods(&self) -> Vec<RpcMethodDef> {
        vec![RpcMethodDef::new(
            "http.connections",
            "current active connection count",
        )]
    }
}