This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
96 lines (73 loc) · 2.1 KB
/
service.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
package service
import (
"context"
"sync"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/itbasis/go-clock"
itbasisCoreUtilsEnvReader "github.com/itbasis/go-core-utils/v2/env-reader"
jwtToken "github.com/itbasis/go-jwt-auth/v2/jwt-token"
logUtils "github.com/itbasis/go-log-utils/v2"
"github.com/itbasis/go-service/v2/db"
"github.com/juju/zaputil/zapctx"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type Service struct {
config *Config
jwtToken jwtToken.JwtToken
grpcServer *grpc.Server
grpcServerMetrics *prometheus.ServerMetrics
GrpcClientInterceptors []grpc.DialOption
gin *gin.Engine
ginControllers []HTTPController
clock clock.Clock
gorm *db.DB
scheduler *gocron.Scheduler
}
func NewServiceWithEnvironment(ctx context.Context, zapConfig zap.Config) *Service {
logUtils.ConfigureDefaultContextLogger(false, zapConfig)
config := &Config{}
if err := itbasisCoreUtilsEnvReader.ReadEnvConfig(ctx, config, nil); err != nil {
zapctx.Default.Sugar().Panic(err)
}
return NewServiceWithConfig(ctx, zapConfig, config)
}
func NewServiceWithConfig(ctx context.Context, zapConfig zap.Config, config *Config) *Service {
logger := zapctx.Logger(ctx).Sugar()
logger.Debugf("config: %++v", config)
_, err := logUtils.ConfigureRootLogger(ctx, config.ServiceName, zapConfig)
if err != nil {
logger.Panic(err)
}
service := &Service{config: config}
service.clock = clock.New()
service.initJwtToken(ctx)
return service
}
func (receiver *Service) Run() {
logger := zapctx.Default.Sugar()
logger.Debug("running service...")
if receiver.config.SchedulerEnabled {
receiver.scheduler.StartAsync()
}
wg := &sync.WaitGroup{}
// gRPC
if receiver.config.GrpcServerDisabled {
logger.Info(gRPCServerIsDisabled)
} else {
wg.Add(1)
go receiver.runGrpcServer(wg)
}
// HTTP
wg.Add(1)
go receiver.runGinServer(wg)
wg.Wait()
}
func (receiver *Service) GetConfig() Config {
return *receiver.config
}
func (receiver *Service) GetClock() clock.Clock {
return receiver.clock
}