Attempt yet another method of activating services

Signed-off-by: Arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-10-15 19:17:29 +01:00
parent 44a584e34d
commit 5492212611
3 changed files with 28 additions and 78 deletions

83
main.go
View File

@ -48,10 +48,9 @@ type Config struct {
} }
type Service struct { type Service struct {
ServiceID uuid.UUID ServiceID uuid.UUID
ServiceMetadata library.Service ServiceMetadata library.Service
Inbox chan library.InterServiceMessage Inbox chan library.InterServiceMessage
ActivationConfirmed bool
} }
var ( var (
@ -73,44 +72,15 @@ func processInterServiceMessage(channel chan library.InterServiceMessage, config
if message.ForServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000000") { if message.ForServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000000") {
// Broadcast message // Broadcast message
for _, service := range services { for _, service := range services {
// We don't want to overwhelm a non-activated service service.Inbox <- message
if service.ActivationConfirmed {
service.Inbox <- message
}
} }
} else if message.ForServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000001") { } else if message.ForServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000001") {
// Service initialization service // Service initialization service
switch message.MessageType { switch message.MessageType {
case 0: case 0:
// Service initialization message, register the service // This has been deprecated, ignore it
lock.Lock() // Send "true" back
inbox := services[message.ServiceID].Inbox services[message.ServiceID].Inbox <- library.InterServiceMessage{
services[message.ServiceID] = Service{
ServiceID: message.ServiceID,
Inbox: inbox,
ActivationConfirmed: true,
ServiceMetadata: services[message.ServiceID].ServiceMetadata,
}
lock.Unlock()
if message.Message != nil {
// Add its router to the host router
serviceConfig, ok := config.Services[strings.ToLower(services[message.ServiceID].ServiceMetadata.Name)]
if !ok {
slog.Error("Service configuration not found for service: " + services[message.ServiceID].ServiceMetadata.Name)
os.Exit(1)
}
if serviceConfig.(map[string]interface{})["subdomain"] != nil {
hostRouter.Map(serviceConfig.(map[string]interface{})["subdomain"].(string), message.Message.(*chi.Mux))
fmt.Println("Mapped subdomain " + serviceConfig.(map[string]interface{})["subdomain"].(string) + " to service " + services[message.ServiceID].ServiceMetadata.Name)
} else {
hostRouter.Map("*", message.Message.(*chi.Mux))
fmt.Println("Mapped service " + services[message.ServiceID].ServiceMetadata.Name)
}
}
// Report a successful activation
inbox <- library.InterServiceMessage{
ServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"), ServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
ForServiceID: message.ServiceID, ForServiceID: message.ServiceID,
MessageType: 0, MessageType: 0,
@ -255,7 +225,7 @@ func processInterServiceMessage(channel chan library.InterServiceMessage, config
if ok && serviceMetadata.ServiceMetadata.Permissions.BlobStorage { if ok && serviceMetadata.ServiceMetadata.Permissions.BlobStorage {
// Send message to Blob Storage service // Send message to Blob Storage service
service, ok := services[uuid.MustParse("00000000-0000-0000-0000-000000000003")] service, ok := services[uuid.MustParse("00000000-0000-0000-0000-000000000003")]
if ok && service.ActivationConfirmed { if ok {
service.Inbox <- message service.Inbox <- message
} else if !ok { } else if !ok {
// Send error message // Send error message
@ -313,7 +283,7 @@ func processInterServiceMessage(channel chan library.InterServiceMessage, config
if ok && serviceMetadata.ServiceMetadata.Permissions.Authenticate { if ok && serviceMetadata.ServiceMetadata.Permissions.Authenticate {
// Send message to Authentication service // Send message to Authentication service
service, ok := services[uuid.MustParse("00000000-0000-0000-0000-000000000004")] service, ok := services[uuid.MustParse("00000000-0000-0000-0000-000000000004")]
if ok && service.ActivationConfirmed { if ok {
service.Inbox <- message service.Inbox <- message
} else if !ok { } else if !ok {
// Send error message // Send error message
@ -575,10 +545,9 @@ func main() {
var inbox = make(chan library.InterServiceMessage) var inbox = make(chan library.InterServiceMessage)
lock.Lock() lock.Lock()
services[serviceInformation.ServiceID] = Service{ services[serviceInformation.ServiceID] = Service{
ServiceID: serviceInformation.ServiceID, ServiceID: serviceInformation.ServiceID,
Inbox: inbox, Inbox: inbox,
ActivationConfirmed: false, ServiceMetadata: serviceInformation,
ServiceMetadata: serviceInformation,
} }
lock.Unlock() lock.Unlock()
@ -586,15 +555,24 @@ func main() {
// Check if they want a resource directory // Check if they want a resource directory
if serviceInformation.Permissions.Resources { if serviceInformation.Permissions.Resources {
main.(func(library.ServiceInitializationInformation))(library.ServiceInitializationInformation{ appRouter := main.(func(library.ServiceInitializationInformation) *chi.Mux)(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: os.DirFS(filepath.Join(config.Global.ResourceDirectory, serviceInformation.ServiceID.String())), 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")
}
}
} else { } else {
main.(func(library.ServiceInitializationInformation))(library.ServiceInitializationInformation{ main.(func(library.ServiceInitializationInformation) *chi.Mux)(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,
@ -606,21 +584,6 @@ func main() {
slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String()) slog.Info("Service " + serviceInformation.Name + " activated with ID " + serviceInformation.ServiceID.String())
} }
// Wait for all the services to have their activations confirmed
var allActivated bool
for !allActivated {
lock.RLock()
allActivated = true
for _, service := range services {
if !service.ActivationConfirmed {
allActivated = false
break
}
}
lock.RUnlock()
time.Sleep(500 * time.Millisecond)
}
// Start the server // Start the server
slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port) slog.Info("Starting server on " + config.Global.IP + ":" + config.Global.Port)
router.Mount("/", hostRouter) router.Mount("/", hostRouter)

View File

@ -174,7 +174,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) { func Main(information library.ServiceInitializationInformation) *chi.Mux {
var conn library.Database var conn library.Database
var mem *sql.DB var mem *sql.DB
var publicKey ed25519.PublicKey var publicKey ed25519.PublicKey
@ -1776,12 +1776,5 @@ func Main(information library.ServiceInitializationInformation) {
} }
}() }()
// Report a successful activation return router
information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID,
ForServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"), // Activation service
MessageType: 0,
SentAt: time.Now(),
Message: router,
}
} }

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
library "git.ailur.dev/ailur/fg-library/v2" library "git.ailur.dev/ailur/fg-library/v2"
"github.com/go-chi/chi/v5"
"path/filepath" "path/filepath"
"io" "io"
@ -322,7 +323,7 @@ func removeFile(file File, serviceID uuid.UUID, information library.ServiceIniti
} }
} }
func Main(information library.ServiceInitializationInformation) { func Main(information library.ServiceInitializationInformation) *chi.Mux {
go func() { go func() {
for { for {
message := <-information.Inbox message := <-information.Inbox
@ -420,12 +421,5 @@ func Main(information library.ServiceInitializationInformation) {
logFunc(response.Message.(error).Error(), 3, information) logFunc(response.Message.(error).Error(), 3, information)
} }
// Report a successful activation return nil
information.Outbox <- library.InterServiceMessage{
ServiceID: ServiceInformation.ServiceID,
ForServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"), // Activation service
MessageType: 0,
SentAt: time.Now(),
Message: nil,
}
} }