Use config dir for serving API

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2020-05-20 15:56:07 +02:00
parent 058e6203a7
commit ebe3fbc180
2 changed files with 33 additions and 40 deletions

View File

@ -21,6 +21,7 @@ type serveOpts struct {
// ServeCommand returns the command to serve the API // ServeCommand returns the command to serve the API
func ServeCommand() *cobra.Command { func ServeCommand() *cobra.Command {
// FIXME(chris-crone): Should warn that specified context is ignored
var opts serveOpts var opts serveOpts
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "serve", Use: "serve",
@ -36,14 +37,12 @@ func ServeCommand() *cobra.Command {
} }
func runServe(ctx context.Context, opts serveOpts) error { func runServe(ctx context.Context, opts serveOpts) error {
s := server.New() s := server.New(ctx)
listener, err := server.CreateListener(opts.address) listener, err := server.CreateListener(opts.address)
if err != nil { if err != nil {
return errors.Wrap(err, "listen address "+opts.address) return errors.Wrap(err, "listen address "+opts.address)
} }
// nolint errcheck // nolint errcheck
defer listener.Close() defer listener.Close()
@ -71,11 +70,7 @@ type cliServer struct {
} }
func (cs *cliServer) Contexts(ctx context.Context, request *cliv1.ContextsRequest) (*cliv1.ContextsResponse, error) { func (cs *cliServer) Contexts(ctx context.Context, request *cliv1.ContextsRequest) (*cliv1.ContextsResponse, error) {
s, err := store.New() s := store.ContextStore(ctx)
if err != nil {
logrus.Error(err)
return &cliv1.ContextsResponse{}, err
}
contexts, err := s.List() contexts, err := s.List()
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)

View File

@ -45,10 +45,10 @@ import (
) )
// New returns a new GRPC server. // New returns a new GRPC server.
func New() *grpc.Server { func New(ctx context.Context) *grpc.Server {
s := grpc.NewServer( s := grpc.NewServer(
grpc.ChainUnaryInterceptor( grpc.ChainUnaryInterceptor(
unaryMeta, unaryMeta(ctx),
unary, unary,
), ),
grpc.StreamInterceptor(stream), grpc.StreamInterceptor(stream),
@ -74,36 +74,34 @@ func stream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
return grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler) return grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler)
} }
func unaryMeta(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { func unaryMeta(clictx context.Context) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx) return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !ok { md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return handler(ctx, req)
}
key, ok := md[apicontext.Key]
if !ok {
return handler(ctx, req)
}
if len(key) == 1 {
s := store.ContextStore(clictx)
ctx = store.WithContextStore(ctx, s)
ctx = apicontext.WithCurrentContext(ctx, key[0])
c, err := client.New(ctx)
if err != nil {
return nil, err
}
ctx, err = proxy.WithClient(ctx, c)
if err != nil {
return nil, err
}
}
return handler(ctx, req) return handler(ctx, req)
} }
key, ok := md[apicontext.Key]
if !ok {
return handler(ctx, req)
}
if len(key) == 1 {
s, err := store.New()
if err != nil {
return nil, err
}
ctx = store.WithContextStore(ctx, s)
ctx = apicontext.WithCurrentContext(ctx, key[0])
c, err := client.New(ctx)
if err != nil {
return nil, err
}
ctx, err = proxy.WithClient(ctx, c)
if err != nil {
return nil, err
}
}
return handler(ctx, req)
} }