Skip to content

Commit 829e6c1

Browse files
authored
Merge pull request #73 from MonkyMars/monitor/services
Ft: Refactor codebase for health logging and different routing stragety
2 parents 725c874 + 198d42f commit 829e6c1

41 files changed

Lines changed: 4500 additions & 699 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ bin/
2323
# Exclude coverage reports
2424
*.out
2525

26+
# Exclude IDE specific files
27+
.vscode/
28+
.idea/

apps/server/.env.example

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ CACHE_MAX_RETRIES=3
5858
CACHE_MIN_RETRY_BACKOFF=8ms
5959
CACHE_MAX_RETRY_BACKOFF=512ms
6060

61-
# ===================
62-
# Audit Settings
63-
# ===================
64-
AUDIT_BATCH_SIZE=50
65-
AUDIT_FLUSH_TIME=20s
66-
AUDIT_CHANNEL_SIZE=1000
67-
AUDIT_MAX_RETRIES=3
68-
AUDIT_MAX_FAILURES=10
69-
AUDIT_RETENTION_DAYS=90
70-
AUDIT_ENABLED=true
71-
7261
# ===================
7362
# Google Settings
7463
# ===================
@@ -88,9 +77,22 @@ CORS_ALLOW_CREDENTIALS=true
8877
# Audit Settings
8978
# ===================
9079
AUDIT_BATCH_SIZE=50
91-
AUDIT_FLUSH_TIME=20s
9280
AUDIT_CHANNEL_SIZE=1000
93-
AUDIT_MAX_RETRIES=3
81+
AUDIT_ENABLED=true
82+
AUDIT_FLUSH_TIME=30s
9483
AUDIT_MAX_FAILURES=10
84+
AUDIT_MAX_RETRIES=3
9585
AUDIT_RETENTION_DAYS=90
96-
AUDIT_ENABLED=true
86+
AUDIT_RETRY_DELAY=3s
87+
88+
# ===================
89+
# Health Middleware Settings
90+
# ===================
91+
HEALTH_BATCH_SIZE=50
92+
HEALTH_CHANNEL_SIZE=1000
93+
HEALTH_ENABLED=true
94+
HEALTH_FLUSH_TIME=10m
95+
HEALTH_MAX_FAILURES=10
96+
HEALTH_MAX_RETRIES=3
97+
HEALTH_RETENTION_DAYS=21
98+
HEALTH_RETRY_DELAY=1m

apps/server/api/internal/app.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/MonkyMars/PWS/lib"
99
"github.com/MonkyMars/PWS/services"
1010
"github.com/MonkyMars/PWS/types"
11-
"github.com/MonkyMars/PWS/workers"
1211
"github.com/gofiber/fiber/v3"
1312
)
1413

@@ -17,7 +16,13 @@ var (
1716
requestCount int64
1817
)
1918

