diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..bdf6569 --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +[[source]] +type = "traefik" +url = "http://10.23.1.103:8080" +destination = "http://10.23.1.103" diff --git a/create-nginx-map.py b/create-nginx-map.py index aaa1305..e5eabec 100644 --- a/create-nginx-map.py +++ b/create-nginx-map.py @@ -3,15 +3,30 @@ from typing import NamedTuple import re import sys import operator +import tomllib +from pathlib import Path +from typing import TypedDict TRAEFIK_HOST_RE = re.compile(r"Host\(`([a-z0-9\.-]+)`\)") +CONFIG_FILE = Path.cwd() / "config.toml" + class Route(NamedTuple): name: str destination: str hostname: str +class Source(TypedDict): + type: str + url: str + destination: str + + +class Config(TypedDict): + source: list[Source] + + def parse_traefik_rule(rule: str) -> list[str]: if "Host(" not in rule: return [] @@ -58,9 +73,15 @@ def get_dokku_routes(dokku_exporter_url: str, dokku_route: str): def main(): + config: Config = tomllib.loads(CONFIG_FILE.read_text()) + routes = [] - routes.extend(get_traefik_routes("http://10.23.1.103:8080", "http://10.23.1.103")) - routes.extend(get_dokku_routes("http://10.23.2.3:8000", "http://10.23.2.3")) + for source in config["source"]: + match source["type"]: + case "traefik": + routes.extend(get_traefik_routes(source["url"], source["destination"])) + case "dokku": + routes.extend(get_dokku_routes(source["url"], source["destination"])) for route in sorted(routes, key=operator.attrgetter("hostname")): print(f"{route.hostname}\t{route.destination}; # {route.name}")