package parser import ( "log/slog" "regexp" "strconv" "strings" "watch-tool/models" ) var ( nginxAccessPattern = regexp.MustCompile(`^(\S+)\s+\S+\s+(\S+)\s+\[([^\]]+)\]\s+"([^"]+)"\s+(\d+)\s+(\d+|-)\s*(?:"([^"]*)"\s+"([^"]*)")?`) ) type NginxParser struct{} func (n *NginxParser) Parse(line string) (models.LogMessage, error) { newEntry := models.LogMessage{ Service: "nginx", } matches := nginxAccessPattern.FindStringSubmatch(strings.TrimSpace(line)) if len(matches) < 7 { return newEntry, nil } statusCode, err := strconv.ParseInt(matches[5], 10, 64) if err != nil { slog.Error("cant parse statuscode", "error", err) } bytesSend, err := strconv.ParseInt(matches[6], 10, 64) if err != nil { slog.Error("cant parse bytessend", "error", err) } baseInfo := models.NGinXBaseInfo{ ClientIP: matches[1], RemoteUser: matches[2], Request: matches[4], StatusCode: int(statusCode), BytesSend: int(bytesSend), } if len(matches) > 7 && matches[7] != "" { baseInfo.Referer = matches[7] } if len(matches) > 8 && matches[8] != "" { baseInfo.UserAgent = matches[8] } if requestParts := strings.Fields(matches[4]); len(requestParts) >= 3 { baseInfo.HTTPMethod = requestParts[0] baseInfo.RequestURI = requestParts[1] baseInfo.HTTPVersion = requestParts[2] } newEntry.ServiceInformation = baseInfo return newEntry, nil }