github.com/uber/kraken@v0.1.4/proxy/proxyserver/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 proxyserver
    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  	"github.com/uber-go/tally"
    23  	"github.com/uber/kraken/lib/middleware"
    24  	"github.com/uber/kraken/origin/blobclient"
    25  	"github.com/uber/kraken/utils/handler"
    26  )
    27  
    28  // Server defines the proxy HTTP server.
    29  type Server struct {
    30  	stats          tally.Scope
    31  	preheatHandler *PreheatHandler
    32  }
    33  
    34  // New creates a new Server.
    35  func New(
    36  	stats tally.Scope,
    37  	client blobclient.ClusterClient) *Server {
    38  
    39  	return &Server{
    40  		stats.Tagged(map[string]string{"module": "proxyserver"}),
    41  		NewPreheatHandler(client)}
    42  }
    43  
    44  // Handler returns the HTTP handler.
    45  func (s *Server) Handler() http.Handler {
    46  	r := chi.NewRouter()
    47  
    48  	r.Use(middleware.StatusCounter(s.stats))
    49  	r.Use(middleware.LatencyTimer(s.stats))
    50  
    51  	r.Get("/health", handler.Wrap(s.healthHandler))
    52  
    53  	r.Post("/registry/notifications", handler.Wrap(s.preheatHandler.Handle))
    54  
    55  	// Serves /debug/pprof endpoints.
    56  	r.Mount("/", http.DefaultServeMux)
    57  
    58  	return r
    59  }
    60  
    61  func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) error {
    62  	fmt.Fprintln(w, "OK")
    63  	return nil
    64  }