123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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"
- "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
- }
|