-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
134 lines (113 loc) · 3.32 KB
/
application.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
128
129
130
131
132
133
134
package potens
import (
"crypto/rsa"
"net"
"regexp"
"strconv"
"strings"
"github.com/kubex/potens-go/definition"
"github.com/kubex/potens-go/identity"
"github.com/kubex/potens-go/kapp"
"github.com/kubex/proto-go/application"
"github.com/kubex/proto-go/discovery"
"github.com/kubex/proto-go/eventpipe"
"go.uber.org/zap"
"google.golang.org/grpc"
)
//Application Helper struct for your application
type Application struct {
/** RunTime **/
Port int
IP net.IP
instanceID string
consoleDomain string
currentStatus discovery.ServiceStatus
appServer *kapp.ApplicationServer
/** Definition **/
Name string
definition *definition.AppDefinition
appRelease discovery.AppRelease
/** Identity **/
identity *identity.AppIdentity
pk *rsa.PrivateKey
kh string
/** Utility **/
logger *zap.Logger
/** gRPC **/
server *grpc.Server
services *serviceCache
logDiscoveryHB bool
}
type serviceCache struct {
discoveryClient discovery.DiscoveryClient
discoveryHost string
discoveryPort string
discoveryRegistered bool
socketHandler *socketHandler
eventPipeClient eventpipe.EventPipeClient
}
//FatalErr If an error is provided, Log().Fatal()
func (app *Application) FatalErr(err error) {
if err != nil {
app.Log().Fatal("Oops", zap.Error(err))
}
}
//GlobalAppID returns the global app ID for the application
func (app *Application) GlobalAppID() string {
return app.definition.GlobalAppID()
}
//VendorID returns the vendor ID for this application
func (app *Application) VendorID() string {
return app.definition.VendorID
}
//AppID returns the app ID for this application
func (app *Application) AppID() string {
return app.definition.AppID
}
//InstanceID returns the instance ID for this session
func (app *Application) InstanceID() string {
return app.instanceID
}
//PortString returns the port as a string
func (app *Application) PortString() string {
return strconv.FormatInt(int64(app.Port), 10)
}
//ConsoleDomain returns the kubex console domain being used
func (app *Application) ConsoleDomain() string {
return app.consoleDomain
}
//IsProduction returns if you are in production environment
func (app *Application) IsProduction() bool {
return app.consoleDomain == KubexProductionConsoleDomain
}
//Log returns a logger to log against
func (app *Application) Log() *zap.Logger {
return app.logger
}
//ServiceKey returns a key that can be used to pre-fix environment variables
func (app *Application) ServiceKey() string {
return strings.ToUpper(strings.Replace(regexp.MustCompile("[^A-Za-z0-9\\-_]").ReplaceAllString(app.Name, ""), "-", "_", -1))
}
//AppServer grpc application server, add routing and functionality for your app on this
func (app *Application) AppServer() *kapp.ApplicationServer {
if app.appServer == nil {
app.appServer = kapp.New()
}
return app.appServer
}
//RegisterAsAppServer listen to requests as a kubex application
func (app *Application) RegisterAsAppServer() *kapp.ApplicationServer {
srv := app.AppServer()
application.RegisterApplicationServer(app.GetGrpcServer(), srv)
return srv
}
//ExposeAndServe DiscoveryOnline && Serve
func (app *Application) ExposeAndServe() error {
discoveryErr := app.DiscoveryOnline()
if discoveryErr != nil {
return discoveryErr
}
err := app.Serve()
app.DiscoveryOffline()
return err
}