Try to fix subdomain architecture
Signed-off-by: arzumify <jliwin98@danwin1210.de>
This commit is contained in:
parent
5ebb572251
commit
6eb9e76316
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module git.ailur.dev/ailur/fulgens
|
||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
|
||||||
require (
|
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/fg-nucleus-library v1.0.2
|
||||||
git.ailur.dev/ailur/pow v1.0.0
|
git.ailur.dev/ailur/pow v1.0.0
|
||||||
github.com/cespare/xxhash/v2 v2.3.0
|
github.com/cespare/xxhash/v2 v2.3.0
|
||||||
|
|
4
go.sum
4
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.1.0 h1:SsLZ56poM6GZPfV/ywU/8WDTelu2dtlPp6jzbEZ4hrA=
|
||||||
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/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 h1:EWfeab+wJKaxx/Qg5TKpvZHicA0V/NilUv2g6W97rtg=
|
||||||
git.ailur.dev/ailur/fg-nucleus-library v1.0.2/go.mod h1:T2mdUiXlZqb917CkNB2vwujkD/QhJDpCHLRvKuskBpY=
|
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=
|
git.ailur.dev/ailur/pow v1.0.0 h1:eCJiZSbskcmzmwR4Nv4YrYpsZci5kfoGM9ihkXAHHoU=
|
||||||
|
|
53
main.go
53
main.go
|
@ -2,9 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
library "git.ailur.dev/ailur/fg-library/v2"
|
library "git.ailur.dev/ailur/fg-library/v2"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"plugin"
|
"plugin"
|
||||||
|
@ -464,10 +464,31 @@ func main() {
|
||||||
config = parseConfig(os.Args[1])
|
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
|
// Create the router
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
router.Use(logger)
|
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)
|
var globalOutbox = make(chan library.InterServiceMessage)
|
||||||
|
|
||||||
// Initialize the service discovery, health-check, and logging services
|
// 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())
|
slog.Info("Activating service " + serviceInformation.Name + " with ID " + serviceInformation.ServiceID.String())
|
||||||
|
|
||||||
// Check if they want a resource directory
|
// Make finalRouter a subdomain router if necessary
|
||||||
if serviceInformation.Permissions.Resources {
|
var finalRouter *chi.Mux
|
||||||
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 {
|
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)
|
finalRouter = subdomains[config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string)]
|
||||||
fmt.Println("Mapped subdomain " + config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{})["subdomain"].(string) + " to service " + serviceInformation.Name)
|
|
||||||
} else {
|
} else {
|
||||||
hostRouter.Map("*", appRouter)
|
finalRouter = router
|
||||||
fmt.Println("Mapped service " + serviceInformation.Name + " to all subdomains")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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,
|
Domain: serviceInformation.Name,
|
||||||
Configuration: config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{}),
|
Configuration: config.Services[strings.ToLower(serviceInformation.Name)].(map[string]interface{}),
|
||||||
Outbox: globalOutbox,
|
Outbox: globalOutbox,
|
||||||
Inbox: inbox,
|
Inbox: inbox,
|
||||||
|
ResourceDir: resourceDir,
|
||||||
|
Router: finalRouter,
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
// Log the service activation
|
// Log the service activation
|
||||||
slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String())
|
slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String())
|
||||||
|
|
|
@ -30,7 +30,6 @@ import (
|
||||||
|
|
||||||
// External libraries
|
// External libraries
|
||||||
"github.com/cespare/xxhash/v2"
|
"github.com/cespare/xxhash/v2"
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
|
@ -174,7 +173,7 @@ func verifyJwt(token string, publicKey ed25519.PublicKey, mem *sql.DB) ([]byte,
|
||||||
return userId, claims, true
|
return userId, claims, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Main(information library.ServiceInitializationInformation) *chi.Mux {
|
func Main(information library.ServiceInitializationInformation) {
|
||||||
var conn library.Database
|
var conn library.Database
|
||||||
var mem *sql.DB
|
var mem *sql.DB
|
||||||
var publicKey ed25519.PublicKey
|
var publicKey ed25519.PublicKey
|
||||||
|
@ -342,7 +341,7 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the router
|
// Set up the router
|
||||||
router := chi.NewRouter()
|
router := information.Router
|
||||||
|
|
||||||
// Add the CORS middleware
|
// Add the CORS middleware
|
||||||
disableCors := func(next http.Handler) http.Handler {
|
disableCors := func(next http.Handler) http.Handler {
|
||||||
|
@ -1799,6 +1798,4 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return router
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
library "git.ailur.dev/ailur/fg-library/v2"
|
library "git.ailur.dev/ailur/fg-library/v2"
|
||||||
nucleusLibrary "git.ailur.dev/ailur/fg-nucleus-library"
|
nucleusLibrary "git.ailur.dev/ailur/fg-nucleus-library"
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"os"
|
"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() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
message := <-information.Inbox
|
message := <-information.Inbox
|
||||||
|
@ -457,6 +456,4 @@ func Main(information library.ServiceInitializationInformation) *chi.Mux {
|
||||||
// Log the error message to the logger service
|
// Log the error message to the logger service
|
||||||
logFunc(response.Message.(error).Error(), 3, information)
|
logFunc(response.Message.(error).Error(), 3, information)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue