Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Oct 28, 2024
1 parent 76c13ef commit 5f2b890
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
33 changes: 0 additions & 33 deletions internal/configtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net"
"net/http"

"github.com/centrifugal/centrifuge"
)
Expand Down Expand Up @@ -117,38 +116,6 @@ type TransformedConnectErrorHttpResponse struct {
Body string `mapstructure:"body" json:"body" envconfig:"body" yaml:"body" toml:"body"`
}

func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatusTransform) (TransformedConnectErrorHttpResponse, bool) {
var code uint32
var body string
switch t := err.(type) {
case *centrifuge.Disconnect:
code = t.Code
body = t.Reason
case centrifuge.Disconnect:
code = t.Code
body = t.Reason
case *centrifuge.Error:
code = t.Code
body = t.Message
default:
}
if code > 0 {
for _, t := range transforms {
if t.Code != code {
continue
}
if t.ToResponse.Body == "" {
t.ToResponse.Body = body
}
return t.ToResponse, true
}
}
return TransformedConnectErrorHttpResponse{
StatusCode: http.StatusInternalServerError,
Body: http.StatusText(http.StatusInternalServerError),
}, false
}

type UniGRPC struct {
Enabled bool `mapstructure:"enabled" json:"enabled" envconfig:"enabled" yaml:"enabled" toml:"enabled"`
Address string `mapstructure:"address" json:"address" envconfig:"address" yaml:"address" toml:"address"`
Expand Down
35 changes: 35 additions & 0 deletions internal/tools/ascii_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tools

import "testing"

func Test_IsASCII(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "empty string",
input: "",
expected: true,
},
{
name: "ascii string",
input: "hello",
expected: true,
},
{
name: "non-ascii string",
input: "こんにちは",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if IsASCII(tt.input) != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, !tt.expected)
}
})
}
}
25 changes: 6 additions & 19 deletions internal/tools/code_translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@ package tools
import (
"net/http"

"github.com/centrifugal/centrifugo/v5/internal/configtypes"

"github.com/centrifugal/centrifuge"
)

type ConnectCodeToHTTPStatus struct {
Enabled bool `mapstructure:"enabled" json:"enabled"`
Transforms []ConnectCodeToHTTPStatusTransform `mapstructure:"transforms" json:"transforms"`
}

type ConnectCodeToHTTPStatusTransform struct {
Code uint32 `mapstructure:"code" json:"code"`
ToResponse TransformedConnectErrorHttpResponse `mapstructure:"to_response" json:"to_response"`
}

type TransformedConnectErrorHttpResponse struct {
Status int `mapstructure:"status_code" json:"status_code"`
Body string `mapstructure:"body" json:"body"`
}

func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatusTransform) (TransformedConnectErrorHttpResponse, bool) {
func ConnectErrorToToHTTPResponse(err error, transforms []configtypes.ConnectCodeToHTTPStatusTransform) (configtypes.TransformedConnectErrorHttpResponse, bool) {
var code uint32
var body string
switch t := err.(type) {
Expand All @@ -47,8 +34,8 @@ func ConnectErrorToToHTTPResponse(err error, transforms []ConnectCodeToHTTPStatu
return t.ToResponse, true
}
}
return TransformedConnectErrorHttpResponse{
Status: http.StatusInternalServerError,
Body: http.StatusText(http.StatusInternalServerError),
return configtypes.TransformedConnectErrorHttpResponse{
StatusCode: http.StatusInternalServerError,
Body: http.StatusText(http.StatusInternalServerError),
}, false
}
5 changes: 3 additions & 2 deletions internal/unihttpstream/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/http"
"time"

"github.com/centrifugal/centrifugo/v5/internal/tools"

"github.com/centrifugal/centrifuge"
"github.com/centrifugal/centrifugo/v5/internal/configtypes"
"github.com/centrifugal/protocol"
)

Expand Down Expand Up @@ -95,7 +96,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.config.ConnectCodeToHTTPStatus.Enabled {
err = c.ConnectNoErrorToDisconnect(connectRequest)
if err != nil {
resp, ok := configtypes.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms)
resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.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 @@ -6,7 +6,7 @@ import (
"net/http"
"time"

"github.com/centrifugal/centrifugo/v5/internal/configtypes"
"github.com/centrifugal/centrifugo/v5/internal/tools"

"github.com/centrifugal/centrifuge"
"github.com/centrifugal/protocol"
Expand Down Expand Up @@ -106,7 +106,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.config.ConnectCodeToHTTPStatus.Enabled {
err = c.ConnectNoErrorToDisconnect(connectRequest)
if err != nil {
resp, ok := configtypes.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms)
resp, ok := tools.ConnectErrorToToHTTPResponse(err, h.config.ConnectCodeToHTTPStatus.Transforms)
if ok {
w.WriteHeader(resp.StatusCode)
_, _ = w.Write([]byte(resp.Body))
Expand Down

0 comments on commit 5f2b890

Please sign in to comment.