64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
// Package service defines generics around implementation of
|
|
// a new service that provides configuration for go-app
|
|
//
|
|
// The interface is meant to make the template more clear to implement
|
|
// without unnecessary changes in main.go
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
|
|
|
|
optsgrpc "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
|
optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
|
)
|
|
|
|
type ShutdownFunc func(ctx context.Context) (string, error)
|
|
|
|
type AppService interface {
|
|
Init(ctx context.Context, config *config.ServiceConfig) (ShutdownFunc, error)
|
|
GetGRPC() *optsgrpc.AppGRPC
|
|
GetHTTP() *optshttp.AppHTTP
|
|
}
|
|
|
|
// MergeServices is used if multiple services are served by this server
|
|
// with their own distinct packages and configuration
|
|
//
|
|
// You should be overriding the done chan, listener, or any other
|
|
// configuration used by the SERVER rather than the individual services.
|
|
func MergeServices(ctx context.Context, appServices ...AppService) (*optsgrpc.AppGRPC, *optshttp.AppHTTP) {
|
|
mergedGRPC := &optsgrpc.AppGRPC{}
|
|
for _, svc := range appServices {
|
|
mergedGRPC = mergeGRPC(mergedGRPC, svc.GetGRPC())
|
|
}
|
|
|
|
mergedHTTP := &optshttp.AppHTTP{}
|
|
for _, svc := range appServices {
|
|
mergedHTTP = mergeHTTP(mergedHTTP, svc.GetHTTP())
|
|
}
|
|
mergedHTTP.Ctx = ctx
|
|
|
|
return mergedGRPC, mergedHTTP
|
|
}
|
|
|
|
func mergeHTTP(a *optshttp.AppHTTP, b *optshttp.AppHTTP) *optshttp.AppHTTP {
|
|
return &optshttp.AppHTTP{
|
|
Funcs: append(a.Funcs, b.Funcs...),
|
|
Middleware: append(a.Middleware, b.Middleware...),
|
|
Handlers: append(a.Handlers, b.Handlers...),
|
|
HealthChecks: append(a.HealthChecks, b.HealthChecks...),
|
|
}
|
|
}
|
|
|
|
func mergeGRPC(a *optsgrpc.AppGRPC, b *optsgrpc.AppGRPC) *optsgrpc.AppGRPC {
|
|
return &optsgrpc.AppGRPC{
|
|
Services: append(a.Services, b.Services...),
|
|
GRPCOpts: append(a.GRPCOpts, b.GRPCOpts...),
|
|
UnaryInterceptors: append(a.UnaryInterceptors, b.UnaryInterceptors...),
|
|
StreamInterceptors: append(a.StreamInterceptors, b.StreamInterceptors...),
|
|
GRPCDialOpts: append(a.GRPCDialOpts, b.GRPCDialOpts...),
|
|
GRPCGatewayOpts: append(a.GRPCGatewayOpts, b.GRPCGatewayOpts...),
|
|
}
|
|
}
|