2024-09-28 19:41:34 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-09-29 19:03:13 +01:00
|
|
|
"errors"
|
2024-10-15 19:06:05 +01:00
|
|
|
library "git.ailur.dev/ailur/fg-library/v2"
|
2024-10-16 18:05:52 +01:00
|
|
|
nucleusLibrary "git.ailur.dev/ailur/fg-nucleus-library"
|
2024-09-28 19:41:34 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ServiceInformation = library.Service{
|
|
|
|
Name: "Storage",
|
|
|
|
Permissions: library.Permissions{
|
|
|
|
Authenticate: false, // This service does not require authentication
|
|
|
|
Database: true, // This service requires database access to store quotas
|
|
|
|
BlobStorage: false, // This service *is* the blob storage
|
|
|
|
InterServiceCommunication: true, // This service does require inter-service communication
|
|
|
|
},
|
|
|
|
ServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000003"),
|
|
|
|
}
|
|
|
|
|
2024-10-13 19:20:19 +01:00
|
|
|
var conn library.Database
|
2024-09-28 19:41:34 +01:00
|
|
|
|
|
|
|
func getQuota(user uuid.UUID, information library.ServiceInitializationInformation) (int64, error) {
|
|
|
|
// Get the user's quota from the database
|
|
|
|
var quota int64
|
2024-10-13 19:20:19 +01:00
|
|
|
userBytes, err := user.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
err = conn.DB.QueryRow("SELECT quota FROM quotas WHERE id = $1", userBytes).Scan("a)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
2024-09-29 19:03:13 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
// The user has no quota set, so we'll set it to the default quota
|
2024-10-13 19:20:19 +01:00
|
|
|
_, err = conn.DB.Exec("INSERT INTO quotas (id, quota) VALUES ($1, $2)", userBytes, int64(information.Configuration["defaultQuota"].(float64)))
|
2024-09-29 19:03:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int64(information.Configuration["defaultQuota"].(float64)), nil
|
|
|
|
}
|
2024-09-28 19:41:34 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return quota, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUsed(user uuid.UUID, information library.ServiceInitializationInformation) (int64, error) {
|
|
|
|
// Check the user's used space via the filesystem
|
|
|
|
var used int64
|
2024-09-29 19:03:13 +01:00
|
|
|
_, err := os.Stat(filepath.Join(information.Configuration["path"].(string), user.String()))
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
2024-09-29 19:03:13 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// The user has no files stored, so we'll set it to 0
|
|
|
|
return 0, nil
|
|
|
|
}
|
2024-09-28 19:41:34 +01:00
|
|
|
return 0, err
|
2024-09-29 19:03:13 +01:00
|
|
|
} else {
|
|
|
|
err := filepath.Walk(filepath.Join(information.Configuration["path"].(string), user.String()), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
used += info.Size()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return used, nil
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func logFunc(message string, messageType uint64, information library.ServiceInitializationInformation) {
|
|
|
|
// Log the error message to the logger service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000002"), // Logger service
|
|
|
|
MessageType: messageType,
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:05:52 +01:00
|
|
|
func storeFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
|
2024-09-28 19:41:34 +01:00
|
|
|
// Create a folder for the user if it doesn't exist
|
2024-10-16 18:05:52 +01:00
|
|
|
err := os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.User.String()), 0755)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user has enough space to store the file
|
2024-10-20 09:31:59 +01:00
|
|
|
// Get the user's used space
|
|
|
|
used, err := getUsed(file.User, information)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 09:31:59 +01:00
|
|
|
// Check if the file already exists
|
|
|
|
stats, err := os.Stat(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name))
|
|
|
|
if err == nil {
|
|
|
|
// The file already exists, subtract the old file size from the user's used space
|
|
|
|
used -= stats.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the user's quota
|
|
|
|
quota, err := getQuota(file.User, information)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user has enough space to store the file
|
2024-10-16 18:05:52 +01:00
|
|
|
if used+int64(len(file.Bytes)) > quota {
|
2024-09-28 19:41:34 +01:00
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 3, // It's the user's fault (never say that to the customer ;P)
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: "User has exceeded their quota",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a folder within that for the service if it doesn't exist
|
2024-10-16 18:05:52 +01:00
|
|
|
err = os.MkdirAll(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String()), 0755)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the file
|
2024-10-16 18:05:52 +01:00
|
|
|
fileStream, err := os.OpenFile(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the file
|
2024-10-16 18:05:52 +01:00
|
|
|
_, err = fileStream.Write(file.Bytes)
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the file
|
|
|
|
err = fileStream.Close()
|
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report success
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 0, // Success
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:05:52 +01:00
|
|
|
func readFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
|
2024-09-28 19:41:34 +01:00
|
|
|
// Open the file
|
2024-10-16 18:05:52 +01:00
|
|
|
fileStream, err := os.Open(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name))
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:05:52 +01:00
|
|
|
// Return the reader
|
2024-09-28 19:41:34 +01:00
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 0, // Success
|
|
|
|
SentAt: time.Now(),
|
2024-10-16 18:05:52 +01:00
|
|
|
Message: fileStream,
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 18:05:52 +01:00
|
|
|
func removeFile(file nucleusLibrary.File, serviceID uuid.UUID, information library.ServiceInitializationInformation) {
|
2024-09-28 19:41:34 +01:00
|
|
|
// Remove the file
|
|
|
|
err := os.Remove(filepath.Join(information.Configuration["path"].(string), file.User.String(), serviceID.String(), file.Name))
|
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report success
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: serviceID,
|
|
|
|
MessageType: 0, // Success
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-20 19:51:16 +01:00
|
|
|
func Main(information library.ServiceInitializationInformation) {
|
2024-09-28 19:41:34 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
message := <-information.Inbox
|
|
|
|
if message.ServiceID == uuid.MustParse("00000000-0000-0000-0000-000000000001") {
|
|
|
|
if message.MessageType == 1 {
|
|
|
|
// We've received an error message. This should never happen.
|
|
|
|
logFunc("Bit flip error: Error given to non-errored service. Move away from radiation or use ECC memory.", 3, information)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch message.MessageType {
|
|
|
|
case 0:
|
|
|
|
// Insert file
|
|
|
|
validate := validator.New()
|
2024-10-16 18:05:52 +01:00
|
|
|
err := validate.Struct(message.Message.(nucleusLibrary.File))
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 2, // An error that's your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Store file
|
2024-10-16 18:05:52 +01:00
|
|
|
storeFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
// Read file
|
|
|
|
validate := validator.New()
|
2024-10-16 18:05:52 +01:00
|
|
|
err := validate.Struct(message.Message.(nucleusLibrary.File))
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 2, // An error that's your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Read file
|
2024-10-16 18:05:52 +01:00
|
|
|
readFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
// Remove file
|
|
|
|
validate := validator.New()
|
2024-10-16 18:05:52 +01:00
|
|
|
err := validate.Struct(message.Message.(nucleusLibrary.File))
|
2024-09-28 19:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-28 19:41:34 +01:00
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 2, // An error that's your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove file
|
2024-10-16 18:05:52 +01:00
|
|
|
removeFile(message.Message.(nucleusLibrary.File), message.ServiceID, information)
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
2024-10-18 17:52:01 +01:00
|
|
|
case 3:
|
|
|
|
// Get quota
|
|
|
|
validate := validator.New()
|
|
|
|
err := validate.Struct(message.Message.(uuid.UUID))
|
|
|
|
if err != nil {
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 2, // An error that's your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get quota
|
|
|
|
quota, err := getQuota(message.Message.(uuid.UUID), information)
|
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report success
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 0, // Success
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: quota,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
// Get used
|
|
|
|
validate := validator.New()
|
|
|
|
err := validate.Struct(message.Message.(uuid.UUID))
|
|
|
|
if err != nil {
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 2, // An error that's your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get used
|
|
|
|
used, err := getUsed(message.Message.(uuid.UUID), information)
|
|
|
|
if err != nil {
|
|
|
|
// First contact the logger service
|
|
|
|
logFunc(err.Error(), 2, information)
|
|
|
|
|
|
|
|
// Then send the error message to the requesting service
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 1, // An error that's not your fault
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report success
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
|
|
|
ForServiceID: message.ServiceID,
|
|
|
|
MessageType: 0, // Success
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: used,
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-09-29 19:03:13 +01:00
|
|
|
// Initiate a connection to the database
|
|
|
|
// Call service ID 1 to get the database connection information
|
|
|
|
information.Outbox <- library.InterServiceMessage{
|
2024-10-02 17:07:01 +01:00
|
|
|
ServiceID: ServiceInformation.ServiceID,
|
2024-09-29 19:03:13 +01:00
|
|
|
ForServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"), // Service initialization service
|
|
|
|
MessageType: 1, // Request connection information
|
|
|
|
SentAt: time.Now(),
|
|
|
|
Message: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the response
|
|
|
|
response := <-information.Inbox
|
|
|
|
if response.MessageType == 2 {
|
|
|
|
// This is the connection information
|
|
|
|
// Set up the database connection
|
2024-10-13 19:20:19 +01:00
|
|
|
conn = response.Message.(library.Database)
|
2024-09-29 19:03:13 +01:00
|
|
|
// Create the quotas table if it doesn't exist
|
2024-10-13 19:20:19 +01:00
|
|
|
if conn.DBType == library.Sqlite {
|
|
|
|
_, err := conn.DB.Exec("CREATE TABLE IF NOT EXISTS quotas (id BLOB PRIMARY KEY, quota BIGINT)")
|
|
|
|
if err != nil {
|
|
|
|
logFunc(err.Error(), 3, information)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := conn.DB.Exec("CREATE TABLE IF NOT EXISTS quotas (id BYTEA PRIMARY KEY, quota BIGINT)")
|
|
|
|
if err != nil {
|
|
|
|
logFunc(err.Error(), 3, information)
|
|
|
|
}
|
2024-09-29 19:03:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is an error message
|
|
|
|
// Log the error message to the logger service
|
|
|
|
logFunc(response.Message.(error).Error(), 3, information)
|
|
|
|
}
|
2024-09-28 19:41:34 +01:00
|
|
|
}
|