66package xapp
77
88import (
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
2017const (
21- dAppName = "MyApp "
22- dAppVersion = "v0.1 .0"
18+ dAppName = "unknown app name "
19+ dHostIp = "0.0.0 .0"
2320)
2421
2522func 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
3750var (
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.
5068func 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
7073func 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
8088func 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
9193func 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+
115115func 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 ())
0 commit comments