options.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package kos
  2. import (
  3. "context"
  4. "git.nspix.com/golang/kos/util/env"
  5. "git.nspix.com/golang/kos/util/ip"
  6. "git.nspix.com/golang/kos/util/sys"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "syscall"
  11. )
  12. type (
  13. Options struct {
  14. Name string //名称
  15. Version string //版本号
  16. Address string //绑定地址
  17. Port int //端口
  18. EnableDebug bool //开启调试模式
  19. DisableHttp bool //禁用HTTP入口
  20. EnableDirectHttp bool //启用HTTP直连模式
  21. DisableCommand bool //禁用命令行入口
  22. EnableDirectCommand bool //启用命令行直连模式
  23. DisableStateApi bool //禁用系统状态接口
  24. Metadata map[string]string //原数据
  25. Context context.Context
  26. Signals []os.Signal
  27. server Server
  28. shortName string
  29. }
  30. Option func(o *Options)
  31. HandleOptions struct {
  32. description string
  33. }
  34. HandleOption func(o *HandleOptions)
  35. )
  36. func (o *Options) ShortName() string {
  37. if o.shortName != "" {
  38. return o.shortName
  39. }
  40. if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
  41. o.shortName = o.Name[pos+1:]
  42. } else {
  43. o.shortName = o.Name
  44. }
  45. return o.shortName
  46. }
  47. func WithHandleDescription(s string) HandleOption {
  48. return func(o *HandleOptions) {
  49. o.description = s
  50. }
  51. }
  52. func WithName(name string, version string) Option {
  53. return func(o *Options) {
  54. o.Name = name
  55. o.Version = version
  56. }
  57. }
  58. func WithPort(port int) Option {
  59. return func(o *Options) {
  60. o.Port = port
  61. }
  62. }
  63. func WithServer(s Server) Option {
  64. return func(o *Options) {
  65. o.server = s
  66. }
  67. }
  68. func WithDebug() Option {
  69. return func(o *Options) {
  70. o.EnableDebug = true
  71. }
  72. }
  73. func WithDirectHttp() Option {
  74. return func(o *Options) {
  75. o.DisableCommand = true
  76. o.EnableDirectHttp = true
  77. }
  78. }
  79. func WithDirectCommand() Option {
  80. return func(o *Options) {
  81. o.DisableHttp = true
  82. o.EnableDirectCommand = true
  83. }
  84. }
  85. func NewOptions(cbs ...Option) *Options {
  86. opts := &Options{
  87. Name: env.Get(EnvAppName, sys.Hostname()),
  88. Version: env.Get(EnvAppVersion, "0.0.1"),
  89. Context: context.Background(),
  90. Metadata: make(map[string]string),
  91. Signals: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL},
  92. }
  93. opts.Port = int(env.Integer(18080, EnvAppPort, "HTTP_PORT", "KOS_PORT"))
  94. opts.Address = env.Getter(ip.Internal(), EnvAppAddress, "KOS_ADDRESS")
  95. opts.EnableDebug, _ = strconv.ParseBool(env.Getter("false", EnvAppDebug, "KOS_DEBUG"))
  96. for _, cb := range cbs {
  97. cb(opts)
  98. }
  99. return opts
  100. }
  101. func newHandleOptions(cbs ...HandleOption) *HandleOptions {
  102. opts := &HandleOptions{}
  103. for _, cb := range cbs {
  104. cb(opts)
  105. }
  106. return opts
  107. }