diff --git a/internal/config/validate.go b/internal/config/validate.go index 49623764c..d2a262b69 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -132,10 +132,10 @@ func (c Config) Validate() error { consumerNames = append(consumerNames, config.Name) } - if err := validateConnectCodeTransforms(c.UniSSE.ConnectCodeToHTTPStatus.Transforms); err != nil { + if err := validateConnectCodeTransforms(c.UniSSE.ConnectCodeToHTTPResponse.Transforms); err != nil { return fmt.Errorf("in uni_sse.connect_code_to_http_status.transforms: %v", err) } - if err := validateConnectCodeTransforms(c.UniHTTPStream.ConnectCodeToHTTPStatus.Transforms); err != nil { + if err := validateConnectCodeTransforms(c.UniHTTPStream.ConnectCodeToHTTPResponse.Transforms); err != nil { return fmt.Errorf("in uni_http_stream.connect_code_to_http_status.transforms: %v", err) } @@ -300,12 +300,12 @@ func validateStatusTransforms(transforms []configtypes.HttpStatusToCodeTransform return nil } -func validateConnectCodeTransforms(transforms []configtypes.ConnectCodeToHTTPStatusTransform) error { +func validateConnectCodeTransforms(transforms []configtypes.ConnectCodeToHTTPResponseTransform) error { for i, transform := range transforms { if transform.Code == 0 { return fmt.Errorf("code should be set in connect_code_to_http_status.transforms[%d]", i) } - if transform.ToResponse.StatusCode == 0 { + if transform.To.StatusCode == 0 { return fmt.Errorf("status_code should be set in connect_code_to_http_status.transforms[%d].to_response", i) } } diff --git a/internal/configtypes/types.go b/internal/configtypes/types.go index 635e5862b..1f6eb05bc 100644 --- a/internal/configtypes/types.go +++ b/internal/configtypes/types.go @@ -75,25 +75,25 @@ type UniWebSocket struct { } type UniHTTPStream struct { - Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` - HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/connection/uni_http_stream" yaml:"handler_prefix" toml:"handler_prefix"` - MaxRequestBodySize int `mapstructure:"max_request_body_size" json:"max_request_body_size" envconfig:"max_request_body_size" default:"65536" yaml:"max_request_body_size" toml:"max_request_body_size"` - ConnectCodeToHTTPStatus ConnectCodeToHTTPStatus `mapstructure:"connect_code_to_http_status" json:"connect_code_to_http_status" envconfig:"connect_code_to_http_status" yaml:"connect_code_to_http_status" toml:"connect_code_to_http_status"` + Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` + HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/connection/uni_http_stream" yaml:"handler_prefix" toml:"handler_prefix"` + MaxRequestBodySize int `mapstructure:"max_request_body_size" json:"max_request_body_size" envconfig:"max_request_body_size" default:"65536" yaml:"max_request_body_size" toml:"max_request_body_size"` + ConnectCodeToHTTPResponse ConnectCodeToHTTPResponse `mapstructure:"connect_code_to_http_response" json:"connect_code_to_http_response" envconfig:"connect_code_to_http_response" yaml:"connect_code_to_http_response" toml:"connect_code_to_http_response"` } type UniSSE struct { - Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` - HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/connection/uni_sse" yaml:"handler_prefix" toml:"handler_prefix"` - MaxRequestBodySize int `mapstructure:"max_request_body_size" json:"max_request_body_size" envconfig:"max_request_body_size" default:"65536" yaml:"max_request_body_size" toml:"max_request_body_size"` - ConnectCodeToHTTPStatus ConnectCodeToHTTPStatus `mapstructure:"connect_code_to_http_status" json:"connect_code_to_http_status" envconfig:"connect_code_to_http_status" yaml:"connect_code_to_http_status" toml:"connect_code_to_http_status"` + Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` + HandlerPrefix string `mapstructure:"handler_prefix" json:"handler_prefix" envconfig:"handler_prefix" default:"/connection/uni_sse" yaml:"handler_prefix" toml:"handler_prefix"` + MaxRequestBodySize int `mapstructure:"max_request_body_size" json:"max_request_body_size" envconfig:"max_request_body_size" default:"65536" yaml:"max_request_body_size" toml:"max_request_body_size"` + ConnectCodeToHTTPResponse ConnectCodeToHTTPResponse `mapstructure:"connect_code_to_http_response" json:"connect_code_to_http_response" envconfig:"connect_code_to_http_response" yaml:"connect_code_to_http_response" toml:"connect_code_to_http_response"` } -type ConnectCodeToHTTPStatusTransforms []ConnectCodeToHTTPStatusTransform +type ConnectCodeToHTTPResponseTransforms []ConnectCodeToHTTPResponseTransform // Decode to implement the envconfig.Decoder interface -func (d *ConnectCodeToHTTPStatusTransforms) Decode(value string) error { +func (d *ConnectCodeToHTTPResponseTransforms) Decode(value string) error { // If the source is a string and the target is a slice, try to parse it as JSON. - var items ConnectCodeToHTTPStatusTransforms + var items ConnectCodeToHTTPResponseTransforms err := json.Unmarshal([]byte(value), &items) if err != nil { return fmt.Errorf("error parsing items from JSON: %v", err) @@ -102,14 +102,14 @@ func (d *ConnectCodeToHTTPStatusTransforms) Decode(value string) error { return nil } -type ConnectCodeToHTTPStatus struct { - Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` - Transforms ConnectCodeToHTTPStatusTransforms `mapstructure:"transforms" default:"[]" json:"transforms" envconfig:"transforms" yaml:"transforms" toml:"transforms"` +type ConnectCodeToHTTPResponse struct { + Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"` + Transforms ConnectCodeToHTTPResponseTransforms `mapstructure:"transforms" default:"[]" json:"transforms" envconfig:"transforms" yaml:"transforms" toml:"transforms"` } -type ConnectCodeToHTTPStatusTransform struct { - Code uint32 `mapstructure:"code" json:"code" envconfig:"code" yaml:"code" toml:"code"` - ToResponse TransformedConnectErrorHttpResponse `mapstructure:"to_response" json:"to_response" envconfig:"to_response" yaml:"to_response" toml:"to_response"` +type ConnectCodeToHTTPResponseTransform struct { + Code uint32 `mapstructure:"code" json:"code" envconfig:"code" yaml:"code" toml:"code"` + To TransformedConnectErrorHttpResponse `mapstructure:"to" json:"to" envconfig:"to" yaml:"to" toml:"to"` } type TransformedConnectErrorHttpResponse struct { diff --git a/internal/tools/code_translate.go b/internal/tools/code_translate.go index de413a18f..94e1898f6 100644 --- a/internal/tools/code_translate.go +++ b/internal/tools/code_translate.go @@ -8,7 +8,7 @@ import ( "github.com/centrifugal/centrifuge" ) -func ConnectErrorToToHTTPResponse(err error, transforms []configtypes.ConnectCodeToHTTPStatusTransform) (configtypes.TransformedConnectErrorHttpResponse, bool) { +func ConnectErrorToToHTTPResponse(err error, transforms []configtypes.ConnectCodeToHTTPResponseTransform) (configtypes.TransformedConnectErrorHttpResponse, bool) { var code uint32 var body string switch t := err.(type) { @@ -28,10 +28,10 @@ func ConnectErrorToToHTTPResponse(err error, transforms []configtypes.ConnectCod if t.Code != code { continue } - if t.ToResponse.Body == "" { - t.ToResponse.Body = body + if t.To.Body == "" { + t.To.Body = body } - return t.ToResponse, true + return t.To, true } } return configtypes.TransformedConnectErrorHttpResponse{ diff --git a/internal/unihttpstream/handler.go b/internal/unihttpstream/handler.go index 7defa9db7..581c04eb9 100644 --- a/internal/unihttpstream/handler.go +++ b/internal/unihttpstream/handler.go @@ -93,10 +93,10 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { connectRequest.Subs = subs } - if h.config.ConnectCodeToHTTPStatus.Enabled { + if h.config.ConnectCodeToHTTPResponse.Enabled { err = c.ConnectNoErrorToDisconnect(connectRequest) if err != nil { - resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) + resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPResponse.Transforms) if ok { w.WriteHeader(resp.StatusCode) _, _ = w.Write([]byte(resp.Body)) diff --git a/internal/unisse/handler.go b/internal/unisse/handler.go index 5a072f5d9..b582e8304 100644 --- a/internal/unisse/handler.go +++ b/internal/unisse/handler.go @@ -103,10 +103,10 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { connectRequest.Subs = subs } - if h.config.ConnectCodeToHTTPStatus.Enabled { + if h.config.ConnectCodeToHTTPResponse.Enabled { err = c.ConnectNoErrorToDisconnect(connectRequest) if err != nil { - resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms) + resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPResponse.Transforms) if ok { w.WriteHeader(resp.StatusCode) _, _ = w.Write([]byte(resp.Body))