options.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. DisableGateway bool //禁用HTTP和COMMAND入口
  20. DisableHttp bool //禁用HTTP入口
  21. EnableDirectHttp bool //启用HTTP直连模式
  22. DisableCommand bool //禁用COMMAND入口
  23. EnableDirectCommand bool //启用COMMAND直连模式
  24. DisableStateApi bool //禁用系统状态接口
  25. Metadata map[string]string //原数据
  26. Context context.Context
  27. Signals []os.Signal
  28. server Server
  29. shortName string
  30. }
  31. Option func(o *Options)
  32. HandleOptions struct {
  33. description string
  34. }
  35. HandleOption func(o *HandleOptions)
  36. )
  37. func (o *Options) ShortName() string {
  38. if o.shortName != "" {
  39. return o.shortName
  40. }
  41. if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
  42. o.shortName = o.Name[pos+1:]
  43. } else {
  44. o.shortName = o.Name
  45. }
  46. return o.shortName
  47. }
  48. func WithHandleDescription(s string) HandleOption {
  49. return func(o *HandleOptions) {
  50. o.description = s
  51. }
  52. }
  53. func WithName(name string, version string) Option {
  54. return func(o *Options) {
  55. o.Name = name
  56. o.Version = version
  57. }
  58. }
  59. func WithoutGateway() Option {
  60. return func(o *Options) {
  61. o.DisableGateway = true
  62. }
  63. }
  64. func WithPort(port int) Option {
  65. return func(o *Options) {
  66. o.Port = port
  67. }
  68. }
  69. func WithServer(s Server) Option {
  70. return func(o *Options) {
  71. o.server = s
  72. }
  73. }
  74. func WithDebug() Option {
  75. return func(o *Options) {
  76. o.EnableDebug = true
  77. }
  78. }
  79. func WithDirectHttp() Option {
  80. return func(o *Options) {
  81. o.DisableCommand = true
  82. o.EnableDirectHttp = true
  83. }
  84. }
  85. func WithDirectCommand() Option {
  86. return func(o *Options) {
  87. o.DisableHttp = true
  88. o.EnableDirectCommand = true
  89. }
  90. }
  91. func NewOptions(cbs ...Option) *Options {
  92. opts := &Options{
  93. Name: env.Get(EnvAppName, sys.Hostname()),
  94. Version: env.Get(EnvAppVersion, "0.0.1"),
  95. Context: context.Background(),
  96. Metadata: make(map[string]string),
  97. Signals: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL},
  98. }
  99. opts.Port = int(env.Integer(18080, EnvAppPort, "HTTP_PORT", "KOS_PORT"))
  100. opts.Address = env.Getter(ip.Internal(), EnvAppAddress, "KOS_ADDRESS")
  101. opts.EnableDebug, _ = strconv.ParseBool(env.Getter("false", EnvAppDebug, "KOS_DEBUG"))
  102. for _, cb := range cbs {
  103. cb(opts)
  104. }
  105. return opts
  106. }
  107. func newHandleOptions(cbs ...HandleOption) *HandleOptions {
  108. opts := &HandleOptions{}
  109. for _, cb := range cbs {
  110. cb(opts)
  111. }
  112. return opts
  113. }