From 809c45c30114ee83e969e38aea4196a2d06cb60c Mon Sep 17 00:00:00 2001 From: fancl Date: Mon, 23 Oct 2023 15:43:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9C=B0=E5=9D=80=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constant.go | 1 + instance.go | 5 +++++ options.go | 15 ++++++++++----- service.go | 5 +---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/constant.go b/constant.go index 97742ca..78a97da 100644 --- a/constant.go +++ b/constant.go @@ -5,6 +5,7 @@ const ( EnvAppVersion = "VOX_VERSION" EnvAppPort = "VOX_PORT" EnvAppAddress = "VOX_ADDRESS" + EnvAppDebug = "VOX_DEBUG" ) const ( diff --git a/instance.go b/instance.go index 73e0a5e..2eb5e11 100644 --- a/instance.go +++ b/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() diff --git a/options.go b/options.go index c218500..b2f78d9 100644 --- a/options.go +++ b/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 } diff --git a/service.go b/service.go index 0b63c97..10b2892 100644 --- a/service.go +++ b/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(),