Skip to content

Commit ce258c0

Browse files
author
myxy99
committed
xapp
1 parent 26de536 commit ce258c0

File tree

5 files changed

+66
-50
lines changed

5 files changed

+66
-50
lines changed

xapp/app.go

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@
66
package xapp
77

88
import (
9-
"github.com/coder2z/g-saber/xcfg"
109
"github.com/coder2z/g-saber/xconsole"
1110
"github.com/coder2z/g-saber/xnet"
12-
"github.com/coder2z/g-saber/xstring"
11+
"github.com/coder2z/g-saber/xtime"
1312
"github.com/coder2z/g-server/xversion"
1413
"os"
1514
"runtime"
16-
"sync"
17-
"time"
1815
)
1916

2017
const (
21-
dAppName = "MyApp"
22-
dAppVersion = "v0.1.0"
18+
dAppName = "unknown app name"
19+
dHostIp = "0.0.0.0"
2320
)
2421

2522
func init() {
@@ -30,71 +27,70 @@ func init() {
3027
xconsole.Blue(` | |___| |__| | |__| | |____| | \ \ / /_ / /__ `)
3128
xconsole.Blue(` \_____\____/|_____/|______|_| \_|____/_____|`)
3229
xconsole.Blue(` --version = ` + xversion.Version)
33-
startTime = time.Now().Format("2006-01-02 15:04:05")
30+
startTime = xtime.Now().Format("2006-01-02 15:04:05")
3431
goVersion = runtime.Version()
32+
33+
name, err := os.Hostname()
34+
if err != nil {
35+
name = dAppName
36+
}
37+
hostName = name
38+
39+
ip, err := xnet.GetLocalIP()
40+
if err != nil {
41+
ip = dHostIp
42+
}
43+
hostIp = ip
44+
45+
appMode = os.Getenv(`SERVER_APP_MODE`)
46+
appId = os.Getenv(`SERVER_APP_ID`)
47+
debug = os.Getenv(`SERVER_APP_DEBUG`)
3548
}
3649

3750
var (
38-
startTime string
39-
goVersion string
51+
startTime string
52+
goVersion string
53+
hostName string
54+
hostIp string
55+
56+
// build -X
4057
appName string
41-
hostName string
4258
buildAppVersion string
4359
buildHost string
44-
debug = true
45-
one = sync.Once{}
46-
appId = xstring.GenerateID()
60+
61+
// env
62+
appMode string
63+
appId string
64+
debug = "true"
4765
)
4866

4967
// Name gets application name.
5068
func Name() string {
51-
if appName == "" {
52-
if appName = xcfg.GetString("app.name"); appName == "" {
53-
appName = dAppName
54-
}
55-
}
5669
return appName
5770
}
5871

59-
// Debug gets application debug.
60-
func Debug() bool {
61-
one.Do(func() {
62-
if data := xcfg.GetString("app.debug"); data == "false" {
63-
debug = false
64-
}
65-
})
66-
return debug
67-
}
68-
6972
//AppVersion get buildAppVersion
7073
func AppVersion() string {
71-
if buildAppVersion == "" {
72-
if buildAppVersion = xcfg.GetString("app.version"); buildAppVersion == "" {
73-
buildAppVersion = dAppVersion
74-
}
75-
}
7674
return buildAppVersion
7775
}
7876

77+
//AppMode get AppMode
78+
func AppMode() string {
79+
return appMode
80+
}
81+
82+
//HostIP get HostIP
83+
func HostIP() string {
84+
return hostIp
85+
}
86+
7987
//BuildHost get buildHost
8088
func BuildHost() string {
81-
if buildHost == "" {
82-
var err error
83-
if buildHost, err = xnet.GetLocalIP(); err != nil {
84-
hostName = "0.0.0.0"
85-
}
86-
}
8789
return buildHost
8890
}
8991

9092
// HostName get host name
9193
func HostName() string {
92-
if hostName == "" {
93-
var err error
94-
if hostName, err = os.Hostname(); err != nil {
95-
hostName = "unknown"
96-
}
97-
}
9894
return hostName
9995
}
10096

@@ -112,11 +108,17 @@ func AppId() string {
112108
return appId
113109
}
114110

111+
func Debug() bool {
112+
return debug == "true"
113+
}
114+
115115
func PrintVersion() {
116116
xconsole.Greenf("app name:", Name())
117117
xconsole.Greenf("app id:", AppId())
118+
xconsole.Greenf("app mode:", AppMode())
118119
xconsole.Greenf("host name:", HostName())
119-
xconsole.Greenf("app debug:", Debug())
120+
xconsole.Greenf("host ip:", HostIP())
121+
xconsole.Greenf("debug:", Debug())
120122
xconsole.Greenf("app version:", AppVersion())
121123
xconsole.Greenf("build host:", BuildHost())
122124
xconsole.Greenf("start time:", StartTime())

xgrpc/client/interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func XAidUnaryClientInterceptor() grpc.UnaryClientInterceptor {
162162
xapp.HostName(),
163163
xapp.AppId(),
164164
),
165-
"ip", xapp.BuildHost(),
165+
"ip", xapp.HostIP(),
166166
"app_id", xapp.AppId(),
167167
"app_name", xapp.Name(),
168168
"host_name", xapp.HostName(),

xmonitor/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package xmonitor
77

88
import (
9-
cfg "github.com/coder2z/g-saber/xcfg"
109
"github.com/coder2z/g-server/xapp"
1110
"github.com/prometheus/client_golang/prometheus/promhttp"
1211
"net/http"
@@ -16,7 +15,7 @@ import (
1615
func init() {
1716
BuildInfoGauge.WithLabelValues(
1817
xapp.Name(),
19-
cfg.GetString("app.mode"),
18+
xapp.AppMode(),
2019
xapp.AppVersion(),
2120
xapp.GoVersion(),
2221
xapp.StartTime(),

xtrace/jaeger/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func DefaultConfig() *Config {
5252
},
5353
Tags: []opentracing.Tag{
5454
{Key: "host_name", Value: xapp.HostName()},
55+
{Key: "host_ip", Value: xapp.HostIP()},
5556
{Key: "app_id", Value: xapp.AppId()},
5657
{Key: "app_name", Value: xapp.Name()},
5758
},

xversion/xversion.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@
55
**/
66
package xversion
77

8-
const Version = "v1.0.5"
8+
import "runtime/debug"
9+
10+
var Version string
11+
12+
func init() {
13+
Version = "unknown version"
14+
info, ok := debug.ReadBuildInfo()
15+
if ok {
16+
for _, value := range info.Deps {
17+
if value.Path == "github.com/coder2z/g-server" {
18+
Version = value.Version
19+
}
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)