github.com/m3db/m3@v1.5.0/src/query/api/v1/handler/topic/get.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 topic 22 23 import ( 24 "net/http" 25 26 clusterclient "github.com/m3db/m3/src/cluster/client" 27 "github.com/m3db/m3/src/cluster/placementhandler/handleroptions" 28 "github.com/m3db/m3/src/cmd/services/m3query/config" 29 "github.com/m3db/m3/src/msg/topic" 30 "github.com/m3db/m3/src/query/api/v1/route" 31 "github.com/m3db/m3/src/query/generated/proto/admin" 32 "github.com/m3db/m3/src/query/util/logging" 33 "github.com/m3db/m3/src/x/instrument" 34 xhttp "github.com/m3db/m3/src/x/net/http" 35 36 "go.uber.org/zap" 37 ) 38 39 const ( 40 // GetURL is the url for the topic get handler (with the GET method). 41 GetURL = route.Prefix + "/topic" 42 43 // GetHTTPMethod is the HTTP method used with this resource. 44 GetHTTPMethod = http.MethodGet 45 ) 46 47 // GetHandler is the handler for topic gets. 48 type GetHandler Handler 49 50 // newGetHandler returns a new instance of GetHandler. 51 func newGetHandler( 52 client clusterclient.Client, 53 cfg config.Configuration, 54 instrumentOpts instrument.Options, 55 ) http.Handler { 56 return &GetHandler{ 57 client: client, 58 cfg: cfg, 59 serviceFn: Service, 60 instrumentOpts: instrumentOpts, 61 } 62 } 63 64 func (h *GetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 65 var ( 66 ctx = r.Context() 67 logger = logging.WithContext(ctx, h.instrumentOpts) 68 ) 69 70 serviceCfg := handleroptions.ServiceNameAndDefaults{} 71 svcOpts := handleroptions.NewServiceOptions(serviceCfg, r.Header, nil) 72 service, err := h.serviceFn(h.client, svcOpts) 73 if err != nil { 74 logger.Error("unable to get service", zap.Error(err)) 75 xhttp.WriteError(w, err) 76 return 77 } 78 79 t, err := service.Get(topicName(r.Header)) 80 if err != nil { 81 logger.Error("unable to get topic", zap.Error(err)) 82 xhttp.WriteError(w, xhttp.NewError(err, http.StatusNotFound)) 83 return 84 } 85 86 pb, err := topic.ToProto(t) 87 if err != nil { 88 logger.Error("unable to get topic protobuf", zap.Error(err)) 89 xhttp.WriteError(w, err) 90 return 91 } 92 93 resp := &admin.TopicGetResponse{ 94 Topic: pb, 95 Version: uint32(t.Version()), 96 } 97 xhttp.WriteProtoMsgJSONResponse(w, resp, logger) 98 }