This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
104 lines (88 loc) · 2.62 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)
const (
Namespace = "namespace"
ObjectType = "objecttype"
ObjectName = "objectname"
)
func main() {
format := new(log.JSONFormatter)
format.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(format)
log.SetOutput(os.Stdout)
r := CreateRouter(log.StandardLogger())
http.Handle("/", r)
if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil {
fmt.Println("unable to start server", "0.0.0.0:8080", err)
}
}
// CreateRouter sets up the main mux
func CreateRouter(logger *log.Logger) *mux.Router {
r := mux.NewRouter()
r.HandleFunc(fmt.Sprintf("/log/{%s}/{%s}/{%s}", Namespace, ObjectType, ObjectName), Logger(logger)).Methods("POST")
r.HandleFunc("/status", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusOK)
})
return r
}
// Logger logs incomming request bodies as raw or json to stdout in json format
func Logger(logger *log.Logger) func(http.ResponseWriter, *http.Request) {
return func(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
metadata := map[string]interface{}{
Namespace: vars[Namespace],
ObjectType: vars[ObjectType],
ObjectName: vars[ObjectName],
}
bodies := bufio.NewScanner(request.Body)
bodies.Split(bufio.ScanLines)
/*
body, err := ioutil.ReadAll(request.Body)
if err != nil {
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(err.Error()))
return
}
*/
for bodies.Scan() {
body := bodies.Bytes()
if request.Header.Get("Content-Type") == "application/json" {
var jsonBody map[string]interface{}
err := json.Unmarshal(body, &jsonBody)
if err != nil {
LogRaw(logger, metadata, string(body))
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(err.Error()))
return
}
LogJSON(logger, metadata, jsonBody)
} else {
LogRaw(logger, metadata, string(body))
}
}
response.WriteHeader(http.StatusOK)
}
}
// LogRaw logs all non application/json types with request body as msg
func LogRaw(logger *log.Logger, fields map[string]interface{}, msg string) {
entry := log.NewEntry(logger)
entry.WithFields(fields).Info(msg)
}
// LogJSON logs all application/json types with request body as fields in the field 'json'
func LogJSON(logger *log.Logger, fields map[string]interface{}, json map[string]interface{}) {
entry := log.NewEntry(logger)
msg := ""
if m, ok := json["msg"].(string); ok {
msg = m
delete(json, "msg")
}
entry.WithFields(fields).WithFields(json).Info(msg)
}