Skip to main content

nitr_std/
config.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// This file is part of Nitr.
3// See https://nitrweb.com/ for more information
4// Copyright (C) 2024-present Jose Quintana <joseluisq.net>
5
6//! Plain-data configuration types shared with the server layer.
7//!
8//! These live outside the modules that implement them, and outside every
9//! Cargo feature, on purpose: the shape of `nitr.toml` must not depend on
10//! which builtins a particular build compiled in. A server built without
11//! the `db` feature still parses a `[database]` section — it just refuses
12//! to enable the builtin, with a message saying so, instead of failing to
13//! recognize the configuration at all.
14
15use std::time::Duration;
16
17/// Policy and limits applied to every outbound `fetch` request.
18///
19/// None of this is reachable from Lua: a script declares intent (a timeout,
20/// a retry count) but cannot widen its own policy or uncap its budget.
21#[derive(Debug, Clone)]
22pub struct FetchOptions {
23    /// When set, only these exact host names may be fetched (compared
24    /// case-insensitively; applies to every redirect hop).
25    pub allowed_hosts: Option<Vec<String>>,
26    /// Allow requests to loopback/private/link-local addresses. Off by
27    /// default; enable for trusted internal aggregation.
28    pub allow_private_networks: bool,
29    /// Maximum response body size accumulated by `resp:text()` /
30    /// `resp:json()`, in bytes.
31    pub max_response_bytes: u64,
32    /// Maximum concurrent requests per `await_all(...)` call.
33    pub max_concurrent: usize,
34    /// Maximum outbound requests one inbound request may make in total.
35    /// `0` removes the cap.
36    pub max_per_request: u32,
37    /// How long to wait for a TCP/TLS connection to an upstream.
38    pub connect_timeout: Duration,
39    /// Default total budget per outbound request.
40    pub timeout: Duration,
41    /// Idle connections kept per upstream host.
42    pub pool_max_idle_per_host: usize,
43    /// Ceiling on the `retry.attempts` a call may ask for.
44    pub max_retries: u32,
45    /// Explicit proxy URL; `None` uses the environment variables unless
46    /// [`no_proxy`](Self::no_proxy) is set.
47    pub proxy: Option<String>,
48    /// Ignore the proxy environment variables.
49    pub no_proxy: bool,
50    /// Forward a W3C `traceparent` derived from the inbound request id.
51    pub propagate_trace_context: bool,
52}
53
54impl Default for FetchOptions {
55    fn default() -> Self {
56        Self {
57            allowed_hosts: None,
58            allow_private_networks: false,
59            max_response_bytes: 8 * 1024 * 1024, // 8 MiB
60            max_concurrent: 8,
61            max_per_request: 32,
62            connect_timeout: Duration::from_secs(10),
63            timeout: Duration::from_secs(30),
64            pool_max_idle_per_host: 8,
65            max_retries: 5,
66            proxy: None,
67            no_proxy: false,
68            propagate_trace_context: false,
69        }
70    }
71}
72
73/// Read policy for the `nitr.env` builtin.
74///
75/// The builtin itself is opt-in (`[std] features`); this narrows what an
76/// enabled one may see. `NITR_*` variables are hidden from scripts
77/// unconditionally — they configure the server, not the application.
78#[derive(Debug, Clone, Default)]
79pub struct EnvOptions {
80    /// Names scripts may read: exact names, or prefixes written with a
81    /// trailing `_` (`"APP_"`). `None` allows every non-`NITR_*` variable.
82    pub allow: Option<Vec<String>>,
83}
84
85/// The pragma set applied to a connection when it is opened.
86#[derive(Debug, Clone)]
87pub struct SqlitePragmas {
88    /// `"wal"`, `"delete"`, any other SQLite journal mode, or `"keep"` to
89    /// leave whatever the database already uses.
90    pub journal_mode: String,
91    /// Milliseconds to wait on a locked database before failing.
92    pub busy_timeout: u64,
93    /// `synchronous` pragma (`"off"`, `"normal"`, `"full"`, `"extra"`).
94    pub synchronous: String,
95    /// Enforce foreign-key constraints.
96    pub foreign_keys: bool,
97    /// `cache_size` per connection; negative values are KiB.
98    pub cache_size: i64,
99}
100
101impl Default for SqlitePragmas {
102    fn default() -> Self {
103        Self {
104            journal_mode: "wal".into(),
105            busy_timeout: 5_000,
106            synchronous: "normal".into(),
107            foreign_keys: true,
108            cache_size: -2_000,
109        }
110    }
111}