@@ -3,12 +3,14 @@ package main
3
3
import (
4
4
"context"
5
5
"flag"
6
- "net/http"
7
-
6
+ "fmt"
8
7
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
9
8
"google.golang.org/grpc"
10
9
"google.golang.org/grpc/credentials/insecure"
11
10
"google.golang.org/grpc/grpclog"
11
+ "net/http"
12
+ "os"
13
+ "strings"
12
14
13
15
gw "github.com/npi-ai/npi/proto/go/api" // Update
14
16
)
@@ -23,17 +25,38 @@ func run() error {
23
25
ctx := context .Background ()
24
26
ctx , cancel := context .WithCancel (ctx )
25
27
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
+ }
27
36
// Register gRPC server endpoint
28
37
// Note: Make sure the gRPC server is running properly and accessible
29
- mux := runtime .NewServeMux ()
38
+ mux := runtime .NewServeMux (
39
+ runtime .WithIncomingHeaderMatcher (headerMatcher ),
40
+ )
30
41
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
+
31
53
err := gw .RegisterAppServerHandlerFromEndpoint (ctx , mux , * grpcServerEndpoint , opts )
32
54
if err != nil {
33
55
return err
34
56
}
35
57
36
58
// Start HTTP server (and proxy calls to gRPC server endpoint)
59
+ fmt .Printf ("Starting HTTP server on port 8081, endpoint: %s" , * grpcServerEndpoint )
37
60
return http .ListenAndServe (":8081" , mux )
38
61
}
39
62
0 commit comments