Skip to content

Commit f3751b9

Browse files
authored
feat(fxhttpserver): Added support for listener address (ankorstore#202)
1 parent 53dfa9e commit f3751b9

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

docs/modules/fxhttpserver.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ It is recommended to keep routing registration separated from dependencies regis
7575
modules:
7676
http:
7777
server:
78-
port: 8080 # http server port (default 8080)
78+
address: ":8080" # http server listener address (default :8080)
7979
errors:
8080
obfuscate: false # to obfuscate error messages on the http server responses
8181
stack: false # to add error stack trace to error response of the http server

fxhttpserver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ modules:
103103
type: stdout
104104
http:
105105
server:
106-
port: 8080 # http server port (default 8080)
106+
address: ":8080" # http server listener address (default :8080)
107107
errors:
108108
obfuscate: false # to obfuscate error messages on the http server responses
109109
stack: false # to add error stack trace to error response of the http server

fxhttpserver/info.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// FxHttpServerModuleInfo is a module info collector for fxcore.
1111
type FxHttpServerModuleInfo struct {
12-
Port int
12+
Address string
1313
Debug bool
1414
Logger string
1515
Binder string
@@ -21,13 +21,13 @@ type FxHttpServerModuleInfo struct {
2121

2222
// NewFxHttpServerModuleInfo returns a new [FxHttpServerModuleInfo].
2323
func NewFxHttpServerModuleInfo(httpServer *echo.Echo, cfg *config.Config) *FxHttpServerModuleInfo {
24-
port := cfg.GetInt("modules.http.server.port")
25-
if port == 0 {
26-
port = DefaultPort
24+
address := cfg.GetString("modules.http.server.address")
25+
if address == "" {
26+
address = DefaultAddress
2727
}
2828

2929
return &FxHttpServerModuleInfo{
30-
Port: port,
30+
Address: address,
3131
Debug: httpServer.Debug,
3232
Logger: fmt.Sprintf("%T", httpServer.Logger),
3333
Binder: fmt.Sprintf("%T", httpServer.Binder),
@@ -46,7 +46,7 @@ func (i *FxHttpServerModuleInfo) Name() string {
4646
// Data return the data of the module info.
4747
func (i *FxHttpServerModuleInfo) Data() map[string]interface{} {
4848
return map[string]interface{}{
49-
"port": i.Port,
49+
"address": i.Address,
5050
"debug": i.Debug,
5151
"binder": i.Binder,
5252
"serializer": i.Serializer,

fxhttpserver/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestNewFxHttpServerModuleInfo(t *testing.T) {
2727
assert.Equal(
2828
t,
2929
map[string]interface{}{
30-
"port": fxhttpserver.DefaultPort,
30+
"address": fxhttpserver.DefaultAddress,
3131
"debug": true,
3232
"binder": "*echo.DefaultBinder",
3333
"serializer": "*echo.DefaultJSONSerializer",

fxhttpserver/module.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
)
1919

2020
const (
21-
ModuleName = "httpserver"
22-
DefaultPort = 8080
21+
ModuleName = "httpserver"
22+
DefaultAddress = ":8080"
2323
)
2424

2525
// FxHttpServerModule is the [Fx] httpserver module.
@@ -95,13 +95,13 @@ func NewFxHttpServer(p FxHttpServerParam) (*echo.Echo, error) {
9595
p.LifeCycle.Append(fx.Hook{
9696
OnStart: func(ctx context.Context) error {
9797
if !p.Config.IsTestEnv() {
98-
port := p.Config.GetInt("modules.http.server.port")
99-
if port == 0 {
100-
port = DefaultPort
98+
address := p.Config.GetString("modules.http.server.address")
99+
if address == "" {
100+
address = DefaultAddress
101101
}
102102

103103
//nolint:errcheck
104-
go httpServer.Start(fmt.Sprintf(":%d", port))
104+
go httpServer.Start(address)
105105
}
106106

107107
return nil

0 commit comments

Comments
 (0)