/
main.go
86 lines (78 loc) · 2.28 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
package main
import (
"encoding/json"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/mizhexiaoxiao/front-tracing-go/common"
"github.com/mizhexiaoxiao/front-tracing-go/config"
"github.com/mizhexiaoxiao/front-tracing-go/logger"
"github.com/mizhexiaoxiao/front-tracing-go/middleware"
"github.com/mizhexiaoxiao/front-tracing-go/tracing"
)
func main() {
//configuration initialization
err := config.Parse()
if err != nil {
logger.InfoLogger().Panic("fatal error config file: ", err)
}
// start
logger.InfoLogger().Println("Starting application")
app := fiber.New()
app.Use(middleware.CORSConfig())
//time calibration
app.Head("/timestamp", func(c *fiber.Ctx) error {
f := c.Response()
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
f.Header.Add("Access-Control-Expose-Headers", "timestamp")
f.Header.Add("timestamp", timestamp)
return nil
})
app.Post("/trace", func(c *fiber.Ctx) error {
body := c.Body()
serviceName := c.Query("servicename", "front")
serviceName = serviceName + ".front"
tracer, closer, err := tracing.NewJaegerTracer(serviceName)
defer closer.Close()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(common.Response{
Code: fiber.StatusInternalServerError,
Msg: err.Error(),
Data: nil,
})
}
ResMapSlice := make([]common.BodyArgs, 20)
if err := json.Unmarshal(body, &ResMapSlice); err != nil {
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(common.Response{
Code: fiber.StatusBadRequest,
Msg: err.Error(),
Data: nil,
})
}
}
for _, value := range ResMapSlice {
// vaildate data
errors := common.ValidateStructBody(value)
if errors != nil {
return c.Status(fiber.StatusBadRequest).JSON(common.Response{
Code: fiber.StatusBadRequest,
Msg: "validate error",
Data: errors,
})
}
if err := tracing.ExtractData(tracer, value); err != nil {
logger.InfoLogger().Println(err)
return c.Status(fiber.StatusInternalServerError).JSON(common.Response{
Code: fiber.StatusInternalServerError,
Msg: err.Error(),
})
}
}
return c.Status(fiber.StatusOK).JSON(common.Response{
Code: fiber.StatusOK,
Msg: "success",
})
})
logger.ErrorLogger().Fatalln(app.Listen(":" + config.FiberPort()))
}