81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package kos
|
|
|
|
import (
|
|
"context"
|
|
"git.nspix.com/golang/kos/util/env"
|
|
"git.nspix.com/golang/kos/util/ip"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
type (
|
|
Options struct {
|
|
Name string
|
|
Version string
|
|
Address string
|
|
Port int
|
|
EnableDebug bool //开启调试模式
|
|
DisableHttp bool //禁用HTTP入口
|
|
DisableCommand 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 NewOptions() *Options {
|
|
opts := &Options{
|
|
Name: env.Get(EnvAppName, ""),
|
|
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(EnvAppPort, 80))
|
|
opts.Address = env.Get(EnvAppAddress, ip.Internal())
|
|
return opts
|
|
}
|