Init and optimizations

This commit is contained in:
2025-07-12 23:09:54 +02:00
parent 1a0b081e96
commit 77e4a2b6d1
11 changed files with 1061 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
package racc_logging
import (
"fmt"
"net/http"
"strings"
"time"
"git.send.nrw/sendnrw/raccws/racc_session"
)
type LogItem struct {
Module string
URL string
Time string
Session string
RemoteIP string
RemotePort string
Method string
Status string
}
var LogBook []LogItem
func GetLastXitems(amount int) []LogItem {
var temp []LogItem
startIndex := len(LogBook) - amount
// Sicherstellen, dass startIndex nicht negativ wird
if startIndex < 0 {
startIndex = 0
}
for i := startIndex; i < len(LogBook); i++ {
temp = append(temp, LogBook[i])
}
return temp
}
func Logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
AppendToLogMainFunction(w, r, "")
next.ServeHTTP(w, r)
})
}
func AppendToLogMainFunction(w http.ResponseWriter, r *http.Request, status string) {
start := time.Now()
sessionID := r.Context().Value(racc_session.SessionKey).(string)
meth := r.Method
IP_ONLY := strings.Split(r.RemoteAddr, ":")[0]
PORT_ONLY := strings.Split(r.RemoteAddr, ":")[1]
li := LogItem{Module: "http", URL: r.URL.Path, Time: start.Format(time.DateTime), Session: sessionID, RemoteIP: IP_ONLY, RemotePort: PORT_ONLY, Status: status, Method: meth}
WriteLog(li)
}
func WriteLog(logitem LogItem) {
LogBook = append(LogBook, logitem)
fmt.Println(logitem)
}