Sfoglia il codice sorgente

添加地址端口

fancl 1 anno fa
parent
commit
809c45c301
4 ha cambiato i file con 17 aggiunte e 9 eliminazioni
  1. 1 0
      constant.go
  2. 5 0
      instance.go
  3. 10 5
      options.go
  4. 1 4
      service.go

+ 1 - 0
constant.go

@@ -5,6 +5,7 @@ const (
 	EnvAppVersion = "VOX_VERSION"
 	EnvAppPort    = "VOX_PORT"
 	EnvAppAddress = "VOX_ADDRESS"
+	EnvAppDebug   = "VOX_DEBUG"
 )
 
 const (

+ 5 - 0
instance.go

@@ -40,6 +40,11 @@ func Http() *http.Server {
 	return std.Http()
 }
 
+func Debug() bool {
+	initApplication()
+	return std.opts.EnableDebug
+}
+
 func Command() *cli.Server {
 	initApplication()
 	return std.Command()

+ 10 - 5
options.go

@@ -6,16 +6,17 @@ import (
 	"git.nspix.com/golang/kos/util/ip"
 	"git.nspix.com/golang/kos/util/sys"
 	"os"
+	"strconv"
 	"strings"
 	"syscall"
 )
 
 type (
 	Options struct {
-		Name                string
-		Version             string
-		Address             string
-		Port                int
+		Name                string            //名称
+		Version             string            //版本号
+		Address             string            //绑定地址
+		Port                int               //端口
 		EnableDebug         bool              //开启调试模式
 		DisableHttp         bool              //禁用HTTP入口
 		EnableDirectHttp    bool              //启用HTTP直连模式
@@ -83,7 +84,7 @@ func WithDirectCommand() Option {
 	}
 }
 
-func NewOptions() *Options {
+func NewOptions(cbs ...Option) *Options {
 	opts := &Options{
 		Name:     env.Get(EnvAppName, sys.Hostname()),
 		Version:  env.Get(EnvAppVersion, "0.0.1"),
@@ -93,5 +94,9 @@ func NewOptions() *Options {
 	}
 	opts.Port = int(env.Integer(18080, EnvAppPort, "HTTP_PORT", "KOS_PORT"))
 	opts.Address = env.Getter(ip.Internal(), EnvAppAddress, "KOS_ADDRESS")
+	opts.EnableDebug, _ = strconv.ParseBool(env.Getter("false", EnvAppDebug, "KOS_DEBUG"))
+	for _, cb := range cbs {
+		cb(opts)
+	}
 	return opts
 }

+ 1 - 4
service.go

@@ -345,10 +345,7 @@ func (app *application) Run() (err error) {
 }
 
 func New(cbs ...Option) *application {
-	opts := NewOptions()
-	for _, cb := range cbs {
-		cb(opts)
-	}
+	opts := NewOptions(cbs...)
 	app := &application{
 		opts:   opts,
 		uptime: time.Now(),