Skip to content

Commit

Permalink
expose silver surfer as a mvc via grpc code incorporated in this commit
Browse files Browse the repository at this point in the history
  • Loading branch information
prakash100198 committed Nov 4, 2024
1 parent cb3cad6 commit 663f602
Show file tree
Hide file tree
Showing 960 changed files with 143,798 additions and 41,413 deletions.
20 changes: 20 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build wireinject
// +build wireinject

package main

import (
"github.com/devtron-labs/silver-surfer/app"
"github.com/devtron-labs/silver-surfer/app/api"
"github.com/devtron-labs/silver-surfer/app/logger"
"github.com/google/wire"
)

func InitializeApp() (*app.App, error) {
wire.Build(
app.NewApp,
logger.NewSugaredLogger,
api.NewGrpcHandlerImpl,
)
return &app.App{}, nil
}
110 changes: 110 additions & 0 deletions app/App.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package app

import (
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/silver-surfer/app/api"
grpc2 "github.com/devtron-labs/silver-surfer/app/grpc"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
"log"
"net"
"os"
"runtime/debug"
"time"
)

type App struct {
grpcServer *grpc.Server
StartupConfig *StartupConfig
logger *zap.SugaredLogger
GrpcHandler *api.GrpcHandlerImpl
}

func NewApp(
logger *zap.SugaredLogger,
GrpcHandler *api.GrpcHandlerImpl,
) *App {
return &App{
logger: logger,
GrpcHandler: GrpcHandler,
}
}

type StartupConfig struct {
GrpcPort int `env:"SERVER_GRPC_PORT" envDefault:"8111"`
GrpcMaxRecvMsgSize int `env:"GRPC_MAX_RECEIVE_MSG_SIZE" envDefault:"20"` // In mb
GrpcMaxSendMsgSize int `env:"GRPC_MAX_SEND_MSG_SIZE" envDefault:"4"` // In mb
}

func (app *App) Start() {
// Parse config
app.StartupConfig = &StartupConfig{}
err := env.Parse(app.StartupConfig)
if err != nil {
app.logger.Errorw("failed to parse configuration")
os.Exit(2)
}

// Start gRPC server
err = app.initGrpcServer(app.StartupConfig.GrpcPort)
if err != nil {
app.logger.Errorw("error starting grpc server", "err", err)
os.Exit(2)
}
}

func (app *App) initGrpcServer(port int) error {
app.logger.Infow("gRPC server starting", "port", app.StartupConfig.GrpcPort)

//listen on the port
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to start grpcServer %v", err)
return err
}

grpcPanicRecoveryHandler := func(p any) (err error) {
app.logger.Error(PanicLogIdentifier, "recovered from panic", "panic", p, "stack", string(debug.Stack()))
return status.Errorf(codes.Internal, "%s", p)
}
recoveryOption := recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)
opts := []grpc.ServerOption{
grpc.MaxRecvMsgSize(app.StartupConfig.GrpcMaxRecvMsgSize * 1024 * 1024), // GRPC Request size
grpc.MaxSendMsgSize(app.StartupConfig.GrpcMaxSendMsgSize * 1024 * 1024), // GRPC Response size
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionAge: 10 * time.Second,
}),
grpc.ChainStreamInterceptor(
recovery.StreamServerInterceptor(recoveryOption)), // panic interceptor, should be at last
grpc.ChainUnaryInterceptor(
recovery.UnaryServerInterceptor(recoveryOption)), // panic interceptor, should be at last
}
// create a new gRPC grpcServer
app.grpcServer = grpc.NewServer(opts...)

// register Silver Surfer service
grpc2.RegisterApplicationServiceServer(app.grpcServer, app.GrpcHandler)

// start listening on address
if err = app.grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to start: %v", err)
return err
}
return nil
}

func (app *App) Stop() {

app.logger.Infow("silver-surfer shutdown initiating")

// Gracefully stop the gRPC server
app.logger.Info("Stopping gRPC server...")
app.grpcServer.GracefulStop()

app.logger.Infow("housekeeping done. exiting now")
}
5 changes: 5 additions & 0 deletions app/Constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app

const (
PanicLogIdentifier = "DEVTRON_PANIC_RECOVER"
)
22 changes: 22 additions & 0 deletions app/api/GrpcHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"context"
"github.com/devtron-labs/silver-surfer/app/grpc"
"go.uber.org/zap"
)

type GrpcHandlerImpl struct {
grpc.UnimplementedApplicationServiceServer
logger *zap.SugaredLogger
}

func NewGrpcHandlerImpl(logger *zap.SugaredLogger) *GrpcHandlerImpl {
return &GrpcHandlerImpl{
logger: logger,
}
}

func (impl *GrpcHandlerImpl) GetClusterUpgradeSummaryValidationResult(ctx context.Context, request *grpc.ClusterUpgradeRequest) (*grpc.ClusterUpgradeResponse, error) {

}
Loading

0 comments on commit 663f602

Please sign in to comment.