Skip to content

Commit d58d6cb

Browse files
authored
fix: add server_host force listen to ipv4 (#566)
1 parent 0d1c3ab commit d58d6cb

6 files changed

Lines changed: 129 additions & 1 deletion

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
SERVER_HOST=0.0.0.0
12
SERVER_PORT=5002
23
SERVER_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi
34
GIN_MODE=release

internal/server/http_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (app *App) server(config *app.Config) func() {
7070
app.pprofGroup(pprofGroup, config)
7171

7272
srv := &http.Server{
73-
Addr: fmt.Sprintf(":%d", config.ServerPort),
73+
Addr: fmt.Sprintf("%s:%d", config.ServerHost, config.ServerPort),
7474
Handler: engine,
7575
}
7676

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package server
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
"testing"
7+
"time"
8+
9+
"github.com/langgenius/dify-plugin-daemon/internal/types/app"
10+
"github.com/langgenius/dify-plugin-daemon/pkg/utils/network"
11+
"github.com/langgenius/dify-plugin-daemon/pkg/utils/routine"
12+
)
13+
14+
func TestServerHostBinding(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
host string
18+
connectToHost string
19+
wantStatusCode int
20+
}{
21+
{
22+
name: "default host 0.0.0.0",
23+
host: "0.0.0.0",
24+
connectToHost: "127.0.0.1",
25+
wantStatusCode: http.StatusOK,
26+
},
27+
{
28+
name: "localhost",
29+
host: "127.0.0.1",
30+
connectToHost: "127.0.0.1",
31+
wantStatusCode: http.StatusOK,
32+
},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
port, err := network.GetRandomPort()
38+
if err != nil {
39+
t.Errorf("failed to get random port: %s", err.Error())
40+
return
41+
}
42+
43+
config := &app.Config{
44+
ServerPort: port,
45+
ServerHost: tt.host,
46+
ServerKey: "test-key",
47+
HealthApiLogEnabled: true,
48+
RoutinePoolSize: 100,
49+
}
50+
config.SetDefault()
51+
52+
routine.InitPool(config.RoutinePoolSize)
53+
54+
appInstance := &App{}
55+
cancel := appInstance.server(config)
56+
57+
if cancel == nil {
58+
t.Errorf("failed to start server")
59+
return
60+
}
61+
defer cancel()
62+
63+
time.Sleep(100 * time.Millisecond)
64+
65+
client := &http.Client{Timeout: 5 * time.Second}
66+
url := "http://" + tt.connectToHost + ":" + strconv.Itoa(int(config.ServerPort)) + "/health/check"
67+
68+
req, err := http.NewRequest("GET", url, nil)
69+
if err != nil {
70+
t.Errorf("failed to create request: %s", err.Error())
71+
return
72+
}
73+
74+
resp, err := client.Do(req)
75+
if err != nil {
76+
t.Errorf("failed to send request: %s", err.Error())
77+
return
78+
}
79+
defer resp.Body.Close()
80+
81+
if resp.StatusCode != tt.wantStatusCode {
82+
t.Errorf("expected status %d, got %d", tt.wantStatusCode, resp.StatusCode)
83+
}
84+
})
85+
}
86+
}

internal/types/app/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717

1818
type Config struct {
1919
// server
20+
ServerHost string `envconfig:"SERVER_HOST"`
2021
ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
2122
ServerKey string `envconfig:"SERVER_KEY" validate:"required"`
2223

internal/types/app/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func (config *Config) SetDefault() {
1010
case DB_TYPE_OCEANBASE, DB_TYPE_SEEKDB:
1111
config.DBType = DB_TYPE_MYSQL
1212
}
13+
setDefaultString(&config.ServerHost, "0.0.0.0")
1314
setDefaultInt(&config.ServerPort, 5002)
1415
setDefaultInt(&config.RoutinePoolSize, 10000)
1516
setDefaultInt(&config.LifetimeCollectionGCInterval, 60)

internal/types/app/default_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package app
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestServerHostDefault(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
inputHost string
11+
expectedHost string
12+
}{
13+
{
14+
name: "empty host should default to 0.0.0.0",
15+
inputHost: "",
16+
expectedHost: "0.0.0.0",
17+
},
18+
{
19+
name: "custom host should be preserved",
20+
inputHost: "127.0.0.1",
21+
expectedHost: "127.0.0.1",
22+
},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
config := &Config{
28+
ServerHost: tt.inputHost,
29+
ServerPort: 5002,
30+
ServerKey: "test-key",
31+
}
32+
config.SetDefault()
33+
34+
if config.ServerHost != tt.expectedHost {
35+
t.Errorf("expected ServerHost %s, got %s", tt.expectedHost, config.ServerHost)
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)