-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
497 lines (427 loc) · 11.6 KB
/
main.go
File metadata and controls
497 lines (427 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"log"
"os"
"time"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
_ "github.com/CodeMeAPixel/FixFX-Core/docs"
"github.com/CodeMeAPixel/FixFX-Core/internal/handlers"
"github.com/CodeMeAPixel/FixFX-Core/internal/routes"
)
// Version information
const (
Version = "0.2.1"
BuildTime = "2026-02-13"
)
// @title FixFX API
// @version 0.1.0
// @description Backend API for FixFX documentation and tools
// @contact.name Support
// @contact.url https://fixfx.wiki/discord
// @host localhost:3001
// @BasePath /api
// @schemes http https
func main() {
// Load environment variables
if err := godotenv.Load(); err != nil {
log.Println("No .env file found, using environment variables")
}
// Log startup information
log.Printf("╔═══════════════════════════════════════════════════════════╗")
log.Printf("║ FixFX Backend API - Starting Up ║")
log.Printf("╠═══════════════════════════════════════════════════════════╣")
log.Printf("║ Version: v%s ║", Version)
log.Printf("║ Build Time: %s ║", BuildTime)
log.Printf("║ Environment: %s ║", os.Getenv("ENVIRONMENT"))
log.Printf("╚═══════════════════════════════════════════════════════════╝")
// Create Fiber app
app := fiber.New(fiber.Config{
AppName: "FixFX API",
ServerHeader: "FixFX/" + Version,
ErrorHandler: globalErrorHandler,
})
// Middleware
app.Use(corsMiddleware())
app.Use(loggingMiddleware())
// Initialize handlers
githubToken := os.Getenv("GITHUB_TOKEN")
handlers.InitArtifactsHandler(githubToken)
handlers.InitNativesHandler()
handlers.InitGameReferencesHandler()
handlers.InitSourceHandler(".", nil) // Use current directory as base path
handlers.InitContributorsHandler(githubToken)
handlers.InitValidatorHandler()
// Health check
app.Get("/health", healthCheck)
// Swagger JSON endpoint for ReDoc (must come first)
app.Get("/docs/doc.json", func(c *fiber.Ctx) error {
return c.SendFile("./docs/swagger.json")
})
// ReDoc UI - Custom styled documentation
app.Get("/docs", docsHandler)
app.Get("/docs/", docsHandler)
app.Get("/docs/index.html", docsHandler)
// API Routes
api := app.Group("/api")
// Register route groups
routes.RegisterArtifactsRoutes(api)
routes.RegisterNativesRoutes(api)
routes.RegisterGameReferencesRoutes(api)
routes.RegisterSourceRoutes(api)
routes.RegisterSearchRoutes(api)
routes.RegisterContributorsRoutes(api)
routes.RegisterValidatorRoutes(api)
// Start server
port := os.Getenv("PORT")
if port == "" {
port = "3001"
}
log.Printf("Starting FixFX API on port %s", port)
// Check for version updates in background
go checkVersionUpdates()
if err := app.Listen(":" + port); err != nil {
log.Fatal(err)
}
}
// @Summary Health Check
// @Description Check if the API is running and get version information
// @Tags Health
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Router /health [get]
func healthCheck(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "healthy",
"version": Version,
"service": "FixFX API",
"timestamp": c.Get("Date"),
})
}
func corsMiddleware() fiber.Handler {
return func(c *fiber.Ctx) error {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Method() == "OPTIONS" {
return c.SendStatus(200)
}
return c.Next()
}
}
func loggingMiddleware() fiber.Handler {
return func(c *fiber.Ctx) error {
log.Printf("%s %s", c.Method(), c.Path())
return c.Next()
}
}
func globalErrorHandler(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
return c.Status(code).JSON(fiber.Map{
"error": err.Error(),
"status": code,
})
}
var docsHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>FixFX API Documentation</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 100%);
color: #e4e4e7;
line-height: 1.6;
}
redoc {
display: block;
}
/* ReDoc customization */
:root {
--primary-color: #5865F2;
--secondary-color: #9D84B7;
--background-color: #09090b;
--text-color: #e4e4e7;
--border-color: #27272a;
--code-background: #18181b;
}
/* ReDoc Components - Dark Theme */
.redoc-container {
background: #09090b !important;
color: #e4e4e7 !important;
}
[data-testid="redoc-container"] {
background: #09090b !important;
}
/* Sidebar styling */
.menu-content, [class*="sidebar"], nav {
background: #0f0f0f !important;
}
.menu-item, [class*="menu-item"] {
color: #e4e4e7 !important;
}
.menu-item.active, [class*="menu-item"][class*="active"] {
color: #5865F2 !important;
background: rgba(88, 101, 242, 0.1) !important;
}
.menu-item:hover {
background: rgba(88, 101, 242, 0.15) !important;
color: #7c7cff !important;
}
/* Header and panels */
.api-info, [class*="api-info"] {
background: rgba(88, 101, 242, 0.05) !important;
border-bottom: 1px solid #27272a !important;
}
[class*="header"], .header {
background: linear-gradient(135deg, rgba(88, 101, 242, 0.05) 0%, rgba(157, 132, 183, 0.03) 100%) !important;
border-bottom: 1px solid #27272a !important;
}
/* Content area */
[class*="right-panel"], .right-panel {
background: #09090b !important;
}
/* Syntax highlighting */
code {
font-family: 'JetBrains Mono', monospace;
background: #18181b !important;
color: #e4e4e7 !important;
padding: 2px 6px;
border-radius: 4px;
}
pre {
background: #18181b !important;
border: 1px solid #27272a !important;
color: #e4e4e7 !important;
}
pre code {
background: transparent !important;
color: inherit !important;
}
/* Links */
a, [class*="link"] {
color: #5865F2 !important;
text-decoration: none;
}
a:hover {
color: #7c7cff !important;
text-decoration: underline;
}
/* Scrollbar styling */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: #09090b;
}
::-webkit-scrollbar-thumb {
background: #27272a;
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: #3f3f46;
}
/* Tables */
table {
border-collapse: collapse;
}
th, [class*="th"] {
background: rgba(88, 101, 242, 0.1) !important;
color: #5865F2 !important;
border: 1px solid #27272a !important;
padding: 12px !important;
text-align: left;
font-weight: 600;
}
td, [class*="td"] {
border: 1px solid #27272a !important;
padding: 12px !important;
background: #09090b !important;
color: #e4e4e7 !important;
}
tr:hover {
background: rgba(88, 101, 242, 0.05) !important;
}
/* Buttons */
button, [role="button"] {
background: #5865F2 !important;
color: white !important;
border: none !important;
padding: 8px 16px !important;
border-radius: 6px !important;
cursor: pointer;
font-weight: 500;
}
button:hover, [role="button"]:hover {
background: #7c7cff !important;
}
/* Response codes */
.response-code, [class*="response"] {
background: #18181b !important;
color: #e4e4e7 !important;
border: 1px solid #27272a !important;
}
/* Try it out button */
[class*="try-it"] {
background: #5865F2 !important;
color: white !important;
}
/* Text and general styling */
p, span, div {
color: #e4e4e7 !important;
}
/* Headings */
h1, h2, h3, h4, h5, h6 {
color: #e4e4e7 !important;
}
/* Loading animation */
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
.loading {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
</style>
</head>
<body>
<redoc
spec-url="/docs/doc.json"
expand-single-schema="true"
sort-props-alphabetically="true"
show-extensions="true"
native-scrollbars="true"
path-in-middle-panel="true"
theme='{
"colors": {
"primary": {
"main": "#5865F2",
"light": "#7c7cff",
"dark": "#4752c4"
},
"success": {
"main": "#10b981"
},
"error": {
"main": "#ef4444"
},
"warning": {
"main": "#f59e0b"
},
"info": {
"main": "#3b82f6"
},
"http": {
"get": "#10b981",
"post": "#3b82f6",
"put": "#f59e0b",
"delete": "#ef4444",
"patch": "#8b5cf6",
"head": "#6b7280",
"options": "#9333ea"
},
"text": {
"primary": "#e4e4e7",
"secondary": "#a1a1a6"
},
"border": {
"dark": "#27272a",
"light": "#3f3f46"
},
"background": {
"primary": "#09090b",
"secondary": "#18181b"
}
},
"schema": {
"nestedBackground": "#18181b",
"linesColor": "#27272a"
},
"typography": {
"fontFamily": "\"Inter\", -apple-system, BlinkMacSystemFont, \"Segoe UI\", sans-serif",
"fontSize": {
"base": "14px",
"big": "18px",
"code": "13px",
"small": "12px",
"title": "24px"
},
"lineHeight": "1.6",
"fontWeightRegular": "400",
"fontWeightBold": "600",
"fontWeightLight": "300",
"smooth": "true"
},
"sidebar": {
"width": "250px",
"backgroundColor": "#0f0f0f",
"textColor": "#e4e4e7"
},
"rightPanel": {
"backgroundColor": "#09090b"
},
"codeBlock": {
"backgroundColor": "#18181b",
"textColor": "#e4e4e7"
}
}'>
</redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"></script>
</body>
</html>
`
func docsHandler(c *fiber.Ctx) error {
c.Set("Content-Type", "text/html; charset=utf-8")
return c.SendString(docsHTML)
}
// checkVersionUpdates checks GitHub releases for newer versions
// This runs as a goroutine to not block startup
func checkVersionUpdates() {
defer func() {
if r := recover(); r != nil {
log.Printf("Version check failed: %v", r)
}
}()
// Wait for Fiber to print its startup message first
time.Sleep(100 * time.Millisecond)
// For now, just log the current version
// In a future release, we can add GitHub API checking
log.Printf("")
log.Printf("✓ FixFX API v%s is running", Version)
log.Printf("→ Documentation available at http://localhost:3001/docs")
log.Printf("→ Health check at http://localhost:3001/health")
log.Printf("")
// TODO: Add GitHub API check for new versions
// Example implementation:
// resp, err := http.Get("https://api.github.com/repos/CodeMeAPixel/FixFX-Core/releases/latest")
// if err != nil {
// log.Printf("Could not check for updates: %v", err)
// return
// }
// var latestRelease struct {
// TagName string `json:"tag_name"`
// }
// json.NewDecoder(resp.Body).Decode(&latestRelease)
// if latestRelease.TagName > "v"+Version {
// log.Printf("⚠️ New version available: %s (current: v%s)", latestRelease.TagName, Version)
// }
}