options.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package yomo
  2. import (
  3. "crypto/tls"
  4. "log/slog"
  5. "github.com/quic-go/quic-go"
  6. "github.com/yomorun/yomo/core"
  7. "github.com/yomorun/yomo/core/router"
  8. )
  9. type (
  10. // SourceOption is option for the Source.
  11. SourceOption core.ClientOption
  12. // SfnOption is option for the SFN.
  13. SfnOption core.ClientOption
  14. )
  15. // SourceOption Options.
  16. var (
  17. // WithCredential sets the credential method for the Source.
  18. WithCredential = func(payload string) SourceOption { return SourceOption(core.WithCredential(payload)) }
  19. // WithSourceTLSConfig sets tls config for the Source.
  20. WithSourceTLSConfig = func(tc *tls.Config) SourceOption { return SourceOption(core.WithClientTLSConfig(tc)) }
  21. // WithSourceQuicConfig sets quic config for the Source.
  22. WithSourceQuicConfig = func(qc *quic.Config) SourceOption { return SourceOption(core.WithClientQuicConfig(qc)) }
  23. // WithLogger sets logger for the Source.
  24. WithLogger = func(l *slog.Logger) SourceOption { return SourceOption(core.WithLogger(l)) }
  25. // WithSourceReConnect makes source Connect until success, unless authentication fails.
  26. WithSourceReConnect = func() SourceOption { return SourceOption(core.WithReConnect()) }
  27. )
  28. // Sfn Options.
  29. var (
  30. // WithSfnCredential sets the credential method for the Sfn.
  31. WithSfnCredential = func(payload string) SfnOption { return SfnOption(core.WithCredential(payload)) }
  32. // WithSfnTLSConfig sets tls config for the Sfn.
  33. WithSfnTLSConfig = func(tc *tls.Config) SfnOption { return SfnOption(core.WithClientTLSConfig(tc)) }
  34. // WithSfnQuicConfig sets quic config for the Sfn.
  35. WithSfnQuicConfig = func(qc *quic.Config) SfnOption { return SfnOption(core.WithClientQuicConfig(qc)) }
  36. // WithSfnLogger sets logger for the Sfn.
  37. WithSfnLogger = func(l *slog.Logger) SfnOption { return SfnOption(core.WithLogger(l)) }
  38. // WithSfnReConnect makes sfn Connect until success, unless authentication fails.
  39. WithSfnReConnect = func() SfnOption { return SfnOption(core.WithReConnect()) }
  40. // WithSfnAIFunctionDefinition sets AI function definition for the Sfn.
  41. WithSfnAIFunctionDefinition = func(description string, inputModel any) SfnOption {
  42. return SfnOption(core.WithAIFunctionDefinition(description, inputModel))
  43. }
  44. // DisableOtelTrace determines whether to disable otel trace.
  45. DisableOtelTrace = func() SfnOption { return SfnOption(core.DisableOtelTrace()) }
  46. )
  47. // ClientOption is option for the upstream Zipper.
  48. type ClientOption = core.ClientOption
  49. type zipperOptions struct {
  50. serverOption []core.ServerOption
  51. clientOption []ClientOption
  52. }
  53. // ZipperOption is option for the Zipper.
  54. type ZipperOption func(*zipperOptions)
  55. var (
  56. // WithAuth sets the zipper authentication method.
  57. WithAuth = func(name string, args ...string) ZipperOption {
  58. return func(zo *zipperOptions) {
  59. zo.serverOption = append(zo.serverOption, core.WithAuth(name, args...))
  60. }
  61. }
  62. // WithZipperTLSConfig sets the TLS configuration for the zipper.
  63. WithZipperTLSConfig = func(tc *tls.Config) ZipperOption {
  64. return func(zo *zipperOptions) {
  65. zo.serverOption = append(zo.serverOption, core.WithServerTLSConfig(tc))
  66. }
  67. }
  68. // WithZipperQuicConfig sets the QUIC configuration for the zipper.
  69. WithZipperQuicConfig = func(qc *quic.Config) ZipperOption {
  70. return func(zo *zipperOptions) {
  71. zo.serverOption = append(zo.serverOption, core.WithServerQuicConfig(qc))
  72. }
  73. }
  74. // WithZipperLogger sets logger for the zipper.
  75. WithZipperLogger = func(l *slog.Logger) ZipperOption {
  76. return func(zo *zipperOptions) {
  77. zo.serverOption = append(zo.serverOption, core.WithServerLogger(l))
  78. }
  79. }
  80. // WithRouter sets router for the zipper.
  81. WithRouter = func(r router.Router) ZipperOption {
  82. return func(zo *zipperOptions) {
  83. zo.serverOption = append(zo.serverOption, core.WithRouter(r))
  84. }
  85. }
  86. // WithConnector sets connector for the zipper.
  87. WithConnector = func(c core.Connector) ZipperOption {
  88. return func(zo *zipperOptions) {
  89. zo.serverOption = append(zo.serverOption, core.WithConnector(c))
  90. }
  91. }
  92. // WithVersionNegotiateFunc sets the version negotiate function for the zipper
  93. WithVersionNegotiateFunc = func(f core.VersionNegotiateFunc) ZipperOption {
  94. return func(zo *zipperOptions) {
  95. zo.serverOption = append(zo.serverOption, core.WithVersionNegotiateFunc(f))
  96. }
  97. }
  98. // WithUpstreamOption provides upstream zipper options for Zipper.
  99. WithUpstreamOption = func(opts ...ClientOption) ZipperOption {
  100. return func(o *zipperOptions) {
  101. o.clientOption = opts
  102. }
  103. }
  104. // WithZipperConnMiddleware sets conn middleware for the zipper.
  105. WithZipperConnMiddleware = func(mw ...core.ConnMiddleware) ZipperOption {
  106. return func(o *zipperOptions) {
  107. o.serverOption = append(o.serverOption, core.WithConnMiddleware(mw...))
  108. }
  109. }
  110. // WithZipperFrameMiddleware sets frame middleware for the zipper.
  111. WithZipperFrameMiddleware = func(mw ...core.FrameMiddleware) ZipperOption {
  112. return func(o *zipperOptions) {
  113. o.serverOption = append(o.serverOption, core.WithFrameMiddleware(mw...))
  114. }
  115. }
  116. )