feat: add logging to existing services

This commit is contained in:
2026-03-22 13:54:43 +01:00
parent e96b8bca12
commit 14aa8a84e5
4 changed files with 82 additions and 73 deletions

View File

@@ -115,6 +115,7 @@ func (m *AuthMiddleware) AuthenticateWithHandler(jwtHandler *jwt.JWTHandler, isO
if os.Getenv("TESTING_ENV") == "true" { 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()} 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("userID", userInfo.UserID)
ctx.Locals("actorUsername", userInfo.Username)
ctx.Locals("userInfo", userInfo) ctx.Locals("userInfo", userInfo)
ctx.Locals("authTime", time.Now()) ctx.Locals("authTime", time.Now())
} else { } else {
@@ -127,6 +128,7 @@ func (m *AuthMiddleware) AuthenticateWithHandler(jwtHandler *jwt.JWTHandler, isO
} }
ctx.Locals("userID", claims.UserID) ctx.Locals("userID", claims.UserID)
ctx.Locals("actorUsername", userInfo.Username)
ctx.Locals("userInfo", userInfo) ctx.Locals("userInfo", userInfo)
ctx.Locals("authTime", time.Now()) ctx.Locals("authTime", time.Now())
} }

View File

@@ -1,7 +1,6 @@
package service package service
import ( import (
"acc-server-manager/local/middleware"
"acc-server-manager/local/model" "acc-server-manager/local/model"
"acc-server-manager/local/repository" "acc-server-manager/local/repository"
"acc-server-manager/local/utl/common" "acc-server-manager/local/utl/common"
@@ -123,11 +122,10 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati
{SettingsJson, req.Settings}, {SettingsJson, req.Settings},
} }
userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) actorID, _ := ctx.Locals("userID").(string)
actorID, actorUsername := "", "system" actorUsername, _ := ctx.Locals("actorUsername").(string)
if userInfo != nil { if actorUsername == "" {
actorID = userInfo.UserID actorUsername = "system"
actorUsername = userInfo.Username
} }
var results []*model.Config var results []*model.Config
@@ -148,6 +146,7 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati
}) })
results = append(results, result) results = append(results, result)
if as.activityLog != nil {
details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData) details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData)
go func(details string) { go func(details string) {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
@@ -159,6 +158,7 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati
}) })
}(details) }(details)
} }
}
if len(results) > 0 { if len(results) > 0 {
as.configCache.InvalidateServerCache(serverID) as.configCache.InvalidateServerCache(serverID)
@@ -173,11 +173,10 @@ func (as *ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface
configFile := ctx.Params("file") configFile := ctx.Params("file")
override := ctx.QueryBool("override", false) override := ctx.QueryBool("override", false)
userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) actorID, _ := ctx.Locals("userID").(string)
actorID, actorUsername := "", "system" actorUsername, _ := ctx.Locals("actorUsername").(string)
if userInfo != nil { if actorUsername == "" {
actorID = userInfo.UserID actorUsername = "system"
actorUsername = userInfo.Username
} }
return as.updateConfigInternal(ctx.UserContext(), serverID, configFile, body, override, actorID, actorUsername) return as.updateConfigInternal(ctx.UserContext(), serverID, configFile, body, override, actorID, actorUsername)
@@ -260,6 +259,7 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri
as.configCache.InvalidateServerCache(serverID) as.configCache.InvalidateServerCache(serverID)
as.serverService.StartAccServerRuntime(server) as.serverService.StartAccServerRuntime(server)
if as.activityLog != nil {
details := BuildConfigDetails(configFile, oldDataUTF8, newData) details := BuildConfigDetails(configFile, oldDataUTF8, newData)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
@@ -270,6 +270,7 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri
Details: details, Details: details,
}) })
}() }()
}
return as.repository.UpdateConfig(ctx, &model.Config{ return as.repository.UpdateConfig(ctx, &model.Config{
ServerID: serverUUID, ServerID: serverUUID,

View File

@@ -1,7 +1,6 @@
package service package service
import ( import (
"acc-server-manager/local/middleware"
"acc-server-manager/local/model" "acc-server-manager/local/model"
"acc-server-manager/local/platform" "acc-server-manager/local/platform"
"acc-server-manager/local/repository" "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) { func extractActor(ctx *fiber.Ctx) (actorID, actorUsername string) {
userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo) actorID, _ = ctx.Locals("userID").(string)
if userInfo != nil { actorUsername, _ = ctx.Locals("actorUsername").(string)
return userInfo.UserID, userInfo.Username if actorUsername == "" {
actorUsername = "system"
} }
return "", "system" return
} }
func (as *ServiceControlService) SetServerService(serverService *ServerService) { func (as *ServiceControlService) SetServerService(serverService *ServerService) {
@@ -106,6 +106,7 @@ func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (stri
status := model.ParseServiceStatus(statusStr) status := model.ParseServiceStatus(statusStr)
as.statusCache.UpdateStatus(serviceName, status) as.statusCache.UpdateStatus(serviceName, status)
if as.activityLog != nil {
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
@@ -116,6 +117,7 @@ func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (stri
Details: `{"service":"` + serviceName + `"}`, Details: `{"service":"` + serviceName + `"}`,
}) })
}() }()
}
return status.String(), nil return status.String(), nil
} }
@@ -141,6 +143,7 @@ func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (strin
status := model.ParseServiceStatus(statusStr) status := model.ParseServiceStatus(statusStr)
as.statusCache.UpdateStatus(serviceName, status) as.statusCache.UpdateStatus(serviceName, status)
if as.activityLog != nil {
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
@@ -151,6 +154,7 @@ func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (strin
Details: `{"service":"` + serviceName + `"}`, Details: `{"service":"` + serviceName + `"}`,
}) })
}() }()
}
return status.String(), nil return status.String(), nil
} }
@@ -176,6 +180,7 @@ func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (st
status := model.ParseServiceStatus(statusStr) status := model.ParseServiceStatus(statusStr)
as.statusCache.UpdateStatus(serviceName, status) as.statusCache.UpdateStatus(serviceName, status)
if as.activityLog != nil {
actorID, actorUsername := extractActor(ctx) actorID, actorUsername := extractActor(ctx)
go func() { go func() {
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{ _ = as.activityLog.Log(context.Background(), &model.ActivityLog{
@@ -186,6 +191,7 @@ func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (st
Details: `{"service":"` + serviceName + `"}`, Details: `{"service":"` + serviceName + `"}`,
}) })
}() }()
}
return status.String(), nil return status.String(), nil
} }

