github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/database/common.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package database contains API endpoints for managing the database.
    22  package database
    23  
    24  import (
    25  	clusterclient "github.com/m3db/m3/src/cluster/client"
    26  	"github.com/m3db/m3/src/cluster/placementhandler/handleroptions"
    27  	dbconfig "github.com/m3db/m3/src/cmd/services/m3dbnode/config"
    28  	"github.com/m3db/m3/src/cmd/services/m3query/config"
    29  	"github.com/m3db/m3/src/query/api/v1/options"
    30  	"github.com/m3db/m3/src/query/util/queryhttp"
    31  	"github.com/m3db/m3/src/x/instrument"
    32  )
    33  
    34  // Handler represents a generic handler for namespace endpoints.
    35  type Handler struct {
    36  	// This is used by other namespace Handlers
    37  	// nolint: structcheck, megacheck, unused
    38  	client         clusterclient.Client
    39  	instrumentOpts instrument.Options
    40  }
    41  
    42  // RegisterRoutes registers the namespace routes
    43  func RegisterRoutes(
    44  	r *queryhttp.EndpointRegistry,
    45  	client clusterclient.Client,
    46  	cfg config.Configuration,
    47  	embeddedDBCfg *dbconfig.DBConfiguration,
    48  	defaults []handleroptions.ServiceOptionsDefault,
    49  	instrumentOpts instrument.Options,
    50  	namespaceValidator options.NamespaceValidator,
    51  	kvStoreProtoParser options.KVStoreProtoParser,
    52  ) error {
    53  	createHandler, err := NewCreateHandler(client, cfg, embeddedDBCfg,
    54  		defaults, instrumentOpts, namespaceValidator)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	kvStoreHandler := NewKeyValueStoreHandler(client, instrumentOpts, kvStoreProtoParser)
    60  
    61  	// Register the same handler under two different endpoints. This just makes explaining things in
    62  	// our documentation easier so we can separate out concepts, but share the underlying code.
    63  	if err := r.Register(queryhttp.RegisterOptions{
    64  		Path:    CreateURL,
    65  		Handler: createHandler,
    66  		Methods: []string{CreateHTTPMethod},
    67  	}); err != nil {
    68  		return err
    69  	}
    70  	if err := r.Register(queryhttp.RegisterOptions{
    71  		Path:    CreateNamespaceURL,
    72  		Handler: createHandler,
    73  		Methods: []string{CreateNamespaceHTTPMethod},
    74  	}); err != nil {
    75  		return err
    76  	}
    77  	if err := r.Register(queryhttp.RegisterOptions{
    78  		Path:    KeyValueStoreURL,
    79  		Handler: kvStoreHandler,
    80  		Methods: []string{KeyValueStoreHTTPMethod},
    81  	}); err != nil {
    82  		return err
    83  	}
    84  
    85  	return nil
    86  }