github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/pkg/engine/engine.go (about)

     1  package engine
     2  
     3  import (
     4  	"github.com/abolfazlbeh/zhycan/internal/engine"
     5  	interalgRPC "github.com/abolfazlbeh/zhycan/internal/grpc"
     6  	"github.com/abolfazlbeh/zhycan/pkg/http"
     7  	"google.golang.org/grpc"
     8  	"strings"
     9  )
    10  
    11  // RegisterRestfulController - register the restful controller to the engine
    12  func RegisterRestfulController(app engine.RestfulApp) {
    13  	routes := app.Routes()
    14  
    15  	controllerName := app.GetName()
    16  	if strings.TrimSpace(controllerName) != "" {
    17  		http.AddHttpGroupByObj(http.HttpGroup{
    18  			GroupName: controllerName,
    19  			F:         nil,
    20  		})
    21  
    22  		for i, item := range routes {
    23  			routes[i].GroupNames = append([]string{controllerName}, item.GroupNames...)
    24  		}
    25  	}
    26  
    27  	http.AddBulkHttpRoutes(routes)
    28  }
    29  
    30  // Controller Struct - The controller struct to be used by others
    31  type Controller struct {
    32  	name string
    33  }
    34  
    35  func (c Controller) GetName() string {
    36  	return c.name
    37  }
    38  
    39  func RegisterGrpcController(app engine.GrpcApp, registerFunc func(server *grpc.Server, cls interface{})) {
    40  	controllerName := app.GetName()
    41  	serverNames := app.GetServerNames()
    42  
    43  	if strings.TrimSpace(controllerName) != "" {
    44  		return
    45  	}
    46  
    47  	if len(serverNames) <= 0 {
    48  		return
    49  	}
    50  
    51  	for _, item := range serverNames {
    52  		srv, err := interalgRPC.GetManager().GetServerByName(item)
    53  		if err != nil {
    54  			// TODO: log the error
    55  			continue
    56  		}
    57  		err = srv.RegisterController(registerFunc, app)
    58  		if err != nil {
    59  			// TODO: log the error
    60  			continue
    61  		}
    62  	}
    63  }