feat(ws server): added in a websocket on port system to help with better logging

This commit is contained in:
2025-07-25 12:13:19 -05:00
parent 074032f20d
commit 5bcbdaf3d0
17 changed files with 679 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
package logging
package loggingx
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package logging
package loggingx
import (
"github.com/gin-gonic/gin"

View File

@@ -1,4 +1,4 @@
package logging
package loggingx
import (
"fmt"
@@ -20,7 +20,7 @@ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
fmt.Println("Origin:", r.Header.Get("Origin"))
//fmt.Println("Origin:", r.Header.Get("Origin"))
return true
},
}
@@ -65,6 +65,17 @@ func handleSSE(c *gin.Context) {
"user_agent": c.Request.UserAgent(),
})
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type")
c.Header("Access-Control-Allow-Methods", "GET, OPTIONS")
// Handle preflight requests
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
@@ -92,6 +103,11 @@ func handleSSE(c *gin.Context) {
func handleWebSocket(c *gin.Context) {
log := New()
log.Info("WebSocket connection established", "logger", map[string]interface{}{
"endpoint": "/api/logger/logs",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
})
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Error("WebSocket upgrade failed", "logger", map[string]interface{}{