vitess.io/vitess@v0.16.2/go/vt/vtadmin/http/api.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package http
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"vitess.io/vitess/go/trace"
    24  	"vitess.io/vitess/go/vt/vtadmin/cache"
    25  	"vitess.io/vitess/go/vt/vtadmin/rbac"
    26  
    27  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    28  )
    29  
    30  // Options defines the set of configurations for an HTTP API server.
    31  type Options struct {
    32  	// CORSOrigins is the list of origins to allow via CORS. An empty or nil
    33  	// slice disables CORS entirely.
    34  	CORSOrigins []string
    35  	// EnableTracing specifies whether to install a tracing middleware on the
    36  	// API subrouter.
    37  	EnableTracing bool
    38  	// DisableCompression specifies whether to turn off gzip compression for API
    39  	// endpoints. It is named as the negative (as opposed to EnableTracing) so
    40  	// the zero value has compression enabled.
    41  	DisableCompression bool
    42  	// DisableDebug specifies whether to omit the /debug/pprof/* and /debug/env
    43  	// routes.
    44  	DisableDebug        bool
    45  	ExperimentalOptions struct {
    46  		TabletURLTmpl string
    47  	}
    48  }
    49  
    50  // API is used to power HTTP endpoint wrappers to the VTAdminServer interface.
    51  type API struct {
    52  	server vtadminpb.VTAdminServer
    53  	opts   Options
    54  }
    55  
    56  // NewAPI returns an HTTP API backed by the given VTAdminServer implementation.
    57  func NewAPI(server vtadminpb.VTAdminServer, opts Options) *API {
    58  	return &API{server: server, opts: opts}
    59  }
    60  
    61  // VTAdminHandler is an HTTP endpoint handler that takes, via injection,
    62  // everything needed to implement a JSON API response.
    63  type VTAdminHandler func(ctx context.Context, r Request, api *API) *JSONResponse
    64  
    65  // Adapt converts a VTAdminHandler into an http.HandlerFunc. It deals with
    66  // wrapping the request in a wrapper for some convenience functions and starts
    67  // a new context, after extracting any potential spans that were set by an
    68  // upstream middleware in the request context.
    69  func (api *API) Adapt(handler VTAdminHandler) http.HandlerFunc {
    70  	return func(w http.ResponseWriter, r *http.Request) {
    71  		ctx := context.Background()
    72  
    73  		span, _ := trace.FromContext(r.Context())
    74  		if span != nil {
    75  			ctx = trace.NewContext(ctx, span)
    76  		}
    77  
    78  		actor, _ := rbac.FromContext(r.Context())
    79  		if actor != nil {
    80  			ctx = rbac.NewContext(ctx, actor)
    81  		}
    82  
    83  		if cache.ShouldRefreshFromRequest(r) {
    84  			ctx = cache.NewIncomingRefreshContext(ctx)
    85  		}
    86  
    87  		handler(ctx, Request{r}, api).Write(w)
    88  	}
    89  }
    90  
    91  // Options returns a copy of the Options this API was configured with.
    92  func (api *API) Options() Options {
    93  	return api.opts
    94  }
    95  
    96  // Server returns the VTAdminServer wrapped by this API.
    97  func (api *API) Server() vtadminpb.VTAdminServer {
    98  	return api.server
    99  }