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

View File

@ -45,10 +45,10 @@ import (
)
// New returns a new GRPC server.
func New() *grpc.Server {
func New(ctx context.Context) *grpc.Server {
s := grpc.NewServer(
grpc.ChainUnaryInterceptor(
unaryMeta,
unaryMeta(ctx),
unary,
),
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)
}
func unaryMeta(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
func unaryMeta(clictx context.Context) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
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)
}
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)
}