-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpotens.go
93 lines (77 loc) · 2 KB
/
potens.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
package potens
import (
"log"
"net"
"os"
"path"
"path/filepath"
"strconv"
"github.com/satori/go.uuid"
"go.uber.org/zap"
"strings"
)
//NewApplication creates an instance of your application, name will be converted to upper and _
func NewApplication(name string) (*Application) {
app := makeApp(name)
osPort := os.Getenv("APP_" + app.ServiceKey() + EnvListenPortSuffix)
if osPort != "" {
intPort, err := strconv.ParseInt(osPort, 10, 32)
if err != nil {
log.Print("Unable to use ", osPort, " as the listen port for ", name)
} else {
app.Port = int(intPort)
}
}
return app
}
//NewService create a new instance of a service, this is usually not required
func NewService(name string) (*Application) {
app := makeApp(name)
return app
}
func makeApp(name string) (*Application) {
var err error
app := &Application{
services: &serviceCache{},
Name: name,
IP: net.IPv4(127, 0, 0, 1),
Port: KubexDefaultGRPCPort,
consoleDomain: KubexProductionConsoleDomain,
logDiscoveryHB: false,
}
app.instanceID = uuid.NewV4().String()
kubexDomain := os.Getenv(EnvKubexConsoleDomain)
if kubexDomain != "" {
app.consoleDomain = kubexDomain
}
if app.consoleDomain != KubexProductionConsoleDomain {
app.logger, err = zap.NewDevelopment()
} else {
app.logger, err = zap.NewProduction()
}
if err != nil {
log.Fatal(err)
}
osPort := os.Getenv(app.ServiceKey() + EnvListenPortSuffix)
if osPort != "" {
intPort, err := strconv.ParseInt(osPort, 10, 32)
if err != nil {
log.Print("Unable to use ", osPort, " as the listen port for ", name)
} else {
app.Port = int(intPort)
}
}
return app
}
func (app *Application) relPath(file string) string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
app.FatalErr(err)
// if executable is in TempDir use getwd instead (go run)
if strings.HasPrefix(dir, os.TempDir()) {
dir, err = os.Getwd()
app.FatalErr(err)
dir, err = filepath.Abs(dir)
app.FatalErr(err)
}
return path.Join(dir, file)
}