/
main.go
127 lines (103 loc) · 2.9 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"time"
"github.com/getsentry/sentry-go"
"github.com/opacity/storage-node/jobs"
"github.com/opacity/storage-node/models"
"github.com/opacity/storage-node/routes"
"github.com/opacity/storage-node/services"
"github.com/opacity/storage-node/utils"
)
var GO_ENV = "localhost"
var VERSION = "local"
func main() {
if GO_ENV == "" {
utils.PanicOnError(errors.New("the GO_ENV variable is not set; application can not run"))
}
os.Setenv("GO_ENV", GO_ENV)
tracesSampleRate := 0.0
if GO_ENV == "production" {
tracesSampleRate = 0.25
err := sentry.Init(sentry.ClientOptions{
Dsn: "https://03e807e8312d47938a94b73ebec3cc84@o126495.ingest.sentry.io/5855671",
Release: VERSION,
Environment: GO_ENV,
AttachStacktrace: true,
TracesSampleRate: tracesSampleRate,
BeforeSend: sentryOpacityBeforeSend,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
defer sentry.Flush(5 * time.Second)
}
defer catchError()
defer models.Close()
utils.SetLive()
err := services.InitStripe(utils.Env.StripeKeyProd)
utils.PanicOnError(err)
utils.SlackLog("Begin to restart service!")
if !utils.Env.DisableDbConn {
models.Connect(utils.Env.DatabaseURL)
}
setEnvPlans()
models.MigrateEnvWallets()
jobs.StartupJobs()
if utils.Env.EnableJobs {
jobs.ScheduleBackgroundJobs()
}
routes.CreateRoutes()
}
func setEnvPlans() {
plans := []utils.PlanInfo{}
results := models.DB.Find(&plans)
utils.Env.Plans = make(utils.PlanResponseType)
if results.RowsAffected == 0 {
err := json.Unmarshal([]byte(utils.DefaultPlansJson), &utils.Env.Plans)
utils.LogIfError(err, nil)
for _, plan := range utils.Env.Plans {
models.DB.Model(&utils.PlanInfo{}).Create(&plan)
}
} else {
for _, plan := range plans {
utils.Env.Plans[plan.StorageInGB] = plan
}
}
utils.CreatePlanMetrics()
}
func sentryOpacityBeforeSend(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
if event.Request != nil {
req := routes.GenericRequest{}
if err := json.Unmarshal([]byte(event.Request.Data), &req); err == nil {
if len(event.Exception) > 0 {
frames := event.Exception[0].Stacktrace.Frames
// do not include http/gin-gonic and the Sentry throw funcs ones
event.Exception[0].Stacktrace.Frames = frames[6 : len(frames)-3]
}
event.Request.Data = req.RequestBody
}
}
return event
}
func catchError() {
// Capture the error
if r := recover(); r != nil {
sentry.CurrentHub().Recover(r)
buff := bytes.NewBufferString("")
buff.Write(debug.Stack())
stacks := strings.Split(buff.String(), "\n")
threadId := stacks[0]
if len(stacks) > 5 {
stacks = stacks[5:] // skip the Stack() and Defer method.
}
utils.SlackLogError(fmt.Sprintf("Crash due to err %v!!!\nRunning on thread: %s,\nStack: \n%v\n", r, threadId, strings.Join(stacks, "\n")))
}
}