add catch all

This commit is contained in:
Lee
2025-02-07 22:12:01 +00:00
parent 019261a988
commit 209536614e
4 changed files with 60 additions and 1 deletions

View File

@ -4,6 +4,7 @@ from command.impl.removeCommand import RemoveCommand
from command.impl.restartCommand import RestartCommand from command.impl.restartCommand import RestartCommand
from command.impl.logsCommand import LogsCommand from command.impl.logsCommand import LogsCommand
from command.impl.addSubPathCommand import AddSubPathCommand from command.impl.addSubPathCommand import AddSubPathCommand
from command.impl.addCatchAllCommand import AddCatchAllCommand
class CommandManager: class CommandManager:
@ -16,8 +17,10 @@ class CommandManager:
self.addCommand(RestartCommand()) self.addCommand(RestartCommand())
self.addCommand(LogsCommand()) self.addCommand(LogsCommand())
self.addCommand(AddSubPathCommand()) self.addCommand(AddSubPathCommand())
self.addCommand(AddCatchAllCommand())
pass pass
def addCommand(self, command): def addCommand(self, command):
self.commands.append(command) self.commands.append(command)
pass pass

View File

@ -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 <name> <domain> <service host>")
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}")

View File

@ -17,6 +17,8 @@ class AddSubPathCommand(Command):
# Fix the path # Fix the path
if path.startswith("/") == False: if path.startswith("/") == False:
path = "/" + path path = "/" + path
if path.endswith("/") == True:
path = path[:-1] # Remove the trailing slash
serviceHost = args[3] serviceHost = args[3]
subPathName = domain + path subPathName = domain + path

View File

@ -38,6 +38,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): def addSubPathRouter(self, name, domain, path, serviceHost):
# Add trailing slashs # Add trailing slashs
if not path.endswith("/"): if not path.endswith("/"):