Skip to content

Commit 4bf8968

Browse files
authored
feat(fxgrpcserver): Added support for listener address (#204)
1 parent 25956bd commit 4bf8968

File tree

7 files changed

+19
-20
lines changed

7 files changed

+19
-20
lines changed

docs/modules/fxgrpcserver.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ icon: material/cube-outline
1313

1414
## Overview
1515

16-
Yokai provides a [fxgrpcserver](https://github.com/ankorstore/yokai/tree/main/fxgrpcserver) module, providing an [gRPC](https://grpc.io/) server to your application.
16+
Yokai provides a [fxgrpcserver](https://github.com/ankorstore/yokai/tree/main/fxgrpcserver) module, offering an [gRPC](https://grpc.io/) server to your application.
1717

1818
It wraps the [grpcserver](https://github.com/ankorstore/yokai/tree/main/grpcserver) module, based on [gRPC-Go](https://github.com/grpc/grpc-go).
1919

@@ -57,7 +57,7 @@ var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
5757
modules:
5858
grpc:
5959
server:
60-
port: 50051 # 50051 by default
60+
address: ":50051" # gRPC server listener address (default :50051)
6161
log:
6262
metadata: # list of gRPC metadata to add to logs on top of x-request-id, empty by default
6363
x-foo: foo # to log for example the metadata x-foo in the log field foo

docs/modules/fxhttpclient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ icon: material/cube-outline
1313

1414
## Overview
1515

16-
Yokai provides a [fxhttpclient](https://github.com/ankorstore/yokai/tree/main/fxhttpclient) module, providing a ready to use [Client](https://pkg.go.dev/net/http#Client) to your application.
16+
Yokai provides a [fxhttpclient](https://github.com/ankorstore/yokai/tree/main/fxhttpclient) module, offering a ready to use [Client](https://pkg.go.dev/net/http#Client) to your application.
1717

1818
It wraps the [httpclient](https://github.com/ankorstore/yokai/tree/main/httpclient) module, based on [net/http](https://pkg.go.dev/net/http).
1919

docs/modules/fxhttpserver.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ icon: material/cube-outline
1313

1414
## Overview
1515

16-
Yokai provides a [fxhttpserver](https://github.com/ankorstore/yokai/tree/main/fxhttpserver) module, providing an HTTP server to your application.
16+
Yokai provides a [fxhttpserver](https://github.com/ankorstore/yokai/tree/main/fxhttpserver) module, offering an HTTP server to your application.
1717

1818
It wraps the [httpserver](https://github.com/ankorstore/yokai/tree/main/httpserver) module, based on [Echo](https://echo.labstack.com/).
1919

fxgrpcserver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ modules:
108108
type: stdout
109109
grpc:
110110
server:
111-
port: 50051 # 50051 by default
111+
address: ":50051" # gRPC server listener address (default :50051)
112112
log:
113113
metadata: # list of gRPC metadata to add to logs on top of x-request-id, empty by default
114114
x-foo: foo # to log for example the metadata x-foo in the log field foo

fxgrpcserver/info.go

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

88
// FxGrpcServerModuleInfo is a module info collector for fxcore.
99
type FxGrpcServerModuleInfo struct {
10-
Port int
10+
Address string
1111
Services map[string]grpc.ServiceInfo
1212
}
1313

1414
// NewFxGrpcServerModuleInfo returns a new [FxGrpcServerModuleInfo].
1515
func NewFxGrpcServerModuleInfo(grpcServer *grpc.Server, cfg *config.Config) *FxGrpcServerModuleInfo {
16-
port := cfg.GetInt("modules.grpc.server.port")
17-
if port == 0 {
18-
port = DefaultPort
16+
address := cfg.GetString("modules.grpc.server.address")
17+
if address == "" {
18+
address = DefaultAddress
1919
}
2020

2121
return &FxGrpcServerModuleInfo{
22-
Port: port,
22+
Address: address,
2323
Services: grpcServer.GetServiceInfo(),
2424
}
2525
}
@@ -32,7 +32,7 @@ func (i *FxGrpcServerModuleInfo) Name() string {
3232
// Data return the data of the module info.
3333
func (i *FxGrpcServerModuleInfo) Data() map[string]interface{} {
3434
return map[string]interface{}{
35-
"port": i.Port,
35+
"address": i.Address,
3636
"services": i.Services,
3737
}
3838
}

fxgrpcserver/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestNewFxGrpcServerModuleInfo(t *testing.T) {
2626
assert.Equal(
2727
t,
2828
map[string]interface{}{
29-
"port": fxgrpcserver.DefaultPort,
29+
"address": fxgrpcserver.DefaultAddress,
3030
"services": map[string]grpc.ServiceInfo{},
3131
},
3232
info.Data(),

fxgrpcserver/module.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package fxgrpcserver
22

33
import (
44
"context"
5-
"fmt"
65
"net"
76
"strconv"
87
"strings"
@@ -27,7 +26,7 @@ import (
2726

2827
const (
2928
ModuleName = "grpcserver"
30-
DefaultPort = 50051
29+
DefaultAddress = ":50051"
3130
DefaultBufconnSize = 1024 * 1024
3231
)
3332

@@ -131,23 +130,23 @@ func NewFxGrpcServer(p FxGrpcServerParam) (*grpc.Server, error) {
131130
// lifecycles
132131
p.LifeCycle.Append(fx.Hook{
133132
OnStart: func(ctx context.Context) error {
134-
port := p.Config.GetInt("modules.grpc.server.port")
135-
if port == 0 {
136-
port = DefaultPort
133+
address := p.Config.GetString("modules.grpc.server.address")
134+
if address == "" {
135+
address = DefaultAddress
137136
}
138137

139138
go func() {
140139
var lis net.Listener
141140
if p.Config.IsTestEnv() {
142141
lis = p.Listener
143142
} else {
144-
lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
143+
lis, err = net.Listen("tcp", address)
145144
if err != nil {
146-
p.Logger.Error().Err(err).Msgf("failed to listen on %d for grpc server", port)
145+
p.Logger.Error().Err(err).Msgf("failed to listen on %s for grpc server", address)
147146
}
148147
}
149148

150-
p.Logger.Info().Msgf("grpc server starting on port %d", port)
149+
p.Logger.Info().Msgf("grpc server starting on %s", address)
151150

152151
if err = grpcServer.Serve(lis); err != nil {
153152
p.Logger.Error().Err(err).Msg("failed to serve grpc server")

0 commit comments

Comments
 (0)