package kos import ( "context" "git.nspix.com/golang/kos/util/env" "git.nspix.com/golang/kos/util/ip" "git.nspix.com/golang/kos/util/sys" "os" "strings" "syscall" ) type ( Options struct { 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 } 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 } } 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 } } func NewOptions() *Options { opts := &Options{ Name: env.Get(EnvAppName, sys.Hostname()), 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}, } opts.Port = int(env.Integer(18080, EnvAppPort, "HTTP_PORT", "KOS_PORT")) opts.Address = env.Getter(ip.Internal(), EnvAppAddress, "KOS_ADDRESS") return opts }