This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
104 lines (75 loc) · 2.05 KB
/
track.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 controllers
import (
"encoding/json"
"github.com/playmean/scoper/common"
"github.com/playmean/scoper/database"
"github.com/playmean/scoper/logger"
"github.com/playmean/scoper/project"
"github.com/playmean/scoper/track"
"github.com/gofiber/fiber/v2"
)
// MiddlewareTrack for tracking APIs
func MiddlewareTrack(c *fiber.Ctx) error {
db := database.DBConn
var prj project.Project
projectKey := c.Params("key")
trackType := c.Params("type")
db.First(&prj, "archived = 0 AND key = ?", projectKey)
if prj.ID == 0 {
return c.Status(fiber.StatusNotFound).JSON(common.Response{
OK: false,
Error: "project not found",
})
}
switch trackType {
case "error":
return trackError(c, &prj)
case "log":
return trackLog(c, &prj)
default:
return c.Status(fiber.StatusBadRequest).JSON(common.Response{
OK: false,
Error: "unknown type of track",
})
}
}
func trackError(c *fiber.Ctx, prj *project.Project) error {
db := database.DBConn
var body track.ReportPacket
c.BodyParser(&body)
formatted, _ := json.MarshalIndent(body, "", " ")
logger.Log("TRACK:ERROR", prj.Key, string(formatted))
track := &track.Track{
Type: "error",
Project: *prj,
Environment: track.ResolveEnvironment(c),
Message: body.Message,
Stack: body.Stack,
Filename: body.Source.Filename,
Lineno: body.Source.Position.Lineno,
Colno: body.Source.Position.Colno,
}
res := db.Save(track)
newTags(track, body.Tags)
return common.Answer(c, res.Error, map[string]string{
"hash": hashID(track.ID),
})
}
func trackLog(c *fiber.Ctx, prj *project.Project) error {
db := database.DBConn
var body track.LogPacket
c.BodyParser(&body)
formatted, _ := json.MarshalIndent(body, "", " ")
logger.Log("TRACK:LOG", prj.Key, string(formatted))
track := &track.Track{
Type: "log",
Project: *prj,
Environment: track.ResolveEnvironment(c),
Meta: marshal(body.Data),
}
res := db.Save(track)
newTags(track, body.Tags)
return common.Answer(c, res.Error, map[string]string{
"hash": hashID(track.ID),
})
}