-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (89 loc) · 2.96 KB
/
main.go
File metadata and controls
106 lines (89 loc) · 2.96 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
package main
import (
"fmt"
"runtime/debug"
"skycrypt/src"
"skycrypt/src/forensics"
"skycrypt/src/utility"
"time"
_ "skycrypt/docs"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"go.uber.org/zap"
)
// @title SkyCrypt API
// @version 1.0
// @description API for SkyCrypt - A Hypixel SkyBlock Stats Viewer
// @host localhost:8080
// @BasePath /
func main() {
if utility.IsForensicsEnabled() {
// ========== FORENSIC LOGGING INIT (MUST BE FIRST) ==========
forensics.InitLogger()
defer forensics.Sync()
forensics.InitErrorTracker()
forensics.InitCriticalPathAnalyzer()
forensics.InitNPlus1Detector()
forensics.Logger.Info("application_starting",
zap.String("phase", "init"),
)
runtimeMonitor := forensics.NewRuntimeMonitor()
go runtimeMonitor.Start()
go forensics.GlobalErrorTracker.StartPeriodicSummary()
go forensics.GlobalCPAnalyzer.StartPeriodicReport()
go forensics.GlobalNPlus1Detector.CleanupOldPatterns()
go forensics.LogCacheStatsPeriodically(nil)
// ========== END FORENSIC LOGGING INIT ==========
}
app := fiber.New(fiber.Config{
Prefork: true, // Enable prefork (requires --pid=host in Docker)
ServerHeader: "", // Remove server header for slight perf gain
DisableKeepalive: false, // Keep connections alive
DisableDefaultDate: true, // Disable date header
DisableDefaultContentType: false,
BodyLimit: 10 << 20, // 10MB
ReadBufferSize: 4096,
WriteBufferSize: 4096,
ReadTimeout: 15 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
})
// ========== FORENSIC REQUEST TRACING (MUST BE BEFORE RECOVER) ==========
if utility.IsForensicsEnabled() {
app.Use(forensics.RequestTracingMiddleware())
}
app.Use(recover.New(recover.Config{
EnableStackTrace: true,
StackTraceHandler: func(c *fiber.Ctx, err interface{}) {
stack := debug.Stack()
fmt.Printf("\033[31m\n========== FATAL PANIC ==========\nPANIC: %v\n\nSTACK TRACE:\n%s\n==================================\033[0m\n", err, stack)
if utility.IsForensicsEnabled() {
forensics.Logger.Error("panic_recovered",
zap.Any("error", err),
zap.String("url", c.OriginalURL()),
zap.String("method", c.Method()),
zap.String("stack_trace", string(stack)),
)
}
utility.SendWebhook(c.OriginalURL(), err, stack)
_ = c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Internal Server Error",
"type": fmt.Sprintf("%T", err),
"detail": fmt.Sprintf("%v", err),
})
},
}))
app.Use(cors.New())
err := src.SetupApplication()
if err != nil {
if utility.IsForensicsEnabled() {
forensics.PrintFatal(fmt.Sprintf("Application setup failed: %v", err), zap.Error(err))
}
panic(err)
}
src.SetupRoutes(app)
if err := app.Listen(":8080"); err != nil {
panic(err)
}
}