添加地址端口

This commit is contained in:
fancl 2023-10-23 15:43:46 +08:00
parent b91e28a512
commit 809c45c301
4 changed files with 17 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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