Crates.io | haproxy-config |
lib.rs | haproxy-config |
version | 0.4.1 |
source | src |
created_at | 2023-04-10 12:23:01.46012 |
updated_at | 2023-04-14 16:00:24.376793 |
description | Parse HAProxy configs and easily query it |
homepage | |
repository | https://github.com/dvdsk/haproxy-config |
max_upload_size | |
id | 835084 |
size | 201,375 |
Parse HAProxy configs and easily query it
See also:
A parser for HAProxy config files. HAProxy's configs have many options to many to build a completely typed API. Such an API would also be quite fragile to changes in the config. This crate therefore presents a loosely typed config.
It parses to sections
consisting of lines
from which a Config
struct can be made. The struct follows the sections of a HAProxy config. Most options within the sections are presented in a HashMap
as key value strings. The important settings have a fully typed API.
List all the ports HAProxy will bind to from the config file.
use haproxy_config::parse_sections;
use haproxy_config::Config;
let file = include_str!("../tests/medium_haproxy.cfg");
let sections = parse_sections(file).unwrap();
let config = Config::try_from(§ions).unwrap();
let frontend_ports = config.frontends.values().map(|f| f.bind.addr.port);
let listen_ports = config.listen.values().map(|f| f.bind.addr.port);
let ports: Vec<_> = frontend_ports.chain(listen_ports).collect();
println!("ports bound to by haproxy: {ports:?}")
Because the API is not fully typed the crate allows some invalid configs to parse. Specifically any invalid configuration inside a section will be interpreted as a configuration value. Sections end at the next section or the end of the file therefore any invalid configuration after the first section is undetected.
The crate will break on valid configs that feature conditional blocks.
This crate is far from complete but covers all my own use cases. I do however welcome any contributions.