-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
88 lines (72 loc) · 2.32 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package proxy
import (
"context"
"net/http"
"time"
"github.com/clavinjune/magellan/gen"
magellanv1 "github.com/clavinjune/magellan/api/proto/magellan/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/clavinjune/gokit/grpcutil"
"github.com/clavinjune/magellan/pkg/component"
"golang.org/x/exp/slog"
"github.com/clavinjune/gokit/cobrautil"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/protobuf/proto"
"github.com/spf13/cobra"
)
var Cmd = &cobra.Command{
Use: "proxy",
Short: "run magellan proxy server",
}
func Init(cmd *cobra.Command) *cobra.Command {
cmd.RunE = RunE
cmd.Flags().IntP("grpc-port", "g", 0, "gRPC port (default random)")
cmd.Flags().IntP("proxy-port", "p", 8000, "proxy port")
return cmd
}
func RunE(cmd *cobra.Command, args []string) error {
wire, cleanup, err := component.Wire(component.Option{
Logger: slog.Default(),
Port: grpcPort(cmd),
ServerOption: nil,
ProxyPort: proxyPort(cmd),
ProxyOption: []runtime.ServeMuxOption{
grpcutil.RemoveOutgoingHeader(nil),
},
ProxyOpenAPIHandler: gen.MustGetOpenAPIHandler(),
})
if err != nil {
return err
}
defer cleanup()
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
wire.Proxy.Register(func(server *grpc.Server, mux *runtime.ServeMux, endpoint string) {
magellanv1.RegisterAuthenticationServiceServer(server, wire.Authentication)
magellanv1.RegisterAuthenticationServiceHandlerFromEndpoint(
cmd.Context(), mux, endpoint, dialOpts)
}).Listen(cmd.Context())
return nil
}
func grpcPort(cmd *cobra.Command) grpcutil.ServerPort {
return grpcutil.ServerPort(cobrautil.MustGetIntFlag(cmd, "grpc-port"))
}
func proxyPort(cmd *cobra.Command) grpcutil.ProxyPort {
return grpcutil.ProxyPort(cobrautil.MustGetIntFlag(cmd, "proxy-port"))
}
func x() runtime.ServeMuxOption {
return runtime.WithForwardResponseOption(func(ctx context.Context, w http.ResponseWriter, message proto.Message) error {
smd, _ := runtime.ServerMetadataFromContext(ctx)
if y := smd.HeaderMD.Get("testing"); len(y) >= 1 {
http.SetCookie(w, &http.Cookie{
Name: "testing",
Value: y[0],
Path: "/",
Expires: time.Now().Add(time.Minute * 10),
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
return nil
})
}