Skip to content

Commit

Permalink
fix gateway port inconsistent setting issue (in-memory vs config file) (
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Mar 12, 2024
1 parent eb0a926 commit c63f7c5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ func init() {
panic(err)
}

_state.OnGatewayPortChange(func(s string) error {
config.Set(common.ConfigKeyGatewayPort, _state.GetGatewayPort())
config.Set(common.ConfigKeyRuntimePath, _state.GetRuntimePath())

_state.OnGatewayPortChange(func(port string) error {
config.Set(common.ConfigKeyGatewayPort, port)
return config.WriteConfig()
})
}
Expand Down
73 changes: 73 additions & 0 deletions route/management_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package route
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -134,3 +135,75 @@ func TestChangePort(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, expectedPort, result.Data)
}

func TestChangePortNegative(t *testing.T) {
defer setup(t)(t)

expectedPort := "123"

// set
request := &model.ChangePortRequest{
Port: expectedPort,
}

body, err := json.Marshal(request)
assert.NilError(t, err)

req, _ := http.NewRequest(http.MethodPut, "/v1/gateway/port", bytes.NewReader(body))
req.RemoteAddr = "127.0.0.1:0"

w := httptest.NewRecorder()
_router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, expectedPort, "123")

// get
req, _ = http.NewRequest(http.MethodGet, "/v1/gateway/port", nil)

w = httptest.NewRecorder()
_router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)

var result *model.Result
decoder := json.NewDecoder(w.Body)

err = decoder.Decode(&result)
assert.NilError(t, err)
assert.Equal(t, expectedPort, result.Data)

// emulate error
_state.OnGatewayPortChange(func(_ string) error {
return errors.New("error")
})

// set
request.Port = "456"

body, err = json.Marshal(request)
assert.NilError(t, err)

req, _ = http.NewRequest(http.MethodPut, "/v1/gateway/port", bytes.NewReader(body))
req.RemoteAddr = "127.0.0.1:0"

w = httptest.NewRecorder()
_router.ServeHTTP(w, req)

assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, expectedPort, "123")

// get
req, _ = http.NewRequest(http.MethodGet, "/v1/gateway/port", nil)

w = httptest.NewRecorder()
_router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)

decoder = json.NewDecoder(w.Body)

err = decoder.Decode(&result)
assert.NilError(t, err)
assert.Equal(t, expectedPort, result.Data)
}
14 changes: 9 additions & 5 deletions service/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ func NewState() *State {
}
}

func (c *State) SetGatewayPort(port string) error {
c.gatewayPort = port
return c.notifyOnGatewayPortChange()
func (c *State) SetGatewayPort(port string) (err error) {
defer func() {
if err == nil {
c.gatewayPort = port
}
}()
return c.notifyOnGatewayPortChange(port)
}

func (c *State) GetGatewayPort() string {
Expand All @@ -32,9 +36,9 @@ func (c *State) OnGatewayPortChange(f func(string) error) {
c.onGatewayPortChange = append(c.onGatewayPortChange, f)
}

func (c *State) notifyOnGatewayPortChange() error {
func (c *State) notifyOnGatewayPortChange(port string) error {
for i := len(c.onGatewayPortChange) - 1; i >= 0; i-- {
if err := c.onGatewayPortChange[i](c.gatewayPort); err != nil {
if err := c.onGatewayPortChange[i](port); err != nil {
return err
}
}
Expand Down

0 comments on commit c63f7c5

Please sign in to comment.