github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/healthz/healthz.go (about)

     1  // Copyright 2017 Google 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  
    15  // Package healthz provides an HTTPHandler that always returns the string "ok".
    16  package healthz
    17  
    18  import (
    19  	"context"
    20  	"io"
    21  	"net/http"
    22  
    23  	"github.com/bazelbuild/rules_webtesting/go/httphelper"
    24  	"github.com/bazelbuild/rules_webtesting/go/wtl/proxy"
    25  )
    26  
    27  type healthz struct{}
    28  
    29  // HTTPHandlerProvider returns a HTTPHandlerProvider for handling healthz requests.
    30  func HTTPHandlerProvider(*proxy.Proxy) (proxy.HTTPHandler, error) {
    31  	return &healthz{}, nil
    32  }
    33  
    34  func (h *healthz) Shutdown(context.Context) error {
    35  	return nil
    36  }
    37  
    38  func (h *healthz) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
    39  	w.Header().Set("Content-Type", "text/plain")
    40  	httphelper.SetDefaultResponseHeaders(w.Header())
    41  	w.WriteHeader(http.StatusOK)
    42  	io.WriteString(w, "ok")
    43  }
    44  
    45  func (*healthz) Name() string {
    46  	return "healthz http handler"
    47  }
    48  
    49  func (*healthz) Healthy(context.Context) error {
    50  	return nil
    51  }