-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (61 loc) · 1.5 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
package main
import (
"context"
"flag"
"github.com/TiyaAnlite/FocotServicesCommon/echox"
"github.com/TiyaAnlite/FocotServicesCommon/envx"
"github.com/TiyaAnlite/FocotServicesCommon/natsx"
"github.com/TiyaAnlite/FocotServicesCommon/tracex"
"github.com/TiyaAnlite/FocotServicesCommon/utils"
"go.opentelemetry.io/otel/trace"
"k8s.io/klog/v2"
"sync"
"testing"
)
type config struct {
echox.EchoConfig
natsx.NatsConfig
ServiceId string `json:"service_id" yaml:"service_id" env:"SERVICE_ID" envDefault:"http-proxy"`
worker *worker
}
type worker struct {
Ctx context.Context
*sync.WaitGroup
trace.Tracer
}
var (
cfg = &config{}
mq *natsx.NatsHelper
traceHelper = &tracex.ServiceTraceHelper{}
)
func init() {
testing.Init()
flag.Parse()
envx.MustLoadEnv(cfg)
traceHelper.SetupTrace()
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
cfg.worker = &worker{
Ctx: ctx,
WaitGroup: &sync.WaitGroup{},
Tracer: traceHelper.NewTracer(),
}
defer traceHelper.Shutdown(context.Background()) // Exit all 2
initCtx, initTracer := cfg.worker.Start(ctx, "Init")
defer initTracer.End()
_, t1 := cfg.worker.Start(initCtx, "Connect NATS")
defer t1.End()
mq = &natsx.NatsHelper{}
if err := mq.Open(cfg.NatsConfig); err != nil {
t1.RecordError(err)
klog.Errorf("Cannot connect to NATS: %s", err.Error())
return
}
go echox.Run(&cfg.EchoConfig, setupRoutes)
utils.Wait4CtrlC()
klog.Infof("stopping...")
cancel()
cfg.worker.Wait()
klog.Info("done")
}