traefik-helper/src/command/impl/addSubPathCommand.py

42 lines
1.3 KiB
Python
Raw Normal View History

2024-08-28 02:33:32 +01:00
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 <name> <domain> <path> <service host>")
def execute(self, traefikConfig: TraefikConfig, args):
if len(args) < 3:
self.printUsage()
return
2024-08-28 02:42:37 +01:00
router = args[0]
2024-08-28 02:33:32 +01:00
domain = args[1]
path = args[2]
serviceHost = args[3]
2024-08-28 02:53:53 +01:00
subPathName = "strip-" + router + "-" + path + "-prefix"
2024-08-28 02:33:32 +01:00
# Fix the path
if path.startswith("/") == False:
path = "/" + path
2024-08-28 02:42:37 +01:00
if traefikConfig.hasPathRewrite(subPathName):
2024-08-28 02:53:53 +01:00
print(f"Sub path for \"{router}\" already exists")
2024-08-28 02:33:32 +01:00
return
2024-08-28 02:42:37 +01:00
if traefikConfig.hasRouter(router) == False:
print(f"Router \"{router}\" does not exist")
2024-08-28 02:33:32 +01:00
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}\"")
2024-08-28 02:42:37 +01:00
traefikConfig.addSubPathRouter(subPathName, domain, path, serviceHost)
2024-08-28 02:33:32 +01:00
traefikConfig.save()
2024-08-28 02:46:46 +01:00
print(f"Access your service at http://{domain}{path}")