Try to fix subdomain architecture

Signed-off-by: arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-10-20 19:51:16 +01:00
parent 5ebb572251
commit 6eb9e76316
5 changed files with 48 additions and 37 deletions

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

53
main.go
View File

@ -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,32 +574,28 @@ 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 {
// Make finalRouter a subdomain router if necessary
var finalRouter *chi.Mux
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)
finalRouter = subdomains[config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string)]
} else {
hostRouter.Map("*", appRouter)
fmt.Println("Mapped service " + serviceInformation.Name + " to all subdomains")
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()))
}
} else {
main.(func(library.ServiceInitializationInformation) *chi.Mux)(library.ServiceInitializationInformation{
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())

View File

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

View File

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