github.com/uber/kraken@v0.1.4/tracker/trackerserver/server.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package trackerserver 15 16 import ( 17 "fmt" 18 "net/http" 19 _ "net/http/pprof" // Registers /debug/pprof endpoints in http.DefaultServeMux. 20 21 "github.com/pressly/chi" 22 chimiddleware "github.com/pressly/chi/middleware" 23 "github.com/uber-go/tally" 24 25 "github.com/uber/kraken/lib/middleware" 26 "github.com/uber/kraken/origin/blobclient" 27 "github.com/uber/kraken/tracker/originstore" 28 "github.com/uber/kraken/tracker/peerhandoutpolicy" 29 "github.com/uber/kraken/tracker/peerstore" 30 "github.com/uber/kraken/utils/handler" 31 "github.com/uber/kraken/utils/listener" 32 "github.com/uber/kraken/utils/log" 33 ) 34 35 // Server serves Tracker endpoints. 36 type Server struct { 37 config Config 38 stats tally.Scope 39 40 peerStore peerstore.Store 41 originStore originstore.Store 42 policy *peerhandoutpolicy.PriorityPolicy 43 44 originCluster blobclient.ClusterClient 45 } 46 47 // New creates a new Server. 48 func New( 49 config Config, 50 stats tally.Scope, 51 policy *peerhandoutpolicy.PriorityPolicy, 52 peerStore peerstore.Store, 53 originStore originstore.Store, 54 originCluster blobclient.ClusterClient) *Server { 55 56 config = config.applyDefaults() 57 58 stats = stats.Tagged(map[string]string{ 59 "module": "trackerserver", 60 }) 61 62 return &Server{ 63 config: config, 64 stats: stats, 65 peerStore: peerStore, 66 originStore: originStore, 67 policy: policy, 68 originCluster: originCluster, 69 } 70 } 71 72 // Handler an http handler for s. 73 func (s *Server) Handler() http.Handler { 74 r := chi.NewRouter() 75 76 r.Use(middleware.StatusCounter(s.stats)) 77 r.Use(middleware.LatencyTimer(s.stats)) 78 79 r.Get("/health", handler.Wrap(s.healthHandler)) 80 r.Get("/announce", handler.Wrap(s.announceHandlerV1)) 81 r.Post("/announce/{infohash}", handler.Wrap(s.announceHandlerV2)) 82 r.Get("/namespace/{namespace}/blobs/{digest}/metainfo", handler.Wrap(s.getMetaInfoHandler)) 83 84 r.Mount("/debug", chimiddleware.Profiler()) 85 86 return r 87 } 88 89 // ListenAndServe is a blocking call which runs s. 90 func (s *Server) ListenAndServe() error { 91 log.Infof("Starting tracker server on %s", s.config.Listener) 92 return listener.Serve(s.config.Listener, s.Handler()) 93 } 94 95 func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) error { 96 fmt.Fprintln(w, "OK") 97 return nil 98 }