130 lines
2.9 KiB
Go
130 lines
2.9 KiB
Go
package kos
|
|
|
|
import (
|
|
"context"
|
|
"git.nobla.cn/golang/kos/util/env"
|
|
"git.nobla.cn/golang/kos/util/ip"
|
|
"git.nobla.cn/golang/kos/util/sys"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
type (
|
|
Options struct {
|
|
Name string //名称
|
|
Version string //版本号
|
|
Address string //绑定地址
|
|
Port int //端口
|
|
EnableDebug bool //开启调试模式
|
|
DisableGateway bool //禁用HTTP和COMMAND入口
|
|
DisableHttp bool //禁用HTTP入口
|
|
EnableDirectHttp bool //启用HTTP直连模式
|
|
DisableCommand bool //禁用COMMAND入口
|
|
EnableDirectCommand bool //启用COMMAND直连模式
|
|
DisableStateApi bool //禁用系统状态接口
|
|
Metadata map[string]string //原数据
|
|
Context context.Context
|
|
Signals []os.Signal
|
|
server Server
|
|
shortName string
|
|
}
|
|
|
|
Option func(o *Options)
|
|
|
|
HandleOptions struct {
|
|
description string
|
|
}
|
|
|
|
HandleOption func(o *HandleOptions)
|
|
)
|
|
|
|
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 WithHandleDescription(s string) HandleOption {
|
|
return func(o *HandleOptions) {
|
|
o.description = s
|
|
}
|
|
}
|
|
|
|
func WithName(name string, version string) Option {
|
|
return func(o *Options) {
|
|
o.Name = name
|
|
o.Version = version
|
|
}
|
|
}
|
|
|
|
func WithoutGateway() Option {
|
|
return func(o *Options) {
|
|
o.DisableGateway = true
|
|
}
|
|
}
|
|
|
|
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(cbs ...Option) *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")
|
|
opts.EnableDebug, _ = strconv.ParseBool(env.Getter("false", EnvAppDebug, "KOS_DEBUG"))
|
|
for _, cb := range cbs {
|
|
cb(opts)
|
|
}
|
|
return opts
|
|
}
|
|
|
|
func newHandleOptions(cbs ...HandleOption) *HandleOptions {
|
|
opts := &HandleOptions{}
|
|
for _, cb := range cbs {
|
|
cb(opts)
|
|
}
|
|
return opts
|
|
}
|