pub struct AppState {
pub client: SearchClient,
pub started: Instant,
pub api_key: Option<String>,
pub max_results_limit: usize,
pub rate_limit_per_minute: u32,
pub max_body_bytes: u64,
pub trusted_proxies: Vec<IpAddr>,
/* private fields */
}Expand description
Shared server state: the search client, rate limiter, API key and
per-request bounds. Construct with AppState::new.
Fields§
§client: SearchClientThe search client serving every endpoint.
started: InstantTime the server started, used for uptime_s.
api_key: Option<String>Optional API key; when set, requests must authenticate.
max_results_limit: usizeUpper bound applied to max_results (from search.max_results_limit).
rate_limit_per_minute: u32Requests allowed per window per client; 0 disables the limiter.
max_body_bytes: u64Maximum accepted request body size in bytes.
trusted_proxies: Vec<IpAddr>Reverse-proxy IPs whose X-Forwarded-For header is trusted for
client IP extraction (see the client_ip helper).
Implementations§
Source§impl AppState
impl AppState
Sourcepub fn new(
client: SearchClient,
api_key: Option<String>,
max_results_limit: usize,
rate_limit_per_minute: u32,
max_body_bytes: u64,
trusted_proxies: Vec<IpAddr>,
) -> Self
pub fn new( client: SearchClient, api_key: Option<String>, max_results_limit: usize, rate_limit_per_minute: u32, max_body_bytes: u64, trusted_proxies: Vec<IpAddr>, ) -> Self
Build server state from a ready SearchClient and the parsed
configuration values.
Constant-time API-key comparison: a configured key is only ever compared with XOR folds of equal-length byte buffers, never with early-exit string equality, so timing cannot leak key prefixes.
Sourcepub fn check_rate(&self, ip: Option<IpAddr>) -> bool
pub fn check_rate(&self, ip: Option<IpAddr>) -> bool
Fixed-window rate limit keyed on the client IP (falling back to a
single global bucket when the client address is unknown, e.g. in
unit tests). Returns true when the request is allowed.