From 14aa8a84e53e6043fa31a0e34cf122d02677624f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Sun, 22 Mar 2026 13:54:43 +0100 Subject: [PATCH] feat: add logging to existing services --- local/middleware/auth.go | 2 + local/service/config.go | 63 ++++++++++--------- local/service/service_control.go | 76 ++++++++++++----------- tests/unit/service/config_service_test.go | 14 ++--- 4 files changed, 82 insertions(+), 73 deletions(-) diff --git a/local/middleware/auth.go b/local/middleware/auth.go index 7abc5fb..fbb0ec3 100755 --- a/local/middleware/auth.go +++ b/local/middleware/auth.go @@ -115,6 +115,7 @@ func (m *AuthMiddleware) AuthenticateWithHandler(jwtHandler *jwt.JWTHandler, isO if os.Getenv("TESTING_ENV") == "true" { userInfo := CachedUserInfo{UserID: uuid.New().String(), Username: "test@example.com", RoleName: "Admin", Permissions: make(map[string]bool), CachedAt: time.Now()} ctx.Locals("userID", userInfo.UserID) + ctx.Locals("actorUsername", userInfo.Username) ctx.Locals("userInfo", userInfo) ctx.Locals("authTime", time.Now()) } else { @@ -127,6 +128,7 @@ func (m *AuthMiddleware) AuthenticateWithHandler(jwtHandler *jwt.JWTHandler, isO } ctx.Locals("userID", claims.UserID) + ctx.Locals("actorUsername", userInfo.Username) ctx.Locals("userInfo", userInfo) ctx.Locals("authTime", time.Now()) } diff --git a/local/service/config.go b/local/service/config.go index bc5a841..9a190a1 100755 --- a/local/service/config.go +++ b/local/service/config.go @@ -1,7 +1,6 @@ package service import ( - "acc-server-manager/local/middleware" "acc-server-manager/local/model" "acc-server-manager/local/repository" "acc-server-manager/local/utl/common" @@ -123,11 +122,10 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati {SettingsJson, req.Settings}, } - userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) - actorID, actorUsername := "", "system" - if userInfo != nil { - actorID = userInfo.UserID - actorUsername = userInfo.Username + actorID, _ := ctx.Locals("userID").(string) + actorUsername, _ := ctx.Locals("actorUsername").(string) + if actorUsername == "" { + actorUsername = "system" } var results []*model.Config @@ -148,16 +146,18 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati }) results = append(results, result) - details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData) - go func(details string) { - _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverUUID, - UserID: actorID, - Username: actorUsername, - Action: model.ActionConfigUpdate, - Details: details, - }) - }(details) + if as.activityLog != nil { + details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData) + go func(details string) { + _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: serverUUID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionConfigUpdate, + Details: details, + }) + }(details) + } } if len(results) > 0 { @@ -173,11 +173,10 @@ func (as *ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface configFile := ctx.Params("file") override := ctx.QueryBool("override", false) - userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) - actorID, actorUsername := "", "system" - if userInfo != nil { - actorID = userInfo.UserID - actorUsername = userInfo.Username + actorID, _ := ctx.Locals("userID").(string) + actorUsername, _ := ctx.Locals("actorUsername").(string) + if actorUsername == "" { + actorUsername = "system" } return as.updateConfigInternal(ctx.UserContext(), serverID, configFile, body, override, actorID, actorUsername) @@ -260,16 +259,18 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri as.configCache.InvalidateServerCache(serverID) as.serverService.StartAccServerRuntime(server) - details := BuildConfigDetails(configFile, oldDataUTF8, newData) - go func() { - _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: serverUUID, - UserID: actorID, - Username: actorUsername, - Action: model.ActionConfigUpdate, - Details: details, - }) - }() + if as.activityLog != nil { + details := BuildConfigDetails(configFile, oldDataUTF8, newData) + go func() { + _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: serverUUID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionConfigUpdate, + Details: details, + }) + }() + } return as.repository.UpdateConfig(ctx, &model.Config{ ServerID: serverUUID, diff --git a/local/service/service_control.go b/local/service/service_control.go index 95c6816..89ad683 100755 --- a/local/service/service_control.go +++ b/local/service/service_control.go @@ -1,7 +1,6 @@ package service import ( - "acc-server-manager/local/middleware" "acc-server-manager/local/model" "acc-server-manager/local/platform" "acc-server-manager/local/repository" @@ -54,11 +53,12 @@ func (as *ServiceControlService) getServerFromCtx(ctx *fiber.Ctx) (*model.Server } func extractActor(ctx *fiber.Ctx) (actorID, actorUsername string) { - userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) - if userInfo != nil { - return userInfo.UserID, userInfo.Username + actorID, _ = ctx.Locals("userID").(string) + actorUsername, _ = ctx.Locals("actorUsername").(string) + if actorUsername == "" { + actorUsername = "system" } - return "", "system" + return } func (as *ServiceControlService) SetServerService(serverService *ServerService) { @@ -106,16 +106,18 @@ func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (stri status := model.ParseServiceStatus(statusStr) as.statusCache.UpdateStatus(serviceName, status) - actorID, actorUsername := extractActor(ctx) - go func() { - _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, - UserID: actorID, - Username: actorUsername, - Action: model.ActionServerStart, - Details: `{"service":"` + serviceName + `"}`, - }) - }() + if as.activityLog != nil { + actorID, actorUsername := extractActor(ctx) + go func() { + _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: server.ID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionServerStart, + Details: `{"service":"` + serviceName + `"}`, + }) + }() + } return status.String(), nil } @@ -141,16 +143,18 @@ func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (strin status := model.ParseServiceStatus(statusStr) as.statusCache.UpdateStatus(serviceName, status) - actorID, actorUsername := extractActor(ctx) - go func() { - _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, - UserID: actorID, - Username: actorUsername, - Action: model.ActionServerStop, - Details: `{"service":"` + serviceName + `"}`, - }) - }() + if as.activityLog != nil { + actorID, actorUsername := extractActor(ctx) + go func() { + _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: server.ID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionServerStop, + Details: `{"service":"` + serviceName + `"}`, + }) + }() + } return status.String(), nil } @@ -176,16 +180,18 @@ func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (st status := model.ParseServiceStatus(statusStr) as.statusCache.UpdateStatus(serviceName, status) - actorID, actorUsername := extractActor(ctx) - go func() { - _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ - ServerID: server.ID, - UserID: actorID, - Username: actorUsername, - Action: model.ActionServerRestart, - Details: `{"service":"` + serviceName + `"}`, - }) - }() + if as.activityLog != nil { + actorID, actorUsername := extractActor(ctx) + go func() { + _ = as.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: server.ID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionServerRestart, + Details: `{"service":"` + serviceName + `"}`, + }) + }() + } return status.String(), nil } diff --git a/tests/unit/service/config_service_test.go b/tests/unit/service/config_service_test.go index ecc3d41..5169a0d 100755 --- a/tests/unit/service/config_service_test.go +++ b/tests/unit/service/config_service_test.go @@ -20,7 +20,7 @@ func TestConfigService_GetConfiguration_ValidFile(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) config, err := configService.GetConfiguration(helper.TestData.Server) tests.AssertNoError(t, err) @@ -43,7 +43,7 @@ func TestConfigService_GetEventConfig_ValidFile(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) eventConfig, err := configService.GetEventConfig(helper.TestData.Server) tests.AssertNoError(t, err) @@ -73,7 +73,7 @@ func TestConfigService_SaveConfiguration_Success(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) newConfig := &model.Configuration{ UdpPort: model.IntString(9999), @@ -115,7 +115,7 @@ func TestConfigService_LoadConfigs_Success(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) configs, err := configService.LoadConfigs(helper.TestData.Server) tests.AssertNoError(t, err) @@ -142,7 +142,7 @@ func TestConfigService_MalformedJSON(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) config, err := configService.GetConfiguration(helper.TestData.Server) if err == nil { @@ -245,7 +245,7 @@ func TestConfigService_Caching_Configuration(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) config1, err := configService.GetConfiguration(helper.TestData.Server) tests.AssertNoError(t, err) @@ -276,7 +276,7 @@ func TestConfigService_Caching_EventConfig(t *testing.T) { configRepo := repository.NewConfigRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB) - configService := service.NewConfigService(configRepo, serverRepo) + configService := service.NewConfigService(configRepo, serverRepo, nil) event1, err := configService.GetEventConfig(helper.TestData.Server) tests.AssertNoError(t, err)