diff --git a/.gitignore b/.gitignore index 6b8afe3..f825138 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea -.env \ No newline at end of file +.env +/saved_log \ No newline at end of file diff --git a/main.go b/main.go index 1ef75d4..3db8375 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,18 @@ package main import ( - "encoding/json" "fmt" "github.com/gin-gonic/gin" "leafdev.top/ivampiresp/msgpack-fluentbit-log-collect/config" "log" + "net/http" + "os" "time" ) var Router *gin.Engine -type LogStruct []struct { +type LogStruct struct { Date float64 `json:"date"` Time time.Time `json:"time"` Stream string `json:"stream"` @@ -50,11 +51,9 @@ func InitGin() { func Controller() { Router.POST("/", func(c *gin.Context) { + c.Header("Content-Type", "application/json") if !ValidatePassword(c.GetHeader("Authorization")) { - c.JSON(401, gin.H{ - "code": 401, - "message": "Unauthorized", - }) + c.Status(http.StatusUnauthorized) return } @@ -63,13 +62,21 @@ func Controller() { // bind LogStruct err := c.BindJSON(&remoteLog) if err != nil { - c.JSON(400, gin.H{ - "code": 400, - }) + c.Status(http.StatusBadRequest) + fmt.Println(err) return } - fmt.Println(remoteLog[0]) + // parse remoteLog.time + for _, logStruct := range remoteLog { + err := Process(logStruct) + if err != nil { + c.Status(http.StatusInternalServerError) + + fmt.Println(err) + return + } + } }) } @@ -78,17 +85,48 @@ func ValidatePassword(pass string) bool { return pass == "Bearer "+config.Auth.Pass } -func Process(body []byte) error { - // time +func Process(logStruct LogStruct) error { + fileName := GetPath(true, logStruct.Time.Year(), logStruct.Time.Month(), logStruct.Time.Day(), logStruct.Time.Hour(), logStruct.Kubernetes.NamespaceName, logStruct.Stream, logStruct.Kubernetes.ContainerName) + path := GetPath(false, logStruct.Time.Year(), logStruct.Time.Month(), logStruct.Time.Day(), logStruct.Time.Hour(), logStruct.Kubernetes.NamespaceName, logStruct.Stream, logStruct.Kubernetes.ContainerName) - var r map[interface{}]interface{} + // validate path exists + if _, err := os.Stat(path); os.IsNotExist(err) { + // create path + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + return err + } + } - if err := json.Unmarshal(body, &r); err != nil { + file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { return err } - fmt.Println(r["kubernetes"]) + defer func(file *os.File) { + err := file.Close() + if err != nil { + fmt.Println(err) + } + }(file) + + _, err = file.WriteString(fmt.Sprintf("[%s] %s\n", logStruct.Time, logStruct.Log)) + if err != nil { + return err + } return nil } + +func GetPath(includeFileName bool, year int, month time.Month, day int, hour int, namespace string, stream string, containerName string) string { + // dir + year/month/day/namespace/hour/podName-stream.log + // month to int + + monthNumber := int(month) + if includeFileName { + return fmt.Sprintf("%s/%d-%d/%d/%s/%d/%s-%s.log", config.Dir.Dir, year, monthNumber, day, namespace, hour, containerName, stream) + } + + return fmt.Sprintf("%s/%d-%d/%d/%s/%d/", config.Dir.Dir, year, monthNumber, day, namespace, hour) +}