From c4dae7ac03f80a67b8f7f4ab84112026bd138161 Mon Sep 17 00:00:00 2001 From: Arzumify Date: Tue, 15 Oct 2024 18:29:16 +0100 Subject: [PATCH] Try to fix subdomains Signed-off-by: Arzumify --- go.mod | 1 + go.sum | 3 +++ main.go | 13 +++++-------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index a3b1476..0bc19b1 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( git.ailur.dev/ailur/pow v1.0.0 github.com/cespare/xxhash/v2 v2.3.0 github.com/go-chi/chi/v5 v5.1.0 + github.com/go-chi/hostrouter v0.2.0 github.com/go-playground/validator/v10 v10.22.1 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 30fe9e9..dbf7a49 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,11 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/hostrouter v0.2.0 h1:GwC7TZz8+SlJN/tV/aeJgx4F+mI5+sp+5H1PelQUjHM= +github.com/go-chi/hostrouter v0.2.0/go.mod h1:pJ49vWVmtsKRKZivQx0YMYv4h0aX+Gcn6V23Np9Wf1s= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= diff --git a/main.go b/main.go index 2f65681..172b43f 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "errors" library "git.ailur.dev/ailur/fg-library" - "github.com/go-chi/chi/v5/middleware" "io" "log" "os" @@ -20,6 +19,7 @@ import ( "path/filepath" "github.com/go-chi/chi/v5" + "github.com/go-chi/hostrouter" "github.com/go-playground/validator/v10" "github.com/google/uuid" @@ -478,6 +478,7 @@ func main() { // Create the router router := chi.NewRouter() router.Use(logger) + hostRouter := hostrouter.New() var globalOutbox = make(chan library.InterServiceMessage) @@ -564,20 +565,16 @@ func main() { lock.Unlock() // Check if they want a subdomain - var finalRouter *chi.Mux + finalRouter := chi.NewRouter() serviceConfig, ok := config.Services[strings.ToLower(serviceInformation.Name)] if !ok { slog.Error("Service configuration not found for service: ", serviceInformation.Name) os.Exit(1) } if serviceConfig.(map[string]interface{})["subdomain"] != nil { - subdomainRouter := chi.NewRouter() - router.Use(middleware.RouteHeaders(). - Route("Host", config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string), middleware.New(subdomainRouter)). - Handler) - finalRouter = subdomainRouter + hostRouter.Map(serviceConfig.(map[string]interface{})["subdomain"].(string), finalRouter) } else { - finalRouter = router + hostRouter.Map("", finalRouter) } slog.Info("Activating service " + serviceInformation.Name + " with ID " + serviceInformation.ServiceID.String())