Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Nov 1, 2024
1 parent 162ccf6 commit bff3d8e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
34 changes: 17 additions & 17 deletions internal/configtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions internal/tools/code_translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/unihttpstream/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions internal/unisse/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit bff3d8e

Please sign in to comment.