diff --git a/src/command/commandManager.py b/src/command/commandManager.py index 5fc73da..27cc07d 100644 --- a/src/command/commandManager.py +++ b/src/command/commandManager.py @@ -4,6 +4,7 @@ from command.impl.removeCommand import RemoveCommand from command.impl.restartCommand import RestartCommand from command.impl.logsCommand import LogsCommand from command.impl.addSubPathCommand import AddSubPathCommand +from command.impl.addCatchAllCommand import AddCatchAllCommand class CommandManager: @@ -13,11 +14,13 @@ class CommandManager: self.addCommand(AddCommand()) self.addCommand(RemoveCommand()) self.addCommand(ListCommand()) - self.addCommand(RestartCommand()) + self.addCommand(RestartCommand()) self.addCommand(LogsCommand()) self.addCommand(AddSubPathCommand()) + self.addCommand(AddCatchAllCommand()) pass + def addCommand(self, command): self.commands.append(command) pass diff --git a/src/command/impl/addCatchAllCommand.py b/src/command/impl/addCatchAllCommand.py new file mode 100644 index 0000000..58f97c0 --- /dev/null +++ b/src/command/impl/addCatchAllCommand.py @@ -0,0 +1,32 @@ +from command.command import Command +from traefik.traefikConfig import TraefikConfig +from utils.dockerUtils import restartTraefik + +class AddCommand(Command): + def __init__(self): + super().__init__("add-catch-all", "Add a catch all domain", "add-catch-all ") + + def execute(self, traefikConfig: TraefikConfig, args): + if len(args) < 3: + self.printUsage() + return + + name = args[0] + domain = args[1] + serviceHost = args[2] + + if traefikConfig.hasRouter(name): + print(f"Router \"{name}\" already exists") + 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.addCatchAllRouter(name, domain, serviceHost) + traefikConfig.save() + + 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 index 3fd8ac3..71dda35 100644 --- a/src/command/impl/addSubPathCommand.py +++ b/src/command/impl/addSubPathCommand.py @@ -17,6 +17,8 @@ class AddSubPathCommand(Command): # Fix the path if path.startswith("/") == False: path = "/" + path + if path.endswith("/") == True: + path = path[:-1] # Remove the trailing slash serviceHost = args[3] subPathName = domain + path diff --git a/src/traefik/traefikConfig.py b/src/traefik/traefikConfig.py index 24b34e1..cf103e9 100644 --- a/src/traefik/traefikConfig.py +++ b/src/traefik/traefikConfig.py @@ -37,6 +37,28 @@ class TraefikConfig: ] } } + + def addCatchAllRouter(self, name, domain, serviceHost): + # Add router + self.configYml["http"]["routers"][name] = { + "entryPoints": ["https"], + "rule": "HostRegexp(`{subdomain:[A-Za-z0-9-]+}.%s`)" % domain, + "middlewares": ["default-headers", "https-redirectscheme"], + "tls": {}, + "priority": 1, + "service": name + } + + # Add service + self.configYml["http"]["services"][name] = { + "loadBalancer": { + "servers": [ + { + "url": serviceHost + } + ] + } + } def addSubPathRouter(self, name, domain, path, serviceHost): # Add trailing slashs