Skip to content

Commit

Permalink
configure translates
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Oct 20, 2024
1 parent eeb608d commit 371b0fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
10 changes: 5 additions & 5 deletions internal/tools/code_translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/centrifugal/centrifuge"
)

type ConnectCodeToHTTPStatusTranslates struct {
Enabled bool
Translates []ConnectCodeToHTTPStatusTranslate
type ConnectCodeToHTTPStatus struct {
Enabled bool `mapstructure:"enabled" json:"enabled"`
Translates []ConnectCodeToHTTPStatusTranslate `mapstructure:"translates" json:"translates"`
}

type ConnectCodeToHTTPStatusTranslate struct {
Code uint32
ToStatusCode int
Code uint32 `mapstructure:"code" json:"code"`
ToStatusCode int `mapstructure:"to_status_code" json:"to_status_code"`
}

type TranslatedHttpResponse struct {
Expand Down
48 changes: 21 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ var defaults = map[string]any{
"uni_sse": false,
"uni_http_stream": false,

"uni_sse_connect_code_to_http_status.enabled": false,
"uni_sse_connect_code_to_http_status.translates": []any{},
"uni_http_stream_connect_code_to_http_status.enabled": false,
"uni_http_stream_connect_code_to_http_status.translates": []any{},

"log_level": "info",
"log_file": "",

Expand Down Expand Up @@ -2070,9 +2075,6 @@ func proxyMapConfig() (*client.ProxyMap, bool) {
if v.IsSet("proxy_http_status_translate") {
tools.DecodeSlice(v, &httpStatusTranslate, "proxy_http_status_translate")
}
if err != nil {
log.Fatal().Msgf("error unmarshaling proxy_http_status_translate: %v", err)
}
for _, translate := range httpStatusTranslate {
if translate.Status == 0 {
log.Fatal().Msg("proxy_http_status_translate status should be set")
Expand Down Expand Up @@ -2564,41 +2566,33 @@ func uniWebsocketHandlerConfig() uniws.Config {
}

func uniSSEHandlerConfig() unisse.Config {
connectCodeToHttpStatusEnabled := viper.GetBool("uni_sse_connect_code_to_http_status.enabled")
var connectCodeToHttpStatusTranslates []tools.ConnectCodeToHTTPStatusTranslate
if viper.IsSet("uni_sse_connect_code_to_http_status.translates") {
tools.DecodeSlice(viper.GetViper(), &connectCodeToHttpStatusTranslates, "uni_sse_connect_code_to_http_status.translates")
}
return unisse.Config{
MaxRequestBodySize: viper.GetInt("uni_sse_max_request_body_size"),
PingPongConfig: getPingPongConfig(),
ConnectCodeTranslates: tools.ConnectCodeToHTTPStatusTranslates{
Enabled: true,
Translates: []tools.ConnectCodeToHTTPStatusTranslate{
{
Code: 4503,
ToStatusCode: 403,
},
{
Code: 4504,
ToStatusCode: 404,
},
},
ConnectCodeTranslates: tools.ConnectCodeToHTTPStatus{
Enabled: connectCodeToHttpStatusEnabled,
Translates: connectCodeToHttpStatusTranslates,
},
}
}

func uniStreamHandlerConfig() unihttpstream.Config {
connectCodeToHttpStatusEnabled := viper.GetBool("uni_http_stream_connect_code_to_http_status.enabled")
var connectCodeToHttpStatusTranslates []tools.ConnectCodeToHTTPStatusTranslate
if viper.IsSet("uni_http_stream_connect_code_to_http_status.translates") {
tools.DecodeSlice(viper.GetViper(), &connectCodeToHttpStatusTranslates, "uni_http_stream_connect_code_to_http_status.translates")
}
return unihttpstream.Config{
MaxRequestBodySize: viper.GetInt("uni_http_stream_max_request_body_size"),
PingPongConfig: getPingPongConfig(),
ConnectCodeTranslates: tools.ConnectCodeToHTTPStatusTranslates{
Enabled: true,
Translates: []tools.ConnectCodeToHTTPStatusTranslate{
{
Code: 4503,
ToStatusCode: 403,
},
{
Code: 4504,
ToStatusCode: 404,
},
},
ConnectCodeTranslates: tools.ConnectCodeToHTTPStatus{
Enabled: connectCodeToHttpStatusEnabled,
Translates: connectCodeToHttpStatusTranslates,
},
}
}
Expand Down

0 comments on commit 371b0fa

Please sign in to comment.