-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
99 lines (81 loc) · 2.2 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
97
98
99
package atk
import (
"github.com/micro/go-log"
"github.com/micro/go-micro"
"github.com/micro/go-grpc"
"github.com/micro/cli"
"github.com/lakstap/go-atk/database/config"
"strings"
"time"
"github.com/patrickmn/go-cache"
"github.com/lakstap/go-atk/database"
)
//ATK Options is a set of options to be passed to Run
type ATKGrpcServiceOption struct {
// Grpc Service Name
ServiceName string
// Version name for the service
Version string
// Type of GRPC Service ( Database )
ServiceType string
//Address GRPC Service binded
Address string
}
// ATK Grpc Service
type ATKGrpcService struct {
Options ATKGrpcServiceOption
Service micro.Service
Session *mdb.DatabaseSession
ATKCache *cache.Cache
}
// New ATK GRPC Service returns a new grpc with default values.
func NewATKGrpcService(opts ATKGrpcServiceOption) *ATKGrpcService {
atkService := &ATKGrpcService{
Options: opts,
ATKCache: cache.New(5*time.Minute, 10*time.Minute),
Service: grpc.NewService(
micro.Address(opts.Address),
micro.Flags(
cli.StringFlag{
Name: "db_config_path",
Usage: "JSON file path for database configuration",
Value: "config/config.json",
},
),
micro.Name(opts.ServiceName), //"go.micro.srv.atk.Grpc.project"
micro.Version(opts.Version),
//gsrv.Options(gogrpc.UnaryInterceptor(unaryInterceptor)),
),
}
// Initialize service based on the type
if strings.EqualFold(opts.ServiceType, "database") {
atkService.Service.Init(
micro.Action(func(c *cli.Context) {
log.Log("Reading the config data from the configuration file..")
dbConfigPath := c.String("db_config_path")
if len(dbConfigPath) > 0 {
log.Log("Parsing the Database config file...", dbConfigPath)
// Read the Config
dbConfig, err := config.ReadConfig(dbConfigPath)
if err != nil {
log.Fatal(err)
}
log.Logf("Read the configuration file (%s)..", dbConfig.Address)
// create the session
atkService.Session, err = mdb.GetDBSession(dbConfig)
if err != nil {
log.Fatal(err)
}
}
}),
)
}
return atkService
}
func (e *ATKGrpcService) RunATKGrpcService() error {
// Run service
if err := e.Service.Run(); err != nil {
log.Fatal(err)
}
return nil
}