Fixed a bug where setting compression would crash the server on local routes, fixed some useless never-will-happen eventualities

Signed-off-by: arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-11-04 17:09:45 +00:00
parent eebe3763f5
commit d40bc7dc8e
2 changed files with 16 additions and 49 deletions

50
main.go
View File

@ -644,7 +644,7 @@ func tryAuthAccess(message library.InterServiceMessage) {
service, ok := activeServices[uuid.MustParse("00000000-0000-0000-0000-000000000004")] service, ok := activeServices[uuid.MustParse("00000000-0000-0000-0000-000000000004")]
if ok { if ok {
service.Inbox <- message service.Inbox <- message
} else if !ok { } else {
// Send error message // Send error message
service, ok := activeServices[message.ServiceID] service, ok := activeServices[message.ServiceID]
if ok { if ok {
@ -660,22 +660,6 @@ func tryAuthAccess(message library.InterServiceMessage) {
slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.") slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.")
os.Exit(1) os.Exit(1)
} }
} else {
// Send error message
service, ok := activeServices[message.ServiceID]
if ok {
service.Inbox <- library.InterServiceMessage{
ServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
ForServiceID: message.ServiceID,
MessageType: 1,
SentAt: time.Now(),
Message: errors.New("authentication service not yet available"),
}
} else {
// This should never happen
slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.")
os.Exit(1)
}
} }
} else { } else {
// Send error message // Send error message
@ -704,7 +688,7 @@ func tryStorageAccess(message library.InterServiceMessage) {
service, ok := activeServices[uuid.MustParse("00000000-0000-0000-0000-000000000003")] service, ok := activeServices[uuid.MustParse("00000000-0000-0000-0000-000000000003")]
if ok { if ok {
service.Inbox <- message service.Inbox <- message
} else if !ok { } else {
// Send error message // Send error message
service, ok := activeServices[message.ServiceID] service, ok := activeServices[message.ServiceID]
if ok { if ok {
@ -720,22 +704,6 @@ func tryStorageAccess(message library.InterServiceMessage) {
slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.") slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.")
os.Exit(1) os.Exit(1)
} }
} else {
// Send error message
service, ok := activeServices[message.ServiceID]
if ok {
service.Inbox <- library.InterServiceMessage{
ServiceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
ForServiceID: message.ServiceID,
MessageType: 1,
SentAt: time.Now(),
Message: errors.New("blob storage is not yet available"),
}
} else {
// This should never happen
slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.")
os.Exit(1)
}
} }
} else { } else {
// Send error message // Send error message
@ -915,19 +883,18 @@ func parseConfig(path string) Config {
func iterateThroughSubdomains(globalOutbox chan library.InterServiceMessage) { func iterateThroughSubdomains(globalOutbox chan library.InterServiceMessage) {
for _, route := range config.Routes { for _, route := range config.Routes {
var subdomainRouter *chi.Mux
// Create the subdomain router
if route.Compression.Level != 0 { if route.Compression.Level != 0 {
compression[route.Subdomain] = CompressionSettings{ compression[route.Subdomain] = CompressionSettings{
Level: int(route.Compression.Level), Level: int(route.Compression.Level),
Algorithm: route.Compression.Algorithm, Algorithm: route.Compression.Algorithm,
} }
} else { }
subdomainRouter = chi.NewRouter()
// Create the subdomain router
subdomainRouter := chi.NewRouter()
subdomainRouter.NotFound(func(w http.ResponseWriter, r *http.Request) { subdomainRouter.NotFound(func(w http.ResponseWriter, r *http.Request) {
serverError(w, 404) serverError(w, 404)
}) })
}
subdomains[route.Subdomain] = subdomainRouter subdomains[route.Subdomain] = subdomainRouter
subdomains[route.Subdomain].Use(logger) subdomains[route.Subdomain].Use(logger)
@ -1158,6 +1125,11 @@ func main() {
// Start the HTTP server // Start the HTTP server
err = http.ListenAndServe(config.Global.IP+":"+config.Global.HTTPPort, http.HandlerFunc(hostRouter)) err = http.ListenAndServe(config.Global.IP+":"+config.Global.HTTPPort, http.HandlerFunc(hostRouter))
if err != nil {
slog.Error("Error starting server: " + err.Error()) slog.Error("Error starting server: " + err.Error())
} else {
// This should never happen
slog.Error("Bit flip error: Impossible service ID. Move away from radiation or use ECC memory.")
}
os.Exit(1) os.Exit(1)
} }

View File

@ -189,11 +189,6 @@ func Main(information library.ServiceInitializationInformation) {
identifier := information.Configuration["identifier"].(string) identifier := information.Configuration["identifier"].(string)
adminKey := information.Configuration["adminKey"].(string) adminKey := information.Configuration["adminKey"].(string)
var err error
if err != nil {
logFunc(err.Error(), 3, information)
}
// Initiate a connection to the database // Initiate a connection to the database
// Call service ID 1 to get the database connection information // Call service ID 1 to get the database connection information
information.Outbox <- library.InterServiceMessage{ information.Outbox <- library.InterServiceMessage{
@ -246,7 +241,7 @@ func Main(information library.ServiceInitializationInformation) {
} }
} }
// Set up the in-memory cache // Set up the in-memory cache
mem, err = sql.Open("sqlite3", "file:"+ServiceInformation.ServiceID.String()+"?mode=memory&cache=shared") mem, err := sql.Open("sqlite3", "file:"+ServiceInformation.ServiceID.String()+"?mode=memory&cache=shared")
if err != nil { if err != nil {
logFunc(err.Error(), 3, information) logFunc(err.Error(), 3, information)
} }
@ -295,7 +290,7 @@ func Main(information library.ServiceInitializationInformation) {
// Set up the signing keys // Set up the signing keys
// Check if the global table has the keys // Check if the global table has the keys
err = conn.DB.QueryRow("SELECT key FROM global LIMIT 1").Scan(&privateKey) err := conn.DB.QueryRow("SELECT key FROM global LIMIT 1").Scan(&privateKey)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
// Generate a new key // Generate a new key