改进
This commit is contained in:
parent
ae29fd16c0
commit
dac88506f6
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/.idea
|
/.idea
|
||||||
.env
|
.env
|
||||||
|
/saved_log
|
68
main.go
68
main.go
@ -1,17 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"leafdev.top/ivampiresp/msgpack-fluentbit-log-collect/config"
|
"leafdev.top/ivampiresp/msgpack-fluentbit-log-collect/config"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Router *gin.Engine
|
var Router *gin.Engine
|
||||||
|
|
||||||
type LogStruct []struct {
|
type LogStruct struct {
|
||||||
Date float64 `json:"date"`
|
Date float64 `json:"date"`
|
||||||
Time time.Time `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
Stream string `json:"stream"`
|
Stream string `json:"stream"`
|
||||||
@ -50,11 +51,9 @@ func InitGin() {
|
|||||||
|
|
||||||
func Controller() {
|
func Controller() {
|
||||||
Router.POST("/", func(c *gin.Context) {
|
Router.POST("/", func(c *gin.Context) {
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
if !ValidatePassword(c.GetHeader("Authorization")) {
|
if !ValidatePassword(c.GetHeader("Authorization")) {
|
||||||
c.JSON(401, gin.H{
|
c.Status(http.StatusUnauthorized)
|
||||||
"code": 401,
|
|
||||||
"message": "Unauthorized",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,13 +62,21 @@ func Controller() {
|
|||||||
// bind LogStruct
|
// bind LogStruct
|
||||||
err := c.BindJSON(&remoteLog)
|
err := c.BindJSON(&remoteLog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, gin.H{
|
c.Status(http.StatusBadRequest)
|
||||||
"code": 400,
|
fmt.Println(err)
|
||||||
})
|
|
||||||
return
|
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
|
return pass == "Bearer "+config.Auth.Pass
|
||||||
}
|
}
|
||||||
|
|
||||||
func Process(body []byte) error {
|
func Process(logStruct LogStruct) error {
|
||||||
// time
|
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
|
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
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user