github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/middleware/company.go (about) 1 package middleware 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/companieshouse/chs.go/log" 8 "github.com/companieshouse/lfp-pay-api/config" 9 "github.com/companieshouse/lfp-pay-api/utils" 10 "github.com/gorilla/mux" 11 ) 12 13 // CompanyMiddleware will intercept the company number in the path and stick it into the context 14 func CompanyMiddleware(h http.Handler) http.Handler { 15 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 vars := mux.Vars(r) 17 companyNumber, err := utils.GetCompanyNumberFromVars(vars) 18 19 // if there is no company number in the path, then it is likely that this middleware has been used on an 20 // incorrect route. 21 if err != nil { 22 log.ErrorR(r, err) 23 } 24 ctx := context.WithValue(r.Context(), config.CompanyNumber, companyNumber) 25 h.ServeHTTP(w, r.WithContext(ctx)) 26 }) 27 }