feat: add activity logs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/middleware"
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/platform"
|
||||
"acc-server-manager/local/repository"
|
||||
@@ -15,6 +16,7 @@ type ServiceControlService struct {
|
||||
repository *repository.ServiceControlRepository
|
||||
serverRepository *repository.ServerRepository
|
||||
serverService *ServerService
|
||||
activityLog *ActivityLogService
|
||||
statusCache *model.ServerStatusCache
|
||||
runtime platform.ServerRuntime
|
||||
}
|
||||
@@ -23,11 +25,13 @@ func NewServiceControlService(
|
||||
repository *repository.ServiceControlRepository,
|
||||
serverRepository *repository.ServerRepository,
|
||||
runtime platform.ServerRuntime,
|
||||
activityLog *ActivityLogService,
|
||||
) *ServiceControlService {
|
||||
return &ServiceControlService{
|
||||
repository: repository,
|
||||
serverRepository: serverRepository,
|
||||
runtime: runtime,
|
||||
activityLog: activityLog,
|
||||
statusCache: model.NewServerStatusCache(model.CacheConfig{
|
||||
ExpirationTime: 30 * time.Second,
|
||||
ThrottleTime: 5 * time.Second,
|
||||
@@ -36,6 +40,27 @@ func NewServiceControlService(
|
||||
}
|
||||
}
|
||||
|
||||
// getServerFromCtx resolves the server from ctx locals (serverId or service name).
|
||||
func (as *ServiceControlService) getServerFromCtx(ctx *fiber.Ctx) (*model.Server, error) {
|
||||
serviceName, ok := ctx.Locals("service").(string)
|
||||
if !ok || serviceName == "" {
|
||||
serverId, ok2 := ctx.Locals("serverId").(string)
|
||||
if !ok2 || serverId == "" {
|
||||
return nil, errors.New("service name missing")
|
||||
}
|
||||
return as.serverRepository.GetByID(ctx.UserContext(), serverId)
|
||||
}
|
||||
return as.serverRepository.GetFirstByServiceName(ctx.UserContext(), serviceName)
|
||||
}
|
||||
|
||||
func extractActor(ctx *fiber.Ctx) (actorID, actorUsername string) {
|
||||
userInfo, _ := ctx.Locals("userInfo").(*middleware.CachedUserInfo)
|
||||
if userInfo != nil {
|
||||
return userInfo.UserID, userInfo.Username
|
||||
}
|
||||
return "", "system"
|
||||
}
|
||||
|
||||
func (as *ServiceControlService) SetServerService(serverService *ServerService) {
|
||||
as.serverService = serverService
|
||||
}
|
||||
@@ -61,10 +86,11 @@ func (as *ServiceControlService) GetStatus(ctx *fiber.Ctx) (string, error) {
|
||||
}
|
||||
|
||||
func (as *ServiceControlService) ServiceControlStartServer(ctx *fiber.Ctx) (string, error) {
|
||||
serviceName, err := as.GetServiceName(ctx)
|
||||
server, err := as.getServerFromCtx(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
serviceName := server.ServiceName
|
||||
|
||||
as.statusCache.UpdateStatus(serviceName, model.StatusStarting)
|
||||
|
||||
@@ -79,14 +105,27 @@ 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 + `"}`,
|
||||
})
|
||||
}()
|
||||
|
||||
return status.String(), nil
|
||||
}
|
||||
|
||||
func (as *ServiceControlService) ServiceControlStopServer(ctx *fiber.Ctx) (string, error) {
|
||||
serviceName, err := as.GetServiceName(ctx)
|
||||
server, err := as.getServerFromCtx(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
serviceName := server.ServiceName
|
||||
|
||||
as.statusCache.UpdateStatus(serviceName, model.StatusStopping)
|
||||
|
||||
@@ -101,14 +140,27 @@ 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 + `"}`,
|
||||
})
|
||||
}()
|
||||
|
||||
return status.String(), nil
|
||||
}
|
||||
|
||||
func (as *ServiceControlService) ServiceControlRestartServer(ctx *fiber.Ctx) (string, error) {
|
||||
serviceName, err := as.GetServiceName(ctx)
|
||||
server, err := as.getServerFromCtx(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
serviceName := server.ServiceName
|
||||
|
||||
as.statusCache.UpdateStatus(serviceName, model.StatusRestarting)
|
||||
|
||||
@@ -123,6 +175,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 + `"}`,
|
||||
})
|
||||
}()
|
||||
|
||||
return status.String(), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user