Nyx by Example

Proxy Config

std/toml parses TOML config files into a flat Map with dot-notation keys. Helpers toml_get_int/bool/string convert values with defaults. Arrays-of-tables ([[upstream]]) require custom parsing.

Code

// nyx-proxy config — parsing proxy.toml with std/toml
// Config nyx-proxy — parsear proxy.toml con std/toml

import "std/toml"

fn main() -> int {
    // A typical proxy.toml config file
    let config_text: String = "[server]\nport = 443\ntls = true\n\n[[upstream]]\nname = \"api\"\nhost = \"127.0.0.1\"\nport = 3000\n\n[[upstream]]\nname = \"static\"\nhost = \"127.0.0.1\"\nport = 3001\n\n[health]\ninterval = 5\ntimeout = 2\nthreshold = 3\n\n[ratelimit]\nper_ip_per_sec = 100"

    let parsed: Map = toml_parse(config_text)

    // Read server config
    let port: int = toml_get_int(parsed, "server.port", 443)
    let tls: bool = toml_get_bool(parsed, "server.tls", false)
    print("server: port=" + int_to_string(port) + " tls=" + int_to_string(tls))

    // Read health config
    let interval: int = toml_get_int(parsed, "health.interval", 10)
    let threshold: int = toml_get_int(parsed, "health.threshold", 3)
    print("health: interval=" + int_to_string(interval) + "s threshold=" + int_to_string(threshold))

    // Read rate limit config
    let rate: int = toml_get_int(parsed, "ratelimit.per_ip_per_sec", 50)
    print("ratelimit: " + int_to_string(rate) + " req/s per IP")

    // Note: TOML arrays-of-tables ([[upstream]]) are not supported by std/toml
    // nyx-proxy uses a custom config loader for those

    return 0
}

Output

server: port=443 tls=1
health: interval=5s threshold=3
ratelimit: 100 req/s per IP

Explanation

Config-as-code is nice until you need a human to edit it. TOML is the sweet spot: it's readable like INI, typed like JSON, and doesn't have YAML's whitespace footguns. std/toml flattens nested tables into dot-notation keys ([server] + port becomes "server.port"), which keeps the API a simple Map. The toml_get_* helpers handle the two things you always want: type coercion and defaults — so missing keys don't crash, they fall back to a sane value. Arrays-of-tables ([[upstream]]) are common in proxy configs for defining a list of backends; std/toml doesn't handle those yet, so nyx-proxy ships a small custom parser for its upstream block.

← Previous Next →

Source: examples/by-example/85-proxy-config.nx