KAOS/code/internal/server/logs.go
djuka 098ed13705 T22: Reorganizacija testova + logs handler + konzola fix
- Razbijen monolitni server_test.go na fokusirane test fajlove:
  api_test.go, dashboard_test.go, docs_test.go, search_test.go,
  submit_test.go, task_detail_test.go, console_test.go, sse_test.go,
  timestamp_test.go, ui_test.go, test_helpers_test.go
- Dodat logs.go handler (handleLogsTail) koji je nedostajao
- Dodat LogFile u config
- Fix konzola: prompt se šalje preko fajla umesto direktno u PTY
- 192 testova prolazi

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 04:45:50 +00:00

42 lines
1004 B
Go

package server
import (
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
)
const maxLogLines = 20
// handleLogsTail returns the last 20 lines of the server log file as plain text.
func (s *Server) handleLogsTail(c *gin.Context) {
if s.Config.LogFile == "" {
c.String(http.StatusOK, "KAOS_LOG_FILE nije podešen. Podesi env varijablu za prikaz logova.")
return
}
data, err := os.ReadFile(s.Config.LogFile)
if err != nil {
if os.IsNotExist(err) {
c.String(http.StatusOK, "Log fajl ne postoji: "+s.Config.LogFile)
return
}
c.String(http.StatusInternalServerError, "Greška pri čitanju loga: "+err.Error())
return
}
lines := tailLines(string(data), maxLogLines)
c.String(http.StatusOK, strings.Join(lines, "\n"))
}
// tailLines returns the last n non-empty lines from text.
func tailLines(text string, n int) []string {
allLines := strings.Split(strings.TrimRight(text, "\n"), "\n")
if len(allLines) <= n {
return allLines
}
return allLines[len(allLines)-n:]
}