-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhandler.go
More file actions
62 lines (47 loc) · 1.08 KB
/
handler.go
File metadata and controls
62 lines (47 loc) · 1.08 KB
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
62
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/labstack/echo"
)
// ESLogger function for Elastic Search log handler
func ESLogger(c echo.Context) error {
body := echo.Map{}
if err := c.Bind(&body); err != nil {
return err
}
body["@timestamp"] = time.Now().Format(time.RFC3339)
indexPattern := os.Getenv("INDEX_PATTERN")
if indexPattern == "" {
indexPattern = "kong-2006-01-02"
}
currentTime := time.Now().Format(indexPattern)
esHost := os.Getenv("ES_HOST")
if esHost == "" {
esHost = "127.0.0.1"
}
esPort := os.Getenv("ES_PORT")
if esPort == "" {
esPort = "9200"
}
tr := &http.Transport{}
client := &http.Client{Transport: tr}
reqBody, err := json.Marshal(body)
// Send log to ElasticSearch
resp, err := client.Post(
fmt.Sprintf("http://%s:%s/%s/_doc", esHost, esPort, currentTime),
"application/json",
bytes.NewBuffer(reqBody),
)
if err != nil {
return err
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
return c.JSON(http.StatusOK, result)
}