mirror of
https://github.com/hexolan/stocklet.git
synced 2026-03-26 19:51:17 +00:00
chore: initial commit
This commit is contained in:
114
internal/pkg/serve/gateway.go
Normal file
114
internal/pkg/serve/gateway.go
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package serve
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/metadata"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/config"
|
||||
"github.com/hexolan/stocklet/internal/pkg/gwauth"
|
||||
)
|
||||
|
||||
func withGatewayErrorHandler() runtime.ServeMuxOption {
|
||||
return runtime.WithErrorHandler(
|
||||
func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
|
||||
runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
|
||||
log.Error().Err(err).Stack().Str("path", r.URL.Path).Str("reqURI", r.RequestURI).Str("remoteAddr", r.RemoteAddr).Msg("")
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func withGatewayMetadataOpt() runtime.ServeMuxOption {
|
||||
return runtime.WithMetadata(
|
||||
func(ctx context.Context, req *http.Request) metadata.MD {
|
||||
return metadata.MD{"from-gateway": {"true"}}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func withGatewayHeaderOpt() runtime.ServeMuxOption {
|
||||
return runtime.WithIncomingHeaderMatcher(
|
||||
func(key string) (string, bool) {
|
||||
switch key {
|
||||
case gwauth.JWTPayloadHeader:
|
||||
// Envoy will validate JWT tokens and provide a payload header
|
||||
// containing a base64 string of the token claims.
|
||||
return "jwt-payload", true
|
||||
default:
|
||||
return key, false
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func withGatewayLogger(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
h.ServeHTTP(w, r)
|
||||
log.Info().Str("path", r.URL.Path).Str("reqURI", r.RequestURI).Str("remoteAddr", r.RemoteAddr).Msg("")
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func NewGatewayServeBase(cfg *config.SharedConfig) (*runtime.ServeMux, []grpc.DialOption) {
|
||||
// Create the base runtime ServeMux
|
||||
mux := runtime.NewServeMux(
|
||||
withGatewayErrorHandler(),
|
||||
withGatewayMetadataOpt(),
|
||||
withGatewayHeaderOpt(),
|
||||
)
|
||||
|
||||
// Attach open telemetry instrumentation through the gRPC client options
|
||||
clientOpts := []grpc.DialOption{
|
||||
grpc.WithStatsHandler(
|
||||
otelgrpc.NewClientHandler(),
|
||||
),
|
||||
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
}
|
||||
|
||||
return mux, clientOpts
|
||||
}
|
||||
|
||||
func Gateway(mux *runtime.ServeMux) error {
|
||||
// Create OTEL instrumentation handler
|
||||
handler := otelhttp.NewHandler(
|
||||
mux,
|
||||
"grpc-gateway",
|
||||
otelhttp.WithSpanNameFormatter(
|
||||
func(operation string, r *http.Request) string {
|
||||
return operation + ": " + r.RequestURI
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
// Create gateway HTTP server
|
||||
svr := &http.Server{
|
||||
Addr: GetAddrToGateway("0.0.0.0"),
|
||||
Handler: withGatewayLogger(handler),
|
||||
}
|
||||
|
||||
return svr.ListenAndServe()
|
||||
}
|
||||
62
internal/pkg/serve/grpc.go
Normal file
62
internal/pkg/serve/grpc.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package serve
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/health"
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
"google.golang.org/grpc/reflection"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/config"
|
||||
)
|
||||
|
||||
func NewGrpcServeBase(cfg *config.SharedConfig) *grpc.Server {
|
||||
// Attach OTEL metrics middleware
|
||||
svr := grpc.NewServer(
|
||||
grpc.StatsHandler(
|
||||
otelgrpc.NewServerHandler(),
|
||||
),
|
||||
)
|
||||
|
||||
// Attach the health service
|
||||
svc := health.NewServer()
|
||||
grpc_health_v1.RegisterHealthServer(svr, svc)
|
||||
|
||||
// Enable reflection in dev mode
|
||||
// Eases usage of tools like grpcurl and grpcui
|
||||
if cfg.DevMode {
|
||||
reflection.Register(svr)
|
||||
}
|
||||
|
||||
return svr
|
||||
}
|
||||
|
||||
func Grpc(svr *grpc.Server) {
|
||||
lis, err := net.Listen("tcp", GetAddrToGrpc("0.0.0.0"))
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Str("port", grpcPort).Msg("failed to listen on gRPC port")
|
||||
}
|
||||
|
||||
err = svr.Serve(lis)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to serve gRPC server")
|
||||
}
|
||||
}
|
||||
32
internal/pkg/serve/serve.go
Normal file
32
internal/pkg/serve/serve.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package serve
|
||||
|
||||
// Port Definitions
|
||||
const (
|
||||
grpcPort string = "9090"
|
||||
gatewayPort string = "90"
|
||||
)
|
||||
|
||||
// Get an address to a gRPC server using the standard port
|
||||
func GetAddrToGrpc(host string) string {
|
||||
return host + ":" + grpcPort
|
||||
}
|
||||
|
||||
// Get an address to a gRPC-gateway interface using the standard port
|
||||
func GetAddrToGateway(host string) string {
|
||||
return host + ":" + gatewayPort
|
||||
}
|
||||
Reference in New Issue
Block a user