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

     1  package pipelineserver
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     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  	teamDBFactory db.TeamFactory
    14  }
    15  
    16  func NewScopedHandlerFactory(
    17  	teamDBFactory db.TeamFactory,
    18  ) *ScopedHandlerFactory {
    19  	return &ScopedHandlerFactory{
    20  		teamDBFactory: teamDBFactory,
    21  	}
    22  }
    23  
    24  func (pdbh *ScopedHandlerFactory) HandlerFor(pipelineScopedHandler func(db.Pipeline) http.Handler) http.HandlerFunc {
    25  	return func(w http.ResponseWriter, r *http.Request) {
    26  		teamName := r.FormValue(":team_name")
    27  		pipelineName := r.FormValue(":pipeline_name")
    28  		pipelineRef := atc.PipelineRef{Name: pipelineName}
    29  		if instanceVars := r.URL.Query().Get("instance_vars"); instanceVars != "" {
    30  			err := json.Unmarshal([]byte(instanceVars), &pipelineRef.InstanceVars)
    31  			if err != nil {
    32  				w.WriteHeader(http.StatusInternalServerError)
    33  				return
    34  			}
    35  		}
    36  
    37  		pipeline, ok := r.Context().Value(auth.PipelineContextKey).(db.Pipeline)
    38  		if !ok {
    39  			dbTeam, found, err := pdbh.teamDBFactory.FindTeam(teamName)
    40  			if err != nil {
    41  				w.WriteHeader(http.StatusInternalServerError)
    42  				return
    43  			}
    44  
    45  			if !found {
    46  				w.WriteHeader(http.StatusNotFound)
    47  				return
    48  			}
    49  
    50  			pipeline, found, err = dbTeam.Pipeline(pipelineRef)
    51  			if err != nil {
    52  				w.WriteHeader(http.StatusInternalServerError)
    53  				return
    54  			}
    55  
    56  			if !found {
    57  				w.WriteHeader(http.StatusNotFound)
    58  				return
    59  			}
    60  		}
    61  
    62  		pipelineScopedHandler(pipeline).ServeHTTP(w, r)
    63  	}
    64  }