20-
func GetSystemHealth(c fiber.Ctx) error {
19+
type AppRoutes struct{}
20+
21+
func NewAppRoutes() *AppRoutes {
22+
return &AppRoutes{}
23+
}
24+
25+
func (ar *AppRoutes) GetSystemHealth(c fiber.Ctx) error {
2126
// Memory stats
2227
var memStats runtime.MemStats
2328
runtime.ReadMemStats(&memStats)
@@ -39,10 +44,7 @@ func GetSystemHealth(c fiber.Ctx) error {
3944
Status: status,
4045
Message: message,
4146
ApplicationUptime: lib.GetUptimeString(appStartTime),
42-
Services: map[string]string{
43-
"database": dbStatus,
44-
"api": "ok",
45-
},
47+
DatabaseStatus: dbStatus,
4648
Metrics: types.HealthMetrics{
4749
MemoryUsageMB: float64(memStats.Alloc) / 1024 / 1024,
4850
GoRoutines: runtime.NumGoroutine(),
@@ -51,34 +53,10 @@ func GetSystemHealth(c fiber.Ctx) error {
5153
})
5254
}
5355

54-
// GetAuditHealth returns the health status of the audit logging system
55-
func GetAuditHealth(c fiber.Ctx) error {
56-
healthStatus := workers.HealthStatus()
57-
58-
status := "ok"
59-
message := "Audit system operational"
60-
61-
if !healthStatus["is_healthy"].(bool) {
62-
status = "degraded"
63-
message = "Audit system experiencing issues"
64-
}
65-
66-
if !healthStatus["worker_running"].(bool) {
67-
status = "error"
68-
message = "Audit worker not running"
69-
}
70-
71-
return response.Success(c, map[string]any{
72-
"status": status,
73-
"message": message,
74-
"details": healthStatus,
75-
})
76-
}
77-
7856
// TODO: Add authentication middleware to protect this endpoint in production
7957
// Thus making sure only authorized admins can access it
8058
// Currently it's only available in development mode because of this issue
81-
func GetDatabaseHealth(c fiber.Ctx) error {
59+
func (ar *AppRoutes) GetDatabaseHealth(c fiber.Ctx) error {
8260
now := time.Now()
8361
if err := services.Ping(); err != nil {
8462
return response.ServiceUnavailable(c, "Database connection error: "+err.Error())
@@ -91,6 +69,11 @@ func GetDatabaseHealth(c fiber.Ctx) error {
9169
})
9270
}
9371

94-
func NotFoundHandler(c fiber.Ctx) error {
95-
return response.NotFound(c, "The requested resource was not found.")
72+
func (ar *AppRoutes) GetLogs(c fiber.Ctx) error {
73+
auditService := services.NewAuditService()
74+
logs, err := auditService.GetLogs()
75+
if err != nil {
76+
return response.InternalServerError(c, "Failed to retrieve audit logs")
77+
}
78+
return response.Success(c, logs)
9679
}

apps/server/api/internal/audit.go

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package auth
22

33
import (
44
"errors"
@@ -13,8 +13,14 @@ import (
1313
"github.com/gofiber/fiber/v3"
1414
)
1515

16+
type AuthRoutes struct{}
17+
18+
func NewAuthRoutes() *AuthRoutes {
19+
return &AuthRoutes{}
20+
}
21+
1622
// Login handles user authentication and returns JWT tokens
17-
func Login(c fiber.Ctx) error {
23+
func (ar *AuthRoutes) Login(c fiber.Ctx) error {
1824
logger := config.SetupLogger()
1925

2026
var authRequest types.AuthRequest
@@ -81,7 +87,7 @@ func Login(c fiber.Ctx) error {
8187
}
8288

8389
// Register handles user registration and returns JWT tokens
84-
func Register(c fiber.Ctx) error {
90+
func (ar *AuthRoutes) Register(c fiber.Ctx) error {
8591
logger := config.SetupLogger()
8692

8793
var registerRequest types.RegisterRequest
@@ -169,7 +175,7 @@ func Register(c fiber.Ctx) error {
169175
}
170176

171177
// RefreshToken handles token refresh using refresh tokens
172-
func RefreshToken(c fiber.Ctx) error {
178+
func (ar *AuthRoutes) RefreshToken(c fiber.Ctx) error {
173179
logger := config.SetupLogger()
174180

175181
token := c.Cookies(lib.RefreshTokenCookieName)
@@ -203,7 +209,7 @@ func RefreshToken(c fiber.Ctx) error {
203209
}
204210

205211
// Me returns the current authenticated user's information
206-
func Me(c fiber.Ctx) error {
212+
func (ar *AuthRoutes) Me(c fiber.Ctx) error {
207213
logger := config.SetupLogger()
208214

209215
claimsInterface := c.Locals("claims")
@@ -238,7 +244,7 @@ func Me(c fiber.Ctx) error {
238244
}
239245

240246
// Logout handles user logout with graceful handling of missing/invalid tokens
241-
func Logout(c fiber.Ctx) error {
247+
func (ar *AuthRoutes) Logout(c fiber.Ctx) error {
242248
logger := config.SetupLogger()
243249

244250
accessToken := c.Cookies(lib.AccessTokenCookieName)

apps/server/api/internal/google_oauth.go renamed to apps/server/api/internal/auth/google_oauth.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package auth
22

33
import (
44
"github.com/MonkyMars/PWS/api/response"
@@ -10,7 +10,7 @@ import (
1010

1111
// GoogleAuthURL handles getting the Google OAuth authorization URL
1212
// GET /auth/google/url
13-
func GoogleAuthURL(c fiber.Ctx) error {
13+
func (ar *AuthRoutes) GoogleAuthURL(c fiber.Ctx) error {
1414
logger := config.SetupLogger()
1515

1616
// Get user from auth middleware
@@ -43,7 +43,7 @@ func GoogleAuthURL(c fiber.Ctx) error {
4343

4444
// GoogleAuthCallback handles the OAuth callback from Google
4545
// GET /auth/google/callback
46-
func GoogleAuthCallback(c fiber.Ctx) error {
46+
func (ar *AuthRoutes) GoogleAuthCallback(c fiber.Ctx) error {
4747
logger := config.SetupLogger()
4848

4949
state := c.Query("state")
@@ -70,7 +70,7 @@ func GoogleAuthCallback(c fiber.Ctx) error {
7070

7171
// GoogleAccessToken handles getting a fresh Google access token
7272
// GET /auth/google/access-token
73-
func GoogleAccessToken(c fiber.Ctx) error {
73+
func (ar *AuthRoutes) GoogleAccessToken(c fiber.Ctx) error {
7474
logger := config.SetupLogger()
7575

7676
// Get user from auth middleware
@@ -107,7 +107,7 @@ func GoogleAccessToken(c fiber.Ctx) error {
107107

108108
// GoogleUnlink handles unlinking a user's Google account
109109
// DELETE /auth/google/unlink
110-
func GoogleUnlink(c fiber.Ctx) error {
110+
func (ar *AuthRoutes) GoogleUnlink(c fiber.Ctx) error {
111111
logger := config.SetupLogger()
112112

113113
// Get user from auth middleware
@@ -138,7 +138,7 @@ func GoogleUnlink(c fiber.Ctx) error {
138138

139139
// GoogleLinkStatus checks if user has linked their Google account
140140
// GET /auth/google/status
141-
func GoogleLinkStatus(c fiber.Ctx) error {
141+
func (ar *AuthRoutes) GoogleLinkStatus(c fiber.Ctx) error {
142142
logger := config.SetupLogger()
143143

144144
// Get user from auth middleware
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package content
2+
3+
type ContentRoutes struct{}
4+
5+
func NewContentRoutes() *ContentRoutes {
6+
return &ContentRoutes{}
7+
}

apps/server/api/internal/content/retrieve.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package files
1+
package content
22

33
import (
44
"fmt"
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// /files/:fileId
12-
func GetSingleFile(c fiber.Ctx) error {
12+
func (cr *ContentRoutes) GetSingleFile(c fiber.Ctx) error {
1313
// Get fileId from URL parameters
1414
fileID := c.Params("fileId")
1515
if fileID == "" {
@@ -31,7 +31,7 @@ func GetSingleFile(c fiber.Ctx) error {
3131
}
3232

3333
// /files/subject/:subjectId/folder/:folderId
34-
func GetFilesBySubject(c fiber.Ctx) error {
34+
func (cr *ContentRoutes) GetFilesBySubject(c fiber.Ctx) error {
3535
// Get subjectId from URL parameters
3636
subjectId := c.Params("subjectId")
3737
if subjectId == "" {
@@ -64,7 +64,7 @@ func GetFilesBySubject(c fiber.Ctx) error {
6464
return response.Paginated(c, items, len(files), 1, len(files))
6565
}
6666

67-
func GetFoldersBySubjectParent(c fiber.Ctx) error {
67+
func (cr *ContentRoutes) GetFoldersBySubjectParent(c fiber.Ctx) error {
6868
subjectId := c.Params("subjectId")
6969
if subjectId == "" {
7070
return response.BadRequest(c, "subjectId parameter is required")

apps/server/api/internal/content/upload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package files
1+
package content
22

33
import (
44
"fmt"
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// /files/upload/single
17-
func UploadSingleFile(c fiber.Ctx) error {
17+
func (cr *ContentRoutes) UploadSingleFile(c fiber.Ctx) error {
1818
claimsInterface := c.Locals("claims")
1919

2020
if claimsInterface == nil {
@@ -65,7 +65,7 @@ func UploadSingleFile(c fiber.Ctx) error {
6565
return response.Created(c, data.Single)
6666
}
6767

68-
func UploadMultipleFiles(c fiber.Ctx) error {
68+
func (cr *ContentRoutes) UploadMultipleFiles(c fiber.Ctx) error {
6969
claimsInterface := c.Locals("claims")
7070

7171
if claimsInterface == nil {

apps/server/api/internal/subjects.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ import (
99
"github.com/gofiber/fiber/v3"
1010
)
1111

12+
type SubjectRoutes struct{}
13+
14+
func NewSubjectRoutes() *SubjectRoutes {
15+
return &SubjectRoutes{}
16+
}
17+
1218
// GetSubjectByID retrieves a subject by its ID
13-
func GetSubjectByID(c fiber.Ctx) error {
19+
func (sr *SubjectRoutes) GetSubjectByID(c fiber.Ctx) error {
1420
logger := config.SetupLogger()
1521
subjectID := c.Params("subjectId")
1622

@@ -32,7 +38,7 @@ func GetSubjectByID(c fiber.Ctx) error {
3238
return response.Success(c, subject)
3339
}
3440

35-
func GetAllSubjects(c fiber.Ctx) error {
41+
func (sr *SubjectRoutes) GetAllSubjects(c fiber.Ctx) error {
3642
logger := config.SetupLogger()
3743

3844
subjectService := services.NewSubjectService()
@@ -45,7 +51,7 @@ func GetAllSubjects(c fiber.Ctx) error {
4551
return response.Success(c, subjects)
4652
}
4753

48-
func GetUserSubjects(c fiber.Ctx) error {
54+
func (sr *SubjectRoutes) GetUserSubjects(c fiber.Ctx) error {
4955
logger := config.SetupLogger()
5056
claimsInterface := c.Locals("claims")
5157

0 commit comments

Comments
 (0)