From 30e3d3363ca5cd6447153e06437518bbb8af9c6b Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 28 Aug 2024 02:33:32 +0100 Subject: [PATCH] maybe add sub path support --- src/command/impl/addCommand.py | 4 --- src/command/impl/addSubPathCommand.py | 41 +++++++++++++++++++++++++++ src/traefik/traefikConfig.py | 39 +++++++++++++++++++------ 3 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 src/command/impl/addSubPathCommand.py diff --git a/src/command/impl/addCommand.py b/src/command/impl/addCommand.py index cad9d92..3851257 100644 --- a/src/command/impl/addCommand.py +++ b/src/command/impl/addCommand.py @@ -27,10 +27,6 @@ class AddCommand(Command): print(f"Adding \"{domain}\" -> \"{serviceHost}\"") traefikConfig.addRouter(name, domain, serviceHost) - traefikConfig.addService(name, serviceHost) - traefikConfig.save() - # restartTraefik() - print(f"Access your service at http://{domain}") \ No newline at end of file diff --git a/src/command/impl/addSubPathCommand.py b/src/command/impl/addSubPathCommand.py new file mode 100644 index 0000000..a7c0ac2 --- /dev/null +++ b/src/command/impl/addSubPathCommand.py @@ -0,0 +1,41 @@ +from command.command import Command +from traefik.traefikConfig import TraefikConfig +from utils.dockerUtils import restartTraefik + +class AddSubPathCommand(Command): + def __init__(self): + super().__init__("add-path", "Add sub path to a domain (eg: bob.com/joe)", "add-path ") + + def execute(self, traefikConfig: TraefikConfig, args): + if len(args) < 3: + self.printUsage() + return + + domain = args[1] + name = args[0] + "-sub-path-" + domain + path = args[2] + serviceHost = args[3] + + # Fix the path + if path.startswith("/") == False: + path = "/" + path + + if traefikConfig.hasPathRewrite(name): + print(f"Router \"{name}\" already exists") + return + + if traefikConfig.hasRouter(name) == False: + print(f"Router \"{name}\" does not exist") + return + + # Validate if the service host is a valid URL + if not serviceHost.startswith("http://") and not serviceHost.startswith("https://"): + print(f"Service host \"{serviceHost}\" is not a valid URL") + return + + print(f"Adding \"{domain}\" -> \"{serviceHost}\"") + + traefikConfig.addSubPathRouter(name, domain, path, serviceHost) + traefikConfig.save() + + print(f"Access your service at http://{domain}") \ No newline at end of file diff --git a/src/traefik/traefikConfig.py b/src/traefik/traefikConfig.py index b0f9e8a..0628b1f 100644 --- a/src/traefik/traefikConfig.py +++ b/src/traefik/traefikConfig.py @@ -38,6 +38,29 @@ class TraefikConfig: } } + def addSubPathRouter(self, name, domain, path, serviceHost): + self.addPathRewrite(name, path) + + # Add router + self.configYml["http"]["routers"][name] = { + "entryPoints": ["https"], + "rule": "Host(`%s`) && PathPrefix(`%s`)" % (domain, path), + "middlewares": ["default-headers", "https-redirectscheme", name], + "tls": {}, + "service": name + } + + # Add service + self.configYml["http"]["services"][name] = { + "loadBalancer": { + "servers": [ + { + "url": serviceHost + } + ] + } + } + def removeRouter(self, name): # Remove router del self.configYml["http"]["routers"][name] @@ -47,16 +70,14 @@ class TraefikConfig: def hasRouter(self, name): return name in self.configYml["http"]["routers"] + + def hasPathRewrite(self, name): + return name in self.configYml["http"]["middlewares"] - def addService(self, name, serviceHost): - # Add service - self.configYml["http"]["services"][name] = { - "loadBalancer": { - "servers": [ - { - "url": serviceHost - } - ] + def addPathRewrite(self, name, path): + self.configYml["http"]["middlewares"][name] = { + "stripPrefix": { + "prefixes": [path] } }