github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/buildserver/scoped_handler_factory.go (about)

     1  package buildserver
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  
     7  	"code.cloudfoundry.org/lager"
     8  	"github.com/pf-qiu/concourse/v6/atc/api/auth"
     9  	"github.com/pf-qiu/concourse/v6/atc/db"
    10  )
    11  
    12  type scopedHandlerFactory struct {
    13  	logger lager.Logger
    14  }
    15  
    16  func NewScopedHandlerFactory(
    17  	logger lager.Logger,
    18  ) *scopedHandlerFactory {
    19  	return &scopedHandlerFactory{
    20  		logger: logger,
    21  	}
    22  }
    23  
    24  func (f *scopedHandlerFactory) HandlerFor(buildScopedHandler func(db.Build) http.Handler) http.HandlerFunc {
    25  	logger := f.logger.Session("scoped-build-factory")
    26  
    27  	return func(w http.ResponseWriter, r *http.Request) {
    28  		build, ok := r.Context().Value(auth.BuildContextKey).(db.Build)
    29  		if !ok {
    30  			logger.Error("build-is-not-in-context", errors.New("build-is-not-in-context"))
    31  			w.WriteHeader(http.StatusInternalServerError)
    32  			return
    33  		}
    34  
    35  		buildScopedHandler(build).ServeHTTP(w, r)
    36  	}
    37  }