From 6eb9e7631609b6c99e0f3555783fb53c77ba363f Mon Sep 17 00:00:00 2001 From: arzumify Date: Sun, 20 Oct 2024 19:51:16 +0100 Subject: [PATCH] Try to fix subdomain architecture Signed-off-by: arzumify --- go.mod | 2 +- go.sum | 4 +-- main.go | 67 ++++++++++++++++++++++-------------- services-src/auth/main.go | 7 ++-- services-src/storage/main.go | 5 +-- 5 files changed, 48 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index cc605a2..2d07a5f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.ailur.dev/ailur/fulgens go 1.23.1 require ( - git.ailur.dev/ailur/fg-library/v2 v2.0.1 + git.ailur.dev/ailur/fg-library/v2 v2.1.0 git.ailur.dev/ailur/fg-nucleus-library v1.0.2 git.ailur.dev/ailur/pow v1.0.0 github.com/cespare/xxhash/v2 v2.3.0 diff --git a/go.sum b/go.sum index 7036fdd..431ce33 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.ailur.dev/ailur/fg-library/v2 v2.0.1 h1:ltPYXf/Om0hnMD8gr1K5bkYrfHqKPSbb0hxa0wtTnZ0= -git.ailur.dev/ailur/fg-library/v2 v2.0.1/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8= +git.ailur.dev/ailur/fg-library/v2 v2.1.0 h1:SsLZ56poM6GZPfV/ywU/8WDTelu2dtlPp6jzbEZ4hrA= +git.ailur.dev/ailur/fg-library/v2 v2.1.0/go.mod h1:1jYbWhabGcIwp7CkhHqvRwC8eP+nHv5BrXPe9NX2HE8= git.ailur.dev/ailur/fg-nucleus-library v1.0.2 h1:EWfeab+wJKaxx/Qg5TKpvZHicA0V/NilUv2g6W97rtg= git.ailur.dev/ailur/fg-nucleus-library v1.0.2/go.mod h1:T2mdUiXlZqb917CkNB2vwujkD/QhJDpCHLRvKuskBpY= git.ailur.dev/ailur/pow v1.0.0 h1:eCJiZSbskcmzmwR4Nv4YrYpsZci5kfoGM9ihkXAHHoU= diff --git a/main.go b/main.go index 7486dbd..2c71cb4 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,9 @@ package main import ( "errors" - "fmt" library "git.ailur.dev/ailur/fg-library/v2" "io" + "io/fs" "log" "os" "plugin" @@ -464,10 +464,31 @@ func main() { config = parseConfig(os.Args[1]) } + // If we are using sqlite, create the database directory if it does not exist + if config.Database.DatabaseType == "sqlite" { + err := os.MkdirAll(config.Database.DatabasePath, 0755) + if err != nil { + slog.Error("Error creating database directory: ", err) + os.Exit(1) + } + } + // Create the router router := chi.NewRouter() router.Use(logger) + // Iterate through the service configurations and create routers for each unique subdomain + subdomains := make(map[string]*chi.Mux) + for _, service := range config.Services { + if service.(map[string]interface{})["subdomain"] != nil { + subdomain := service.(map[string]interface{})["subdomain"].(string) + if subdomains[subdomain] == nil { + subdomains[subdomain] = chi.NewRouter() + hostRouter.Map(subdomain, subdomains[subdomain]) + } + } + } + var globalOutbox = make(chan library.InterServiceMessage) // Initialize the service discovery, health-check, and logging services @@ -553,33 +574,29 @@ func main() { slog.Info("Activating service " + serviceInformation.Name + " with ID " + serviceInformation.ServiceID.String()) - // Check if they want a resource directory - if serviceInformation.Permissions.Resources { - appRouter := main.(func(library.ServiceInitializationInformation) *chi.Mux)(library.ServiceInitializationInformation{ - Domain: serviceInformation.Name, - Configuration: config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{}), - Outbox: globalOutbox, - Inbox: inbox, - ResourceDir: os.DirFS(filepath.Join(config.Global.ResourceDirectory, serviceInformation.ServiceID.String())), - }) - if appRouter != nil { - if config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"] != nil { - hostRouter.Map(config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string), appRouter) - fmt.Println("Mapped subdomain " + config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string) + " to service " + serviceInformation.Name) - } else { - hostRouter.Map("*", appRouter) - fmt.Println("Mapped service " + serviceInformation.Name + " to all subdomains") - } - } + // Make finalRouter a subdomain router if necessary + var finalRouter *chi.Mux + if config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"] != nil { + finalRouter = subdomains[config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string)] } else { - main.(func(library.ServiceInitializationInformation) *chi.Mux)(library.ServiceInitializationInformation{ - Domain: serviceInformation.Name, - Configuration: config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{}), - Outbox: globalOutbox, - Inbox: inbox, - }) + finalRouter = router } + // Check if they want a resource directory + var resourceDir fs.FS = nil + if serviceInformation.Permissions.Resources { + resourceDir = os.DirFS(filepath.Join(config.Global.ResourceDirectory, serviceInformation.ServiceID.String())) + } + + main.(func(library.ServiceInitializationInformation))(library.ServiceInitializationInformation{ + Domain: serviceInformation.Name, + Configuration: config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{}), + Outbox: globalOutbox, + Inbox: inbox, + ResourceDir: resourceDir, + Router: finalRouter, + }) + // Log the service activation slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String()) } diff --git a/services-src/auth/main.go b/services-src/auth/main.go index e49388e..c3df80c 100644 --- a/services-src/auth/main.go +++ b/services-src/auth/main.go @@ -30,7 +30,6 @@ import ( // External libraries "github.com/cespare/xxhash/v2" - "github.com/go-chi/chi/v5" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" _ "modernc.org/sqlite" @@ -174,7 +173,7 @@ func verifyJwt(token string, publicKey ed25519.PublicKey, mem *sql.DB) ([]byte, return userId, claims, true } -func Main(information library.ServiceInitializationInformation) *chi.Mux { +func Main(information library.ServiceInitializationInformation) { var conn library.Database var mem *sql.DB var publicKey ed25519.PublicKey @@ -342,7 +341,7 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux { } // Set up the router - router := chi.NewRouter() + router := information.Router // Add the CORS middleware disableCors := func(next http.Handler) http.Handler { @@ -1799,6 +1798,4 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux { } } }() - - return router } diff --git a/services-src/storage/main.go b/services-src/storage/main.go index f1e3b78..c3874ea 100644 --- a/services-src/storage/main.go +++ b/services-src/storage/main.go @@ -5,7 +5,6 @@ import ( "errors" library "git.ailur.dev/ailur/fg-library/v2" nucleusLibrary "git.ailur.dev/ailur/fg-nucleus-library" - "github.com/go-chi/chi/v5" "path/filepath" "os" @@ -284,7 +283,7 @@ func removeFile(file nucleusLibrary.File, serviceID uuid.UUID, information libra } } -func Main(information library.ServiceInitializationInformation) *chi.Mux { +func Main(information library.ServiceInitializationInformation) { go func() { for { message := <-information.Inbox @@ -457,6 +456,4 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux { // Log the error message to the logger service logFunc(response.Message.(error).Error(), 3, information) } - - return nil }