Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template for healthcheck in gRPC #36

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions wrap/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
serverFileSuffix = "_server.go"
serverWrapperFileSuffix = "_gofr.go"
clientFileSuffix = "_client.go"
clientHealthFile = "health_client.go"
serverHealthFile = "health_gofr.go"
)

var (
Expand Down Expand Up @@ -57,18 +59,25 @@ type FileType struct {

// BuildGRPCGoFrClient generates gRPC client wrapper code based on a proto definition.
func BuildGRPCGoFrClient(ctx *gofr.Context) (any, error) {
gRPCClient := FileType{
FileSuffix: clientFileSuffix,
CodeGenerator: generateGoFrClient,
gRPCClient := []FileType{
{
FileSuffix: clientFileSuffix,
CodeGenerator: generateGoFrClient,
},
{
FileSuffix: clientHealthFile,
CodeGenerator: generateGoFrClientHealth,
},
}

return generateWrapper(ctx, gRPCClient)
return generateWrapper(ctx, gRPCClient...)
}

// BuildGRPCGoFrServer generates gRPC client and server code based on a proto definition.
func BuildGRPCGoFrServer(ctx *gofr.Context) (any, error) {
gRPCServer := []FileType{
{FileSuffix: serverWrapperFileSuffix, CodeGenerator: generateGoFrServerWrapper},
{FileSuffix: serverHealthFile, CodeGenerator: generateGoFrServerHealthWrapper},
{FileSuffix: serverFileSuffix, CodeGenerator: generateGoFrServer},
}

Expand Down Expand Up @@ -117,8 +126,18 @@ func generateWrapper(ctx *gofr.Context, options ...FileType) (any, error) {
return nil, ErrGeneratingWrapper
}

// Generate output file path based on service name and file suffix.
outputFilePath := path.Join(projectPath, strings.ToLower(service.Name)+option.FileSuffix)
var outputFilePath string
// Compare the CodeGenerator function reference
switch option.FileSuffix {
case clientHealthFile:
outputFilePath = path.Join(projectPath, clientHealthFile)
case serverHealthFile:
outputFilePath = path.Join(projectPath, serverHealthFile)
default:
// Use the service name for other cases
outputFilePath = path.Join(projectPath, strings.ToLower(service.Name)+option.FileSuffix)
}

if writeErr := os.WriteFile(outputFilePath, []byte(generatedCode), filePerm); writeErr != nil {
ctx.Errorf("Failed to write file %s: %v", outputFilePath, writeErr)
return nil, ErrWritingFile
Expand Down Expand Up @@ -154,6 +173,15 @@ func generateGoFrServerWrapper(ctx *gofr.Context, data *WrapperData) string {
return executeTemplate(ctx, data, wrapperTemplate)
}

// Generate GoFr server wrapper for gRPC using the wrapperTemplate.
func generateGoFrServerHealthWrapper(ctx *gofr.Context, data *WrapperData) string {
return executeTemplate(ctx, data, healthServerTemplate)
}

func generateGoFrClientHealth(ctx *gofr.Context, data *WrapperData) string {
return executeTemplate(ctx, data, clientHealthTemplate)
}

// Generate GoFr gRPCHandler code using the serverTemplate.
func generateGoFrServer(ctx *gofr.Context, data *WrapperData) string {
return executeTemplate(ctx, data, serverTemplate)
Expand Down
Loading
Loading