View File

@@ -20,7 +20,7 @@ func TestConfigService_GetConfiguration_ValidFile(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) config, err := configService.GetConfiguration(helper.TestData.Server)
tests.AssertNoError(t, err) tests.AssertNoError(t, err)
@@ -43,7 +43,7 @@ func TestConfigService_GetEventConfig_ValidFile(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) eventConfig, err := configService.GetEventConfig(helper.TestData.Server)
tests.AssertNoError(t, err) tests.AssertNoError(t, err)
@@ -73,7 +73,7 @@ func TestConfigService_SaveConfiguration_Success(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(helper.DB) serverRepo := repository.NewServerRepository(helper.DB)
configService := service.NewConfigService(configRepo, serverRepo) configService := service.NewConfigService(configRepo, serverRepo, nil)
newConfig := &model.Configuration{ newConfig := &model.Configuration{
UdpPort: model.IntString(9999), UdpPort: model.IntString(9999),
@@ -115,7 +115,7 @@ func TestConfigService_LoadConfigs_Success(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) configs, err := configService.LoadConfigs(helper.TestData.Server)
tests.AssertNoError(t, err) tests.AssertNoError(t, err)
@@ -142,7 +142,7 @@ func TestConfigService_MalformedJSON(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) config, err := configService.GetConfiguration(helper.TestData.Server)
if err == nil { if err == nil {
@@ -245,7 +245,7 @@ func TestConfigService_Caching_Configuration(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) config1, err := configService.GetConfiguration(helper.TestData.Server)
tests.AssertNoError(t, err) tests.AssertNoError(t, err)
@@ -276,7 +276,7 @@ func TestConfigService_Caching_EventConfig(t *testing.T) {
configRepo := repository.NewConfigRepository(helper.DB) configRepo := repository.NewConfigRepository(helper.DB)
serverRepo := repository.NewServerRepository(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) event1, err := configService.GetEventConfig(helper.TestData.Server)
tests.AssertNoError(t, err) tests.AssertNoError(t, err)