Skip to content

Commit

Permalink
feat(fxgrpcserver): Added support for listener address (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Apr 5, 2024
1 parent 25956bd commit 4bf8968
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/modules/fxgrpcserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ icon: material/cube-outline

## Overview

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

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

Expand Down Expand Up @@ -57,7 +57,7 @@ var Bootstrapper = fxcore.NewBootstrapper().WithOptions(
modules:
grpc:
server:
port: 50051 # 50051 by default
address: ":50051" # gRPC server listener address (default :50051)
log:
metadata: # list of gRPC metadata to add to logs on top of x-request-id, empty by default
x-foo: foo # to log for example the metadata x-foo in the log field foo
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/fxhttpclient.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ icon: material/cube-outline

## Overview

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.
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.

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

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/fxhttpserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ icon: material/cube-outline

## Overview

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

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

Expand Down
2 changes: 1 addition & 1 deletion fxgrpcserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ modules:
type: stdout
grpc:
server:
port: 50051 # 50051 by default
address: ":50051" # gRPC server listener address (default :50051)
log:
metadata: # list of gRPC metadata to add to logs on top of x-request-id, empty by default
x-foo: foo # to log for example the metadata x-foo in the log field foo
Expand Down
12 changes: 6 additions & 6 deletions fxgrpcserver/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (

// FxGrpcServerModuleInfo is a module info collector for fxcore.
type FxGrpcServerModuleInfo struct {
Port int
Address string
Services map[string]grpc.ServiceInfo
}

// NewFxGrpcServerModuleInfo returns a new [FxGrpcServerModuleInfo].
func NewFxGrpcServerModuleInfo(grpcServer *grpc.Server, cfg *config.Config) *FxGrpcServerModuleInfo {
port := cfg.GetInt("modules.grpc.server.port")
if port == 0 {
port = DefaultPort
address := cfg.GetString("modules.grpc.server.address")
if address == "" {
address = DefaultAddress
}

return &FxGrpcServerModuleInfo{
Port: port,
Address: address,
Services: grpcServer.GetServiceInfo(),
}
}
Expand All @@ -32,7 +32,7 @@ func (i *FxGrpcServerModuleInfo) Name() string {
// Data return the data of the module info.
func (i *FxGrpcServerModuleInfo) Data() map[string]interface{} {
return map[string]interface{}{
"port": i.Port,
"address": i.Address,
"services": i.Services,
}
}
2 changes: 1 addition & 1 deletion fxgrpcserver/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestNewFxGrpcServerModuleInfo(t *testing.T) {
assert.Equal(
t,
map[string]interface{}{
"port": fxgrpcserver.DefaultPort,
"address": fxgrpcserver.DefaultAddress,
"services": map[string]grpc.ServiceInfo{},
},
info.Data(),
Expand Down
15 changes: 7 additions & 8 deletions fxgrpcserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fxgrpcserver

import (
"context"
"fmt"
"net"
"strconv"
"strings"
Expand All @@ -27,7 +26,7 @@ import (

const (
ModuleName = "grpcserver"
DefaultPort = 50051
DefaultAddress = ":50051"
DefaultBufconnSize = 1024 * 1024
)

Expand Down Expand Up @@ -131,23 +130,23 @@ func NewFxGrpcServer(p FxGrpcServerParam) (*grpc.Server, error) {
// lifecycles
p.LifeCycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
port := p.Config.GetInt("modules.grpc.server.port")
if port == 0 {
port = DefaultPort
address := p.Config.GetString("modules.grpc.server.address")
if address == "" {
address = DefaultAddress
}

go func() {
var lis net.Listener
if p.Config.IsTestEnv() {
lis = p.Listener
} else {
lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
lis, err = net.Listen("tcp", address)
if err != nil {
p.Logger.Error().Err(err).Msgf("failed to listen on %d for grpc server", port)
p.Logger.Error().Err(err).Msgf("failed to listen on %s for grpc server", address)
}
}

p.Logger.Info().Msgf("grpc server starting on port %d", port)
p.Logger.Info().Msgf("grpc server starting on %s", address)

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

0 comments on commit 4bf8968

Please sign in to comment.