Skip to main content

GatewayServer

Struct GatewayServer 

Source
pub struct GatewayServer { /* private fields */ }
Expand description

HTTP gateway server with bearer-auth, rate limiting, and body-size enforcement.

Build the server with GatewayServer::new, apply optional configuration via the builder methods, then drive it with GatewayServer::serve.

§Defaults

SettingDefault
Bearer authdisabled (open)
Rate limit120 requests / 60 s per IP
Max body size1 MiB (1 048 576 bytes)

§Example

use tokio::sync::{mpsc, watch};
use zeph_gateway::GatewayServer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, _rx) = mpsc::channel::<String>(64);
    let (_stx, srx) = watch::channel(false);

    GatewayServer::new("127.0.0.1", 9000, tx, srx)
        .with_auth(Some("hunter2".into()))
        .with_rate_limit(30)
        .with_max_body_size(512 * 1024)
        .serve()
        .await?;

    Ok(())
}

Implementations§

Source§

impl GatewayServer

Source

pub fn new( bind: &str, port: u16, webhook_tx: Sender<String>, shutdown_rx: Receiver<bool>, ) -> Self

Create a new gateway server.

bind is parsed as an IP address string (e.g. "127.0.0.1" or "0.0.0.0"). If parsing fails, the server falls back to 127.0.0.1:<port> and emits a warning.

webhook_tx receives every valid, sanitised webhook message as a formatted "[sender@channel] body" string.

shutdown_rx is a watch::Receiver<bool> that signals graceful shutdown when its value transitions to true. Sending true causes the server to stop accepting new connections and drain in-flight requests.

§Panics

Does not panic. Invalid bind values fall back to 127.0.0.1 with a log warning.

Source

pub fn with_auth(self, token: Option<String>) -> Self

Set the bearer token required on POST /webhook requests.

When token is Some, every request to /webhook must carry an Authorization: Bearer <token> header. The comparison is performed in constant time (BLAKE3 + subtle::ConstantTimeEq) to prevent timing-oracle attacks.

When token is None, bearer authentication is disabled and a warning is logged at startup.

§Example
use tokio::sync::{mpsc, watch};
use zeph_gateway::GatewayServer;

let (tx, _rx) = mpsc::channel::<String>(1);
let (_stx, srx) = watch::channel(false);

let server = GatewayServer::new("127.0.0.1", 8080, tx, srx)
    .with_auth(Some("super-secret".into()));
Source

pub fn with_rate_limit(self, limit: u32) -> Self

Set the per-IP rate limit for POST /webhook.

limit is the maximum number of requests allowed per remote IP in a 60-second fixed window. Setting limit to 0 disables rate limiting.

§Example
use tokio::sync::{mpsc, watch};
use zeph_gateway::GatewayServer;

let (tx, _rx) = mpsc::channel::<String>(1);
let (_stx, srx) = watch::channel(false);

// Allow at most 30 webhook posts per minute per IP.
let server = GatewayServer::new("127.0.0.1", 8080, tx, srx)
    .with_rate_limit(30);
Source

pub fn with_max_body_size(self, size: usize) -> Self

Set the maximum allowed request body size in bytes.

Requests whose body exceeds this size are rejected with 413 Content Too Large before any handler is invoked. The default is 1 MiB (1 048 576 bytes).

§Example
use tokio::sync::{mpsc, watch};
use zeph_gateway::GatewayServer;

let (tx, _rx) = mpsc::channel::<String>(1);
let (_stx, srx) = watch::channel(false);

// Restrict bodies to 64 KiB.
let server = GatewayServer::new("127.0.0.1", 8080, tx, srx)
    .with_max_body_size(64 * 1024);
Source

pub async fn serve(self) -> Result<(), GatewayError>

Start the HTTP gateway server and block until shutdown is signalled.

Binds a TCP listener on the configured address, installs middleware (body-size limit → auth → rate limiting), and serves requests until the watch::Receiver supplied to GatewayServer::new transitions to true.

§Errors

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,