add grpc support
This commit is contained in:
@ -1,11 +1,26 @@
|
||||
package opts
|
||||
|
||||
import (
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
)
|
||||
|
||||
type GRPCOpts struct {
|
||||
*config.GRPCConfig
|
||||
*app.AppGRPC
|
||||
*AppGRPC
|
||||
}
|
||||
|
||||
type AppGRPC struct {
|
||||
Services []*GRPCService
|
||||
UnaryInterceptors []grpc.UnaryServerInterceptor
|
||||
StreamInterceptors []grpc.StreamServerInterceptor
|
||||
GRPCOpts []grpc.ServerOption
|
||||
GRPCDone <-chan error
|
||||
}
|
||||
|
||||
type GRPCService struct {
|
||||
Name string // Descriptive name of service
|
||||
Type *grpc.ServiceDesc // Type (from protoc generated code)
|
||||
Service any // Implementation of GRPCService.Type (ptr)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -27,20 +28,7 @@ var (
|
||||
defIdleTimeout = 15 * time.Second
|
||||
)
|
||||
|
||||
type HTTPFunc struct {
|
||||
Path string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
type HTTPServerOpts struct {
|
||||
Ctx context.Context
|
||||
HandleFuncs []HTTPFunc
|
||||
Middleware []http.Handler
|
||||
HealthCheckFuncs []HealthCheckFunc
|
||||
CustomListener net.Listener
|
||||
}
|
||||
|
||||
func prepHTTPServer(opts *HTTPServerOpts) *http.Server {
|
||||
func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
|
||||
var (
|
||||
cfg = config.MustFromCtx(opts.Ctx)
|
||||
l = zerolog.Ctx(opts.Ctx)
|
||||
@ -54,11 +42,11 @@ func prepHTTPServer(opts *HTTPServerOpts) *http.Server {
|
||||
mux.Handle(pattern, handler) // Associate pattern with handler
|
||||
}
|
||||
|
||||
healthChecks := handleHealthCheckFunc(opts.Ctx, opts.HealthCheckFuncs...)
|
||||
healthChecks := handleHealthCheckFunc(opts.Ctx, opts.HealthChecks...)
|
||||
otelHandleFunc("/health", healthChecks)
|
||||
otelHandleFunc("/", healthChecks)
|
||||
|
||||
for _, f := range opts.HandleFuncs {
|
||||
for _, f := range opts.Funcs {
|
||||
otelHandleFunc(f.Path, f.HandlerFunc)
|
||||
}
|
||||
|
||||
@ -130,7 +118,7 @@ func prepHTTPServer(opts *HTTPServerOpts) *http.Server {
|
||||
|
||||
// Returns a shutdown func and a done channel if the
|
||||
// server aborts abnormally. Returns error on failure to start
|
||||
func InitHTTPServer(opts *HTTPServerOpts) (
|
||||
func InitHTTPServer(opts *opts.AppHTTP) (
|
||||
func(context.Context) error, <-chan any, error,
|
||||
) {
|
||||
l := zerolog.Ctx(opts.Ctx)
|
||||
|
@ -9,11 +9,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
||||
)
|
||||
|
||||
type HealthCheckFunc func(context.Context) error
|
||||
|
||||
func handleHealthCheckFunc(ctx context.Context, hcFuncs ...HealthCheckFunc) func(w http.ResponseWriter, r *http.Request) {
|
||||
func handleHealthCheckFunc(ctx context.Context, hcFuncs ...opts.HealthCheckFunc) func(w http.ResponseWriter, r *http.Request) {
|
||||
// Return http handle func
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
|
23
pkg/srv/http/opts/http_config.go
Normal file
23
pkg/srv/http/opts/http_config.go
Normal file
@ -0,0 +1,23 @@
|
||||
package opts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type AppHTTP struct {
|
||||
Ctx context.Context
|
||||
Funcs []HTTPFunc
|
||||
Middleware []http.Handler
|
||||
HealthChecks []HealthCheckFunc
|
||||
CustomListener net.Listener
|
||||
HTTPDone <-chan any
|
||||
}
|
||||
|
||||
type HTTPFunc struct {
|
||||
Path string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
type HealthCheckFunc func(context.Context) error
|
Reference in New Issue
Block a user