-
Notifications
You must be signed in to change notification settings - Fork 3
/
cs2log_http.go
61 lines (52 loc) · 1.41 KB
/
cs2log_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cs2loghttp
import (
"bufio"
"net/http"
"regexp"
"strings"
"github.com/gin-gonic/gin"
cs2log "github.com/janstuemmel/cs2-log"
)
// httpLogPattern is a regexp to parse the log line on HTTP
var httpLogPattern = regexp.MustCompile(`(\d{2}\/\d{2}\/\d{4} - \d{2}:\d{2}:\d{2}.\d{3}) - (.*)`)
type handler func(ip string, id string, msg cs2log.Message) error
// NewLogHandler returns a new LogHandler. This function has side effect to override log line prefix.
func NewLogHandler(h handler) LogHandler {
// Override log line prefix
cs2log.LogLinePattern = httpLogPattern
return LogHandler{
handler: h,
}
}
// LogHandler is a handler for cs2-log HTTP.
type LogHandler struct {
handler handler
}
// Handle returns a gin.HandlerFunc to handle cs2-log HTTP.
func (l *LogHandler) Handle() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
raw, err := c.GetRawData()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
c.Abort()
return
}
sl := strings.NewReader(string(raw))
scanner := bufio.NewScanner(sl)
for scanner.Scan() {
msg, err := cs2log.Parse(scanner.Text())
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
c.Abort()
return
}
if err := l.handler(c.ClientIP(), id, msg); err != nil {
c.String(http.StatusInternalServerError, err.Error())
c.Abort()
return
}
}
c.String(http.StatusOK, "OK")
}
}