maybe add sub path support
Some checks failed
Publish Docker Image / docker (push) Failing after 1m34s

This commit is contained in:
Lee 2024-08-28 02:33:32 +01:00
parent 97ac22e62e
commit 30e3d3363c
3 changed files with 71 additions and 13 deletions

@ -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}")

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

@ -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]
@ -48,15 +71,13 @@ class TraefikConfig:
def hasRouter(self, name):
return name in self.configYml["http"]["routers"]
def addService(self, name, serviceHost):
# Add service
self.configYml["http"]["services"][name] = {
"loadBalancer": {
"servers": [
{
"url": serviceHost
}
]
def hasPathRewrite(self, name):
return name in self.configYml["http"]["middlewares"]
def addPathRewrite(self, name, path):
self.configYml["http"]["middlewares"][name] = {
"stripPrefix": {
"prefixes": [path]
}
}