|
|
|
|
@ -101,7 +101,7 @@ func (s *Server) routes() {
|
|
|
|
|
s.router.Use(func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
requestID := middleware.GetReqID(r.Context())
|
|
|
|
|
s.logger.Info("Request started",
|
|
|
|
|
s.logger.Debug("Request started",
|
|
|
|
|
"method", r.Method,
|
|
|
|
|
"path", r.URL.Path,
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
@ -110,7 +110,7 @@ func (s *Server) routes() {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
|
|
s.logger.Info("Request completed",
|
|
|
|
|
s.logger.Debug("Request completed",
|
|
|
|
|
"method", r.Method,
|
|
|
|
|
"path", r.URL.Path,
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
@ -450,7 +450,7 @@ func (s *Server) broadcastRaceEvent(event derby.Event) {
|
|
|
|
|
heatResult.Lane4Time = event.Result.Time
|
|
|
|
|
heatResult.Lane4Position = event.Result.FinishPlace
|
|
|
|
|
}
|
|
|
|
|
s.sendRaceEventToAllClients(fmt.Sprintf("event: lane-%d-time\ndata: %.4f\n\n", event.Result.Lane, event.Result.Time))
|
|
|
|
|
s.sendRaceEventToAllClients(fmt.Sprintf("event: lane-%d-time\ndata: %.3f\n\n", event.Result.Lane, event.Result.Time))
|
|
|
|
|
s.sendRaceEventToAllClients(fmt.Sprintf("event: lane-%d-position\ndata: %d\n\n", event.Result.Lane, event.Result.FinishPlace))
|
|
|
|
|
|
|
|
|
|
case derby.EventRaceComplete:
|
|
|
|
|
@ -604,7 +604,7 @@ func (s *Server) handleEvents() http.HandlerFunc {
|
|
|
|
|
requestID = fmt.Sprintf("req-%d", time.Now().UnixNano())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.logger.Info("SSE connection request received",
|
|
|
|
|
s.logger.Debug("SSE connection request received",
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
"remoteAddr", r.RemoteAddr,
|
|
|
|
|
"userAgent", r.UserAgent())
|
|
|
|
|
@ -634,7 +634,7 @@ func (s *Server) handleEvents() http.HandlerFunc {
|
|
|
|
|
clientCount := len(s.clients)
|
|
|
|
|
s.clientsMux.Unlock()
|
|
|
|
|
|
|
|
|
|
s.logger.Info("New client connected",
|
|
|
|
|
s.logger.Debug("New client connected",
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
"clientIP", r.RemoteAddr,
|
|
|
|
|
"totalClients", clientCount)
|
|
|
|
|
@ -655,7 +655,7 @@ func (s *Server) handleEvents() http.HandlerFunc {
|
|
|
|
|
s.clientsMux.Unlock()
|
|
|
|
|
close(clientChan)
|
|
|
|
|
|
|
|
|
|
s.logger.Info("Client disconnected",
|
|
|
|
|
s.logger.Debug("Client disconnected",
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
"clientIP", r.RemoteAddr,
|
|
|
|
|
"remainingClients", remainingClients)
|
|
|
|
|
@ -695,7 +695,7 @@ func (s *Server) handleEvents() http.HandlerFunc {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case <-r.Context().Done():
|
|
|
|
|
s.logger.Info("Client context done",
|
|
|
|
|
s.logger.Debug("Client context done",
|
|
|
|
|
"requestID", requestID,
|
|
|
|
|
"error", r.Context().Err())
|
|
|
|
|
return
|
|
|
|
|
@ -762,7 +762,7 @@ func (s *Server) handleStatus() http.HandlerFunc {
|
|
|
|
|
// handleAdminEvents handles SSE events
|
|
|
|
|
func (s *Server) handleAdminEvents() http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
s.logger.Info("Admin events SSE connection established")
|
|
|
|
|
s.logger.Debug("Admin events SSE connection established")
|
|
|
|
|
// Set headers for SSE
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
@ -776,19 +776,19 @@ func (s *Server) handleAdminEvents() http.HandlerFunc {
|
|
|
|
|
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.logger.Info("Admin events SSE headers flushed")
|
|
|
|
|
s.logger.Debug("Admin events SSE headers flushed")
|
|
|
|
|
|
|
|
|
|
// Create a channel for this client
|
|
|
|
|
clientChan := make(chan string, 10)
|
|
|
|
|
s.logger.Info("Admin events SSE client channel created")
|
|
|
|
|
s.logger.Debug("Admin events SSE client channel created")
|
|
|
|
|
// Add client to map with mutex protection
|
|
|
|
|
s.adminclientsMux.Lock()
|
|
|
|
|
s.adminclients[clientChan] = true
|
|
|
|
|
clientCount := len(s.adminclients)
|
|
|
|
|
s.adminclientsMux.Unlock()
|
|
|
|
|
s.logger.Info("Admin events SSE client added to map")
|
|
|
|
|
s.logger.Debug("Admin events SSE client added to map")
|
|
|
|
|
|
|
|
|
|
s.logger.Info("New client connected",
|
|
|
|
|
s.logger.Debug("New client connected",
|
|
|
|
|
"clientIP", r.RemoteAddr,
|
|
|
|
|
"totalClients", clientCount)
|
|
|
|
|
|
|
|
|
|
@ -800,7 +800,7 @@ func (s *Server) handleAdminEvents() http.HandlerFunc {
|
|
|
|
|
s.adminclientsMux.Unlock()
|
|
|
|
|
close(clientChan)
|
|
|
|
|
|
|
|
|
|
s.logger.Info("Client disconnected",
|
|
|
|
|
s.logger.Debug("Client disconnected",
|
|
|
|
|
"clientIP", r.RemoteAddr,
|
|
|
|
|
"remainingClients", remainingClients)
|
|
|
|
|
}()
|
|
|
|
|
|