kos/options.go

98 lines
2.2 KiB
Go
Raw Normal View History

2023-04-23 17:57:36 +08:00
package kos
import (
"context"
"git.nspix.com/golang/kos/util/env"
"git.nspix.com/golang/kos/util/ip"
2023-04-26 15:37:35 +08:00
"git.nspix.com/golang/kos/util/sys"
2023-04-23 17:57:36 +08:00
"os"
"strings"
"syscall"
)
type (
Options struct {
2023-05-31 11:14:15 +08:00
Name string
Version string
Address string
Port int
EnableDebug bool //开启调试模式
DisableHttp bool //禁用HTTP入口
EnableDirectHttp bool //启用HTTP直连模式
DisableCommand bool //禁用命令行入口
EnableDirectCommand bool //启用命令行直连模式
DisableStateApi bool //禁用系统状态接口
Metadata map[string]string //原数据
Context context.Context
Signals []os.Signal
server Server
shortName string
2023-04-23 17:57:36 +08:00
}
Option func(o *Options)
)
func (o *Options) ShortName() string {
if o.shortName != "" {
return o.shortName
}
if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
o.shortName = o.Name[pos+1:]
} else {
o.shortName = o.Name
}
return o.shortName
}
func WithName(name string, version string) Option {
return func(o *Options) {
o.Name = name
o.Version = version
}
}
func WithPort(port int) Option {
return func(o *Options) {
o.Port = port
}
}
func WithServer(s Server) Option {
return func(o *Options) {
o.server = s
}
}
func WithDebug() Option {
return func(o *Options) {
o.EnableDebug = true
}
}
2023-05-31 11:14:15 +08:00
func WithDirectHttp() Option {
return func(o *Options) {
o.DisableCommand = true
o.EnableDirectHttp = true
}
}
func WithDirectCommand() Option {
return func(o *Options) {
o.DisableHttp = true
o.EnableDirectCommand = true
}
}
2023-04-23 17:57:36 +08:00
func NewOptions() *Options {
opts := &Options{
2023-04-26 15:37:35 +08:00
Name: env.Get(EnvAppName, sys.Hostname()),
2023-04-23 17:57:36 +08:00
Version: env.Get(EnvAppVersion, "0.0.1"),
Context: context.Background(),
Metadata: make(map[string]string),
Signals: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL},
}
2023-05-31 11:14:15 +08:00
opts.Port = int(env.Integer(18080, EnvAppPort, "HTTP_PORT", "KOS_PORT"))
2023-07-07 09:53:37 +08:00
opts.Address = env.Getter(ip.Internal(), EnvAppAddress, "KOS_ADDRESS")
2023-04-23 17:57:36 +08:00
return opts
}