Skip to content

Commit 859e969

Browse files
committed
feat: support customized http header in gateway
1 parent db23004 commit 859e969

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ release-npi-cli:
3030
docker-build:
3131
docker buildx build --platform ${DOCKER_PLATFORM} -t npiai/npi:${IMAGE_TAG} . --push
3232

33+
docker-build-gateway:
34+
docker buildx build --platform linux/amd64 -t npiai/gateway:latest -f build/gateway.Dockerfile proto/go --push
35+
3336
docker-build-base:
3437
docker buildx build --platform ${DOCKER_PLATFORM} -t npiai/base:3.10 -f build/base.Dockerfile build --push
3538

build/gateway.Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM --platform=$BUILDPLATFORM golang as builder
2+
3+
LABEL maintainer="Wenfeng Wang <w@npi.ai>"
4+
5+
COPY . /npiai
6+
7+
WORKDIR /npiai
8+
9+
RUN GOOS=linux GOARCH=amd64 go build -o gateway main.go
10+
11+
FROM --platform=$BUILDPLATFORM ubuntu:22.04
12+
13+
COPY --from=builder /npiai/gateway /npiai/gateway
14+
15+
CMD ["/npiai/gateway"]

proto/go/test.go proto/go/main.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"context"
55
"flag"
6-
"net/http"
7-
6+
"fmt"
87
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
98
"google.golang.org/grpc"
109
"google.golang.org/grpc/credentials/insecure"
1110
"google.golang.org/grpc/grpclog"
11+
"net/http"
12+
"os"
13+
"strings"
1214

1315
gw "github.com/npi-ai/npi/proto/go/api" // Update
1416
)
@@ -23,17 +25,38 @@ func run() error {
2325
ctx := context.Background()
2426
ctx, cancel := context.WithCancel(ctx)
2527
defer cancel()
26-
28+
headerMatcher := func(key string) (string, bool) {
29+
switch strings.ToUpper(key) {
30+
case "X-NPI-TOKEN", "USER-AGENT", "ACCEPT":
31+
return key, true
32+
default:
33+
return key, false
34+
}
35+
}
2736
// Register gRPC server endpoint
2837
// Note: Make sure the gRPC server is running properly and accessible
29-
mux := runtime.NewServeMux()
38+
mux := runtime.NewServeMux(
39+
runtime.WithIncomingHeaderMatcher(headerMatcher),
40+
)
3041
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
42+
opts = append(opts, grpc.WithUnaryInterceptor(
43+
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
44+
// just for debug
45+
return invoker(ctx, method, req, reply, cc, opts...)
46+
}))
47+
48+
endpoint := os.Getenv("GRPC_SERVER_ENDPOINT")
49+
if endpoint != "" && *grpcServerEndpoint == "localhost:9140" {
50+
*grpcServerEndpoint = endpoint
51+
}
52+
3153
err := gw.RegisterAppServerHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
3254
if err != nil {
3355
return err
3456
}
3557

3658
// Start HTTP server (and proxy calls to gRPC server endpoint)
59+
fmt.Printf("Starting HTTP server on port 8081, endpoint: %s", *grpcServerEndpoint)
3760
return http.ListenAndServe(":8081", mux)
3861
}
3962

0 commit comments

Comments
 (0)