-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement etcd gRPC gateway for HTTP JSON-RPC compatibility #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package etcdgateway | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| gw "go.etcd.io/etcd/api/v3/etcdserverpb/gw" | ||
|
|
||
| "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/protobuf/encoding/protojson" | ||
| ) | ||
|
|
||
| // NewHandler creates an HTTP handler that serves the etcd v3 HTTP/JSON gateway routes | ||
| // (e.g. /v3/kv/range) and proxies them to the provided gRPC endpoint. | ||
| // | ||
| // The endpoint should be a gRPC dial target (typically host:port). If a port-only | ||
| // address like ":2379" is provided, it will be normalized to "127.0.0.1:2379". | ||
| func NewHandler(ctx context.Context, endpoint string, dialOpts []grpc.DialOption, muxOpts ...runtime.ServeMuxOption) (http.Handler, error) { | ||
| endpoint = normalizeEndpoint(endpoint) | ||
|
|
||
| // 参考 etcd | ||
|
yantingqiu marked this conversation as resolved.
|
||
| mux := runtime.NewServeMux( | ||
| runtime.WithMarshalerOption(runtime.MIMEWildcard, | ||
| &runtime.HTTPBodyMarshaler{ | ||
| Marshaler: &runtime.JSONPb{ | ||
| MarshalOptions: protojson.MarshalOptions{ | ||
| UseProtoNames: true, | ||
| EmitUnpopulated: false, | ||
| }, | ||
| UnmarshalOptions: protojson.UnmarshalOptions{ | ||
| DiscardUnknown: true, | ||
| }, | ||
| }, | ||
| }, | ||
| ), | ||
| ) | ||
|
|
||
| // Register handlers for services implemented by MetaStore. | ||
| if err := gw.RegisterKVHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gw.RegisterWatchHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gw.RegisterLeaseHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gw.RegisterClusterHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gw.RegisterMaintenanceHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
| if err := gw.RegisterAuthHandlerFromEndpoint(ctx, mux, endpoint, dialOpts); err != nil { | ||
| return nil, err | ||
| } | ||
|
yantingqiu marked this conversation as resolved.
|
||
|
|
||
| return mux, nil | ||
| } | ||
|
|
||
| // TODO 使用 listen-client-urls 配置来规范化 endpoint | ||
| func normalizeEndpoint(endpoint string) string { | ||
| endpoint = strings.TrimSpace(endpoint) | ||
| if strings.HasPrefix(endpoint, ":") { | ||
| return "127.0.0.1" + endpoint | ||
| } | ||
| if strings.HasPrefix(endpoint, "0.0.0.0:") { | ||
| return "127.0.0.1" + strings.TrimPrefix(endpoint, "0.0.0.0") | ||
| } | ||
| return endpoint | ||
| } | ||
|
Comment on lines
+1
to
+73
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package etcdgateway | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "metaStore/pkg/log" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // WithLogging wraps an HTTP handler and emits a structured log line per request. | ||
| func WithLogging(next http.Handler) http.Handler { | ||
| if next == nil { | ||
| return nil | ||
| } | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| log.Info("grpc-gateway request received", | ||
| zap.String("method", r.Method), | ||
| zap.String("path", r.URL.Path), | ||
| zap.String("component", "grpc-gateway"), | ||
| ) | ||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.