From 6c8de4dbb36f2a0beb359d2d448c6ddb84f1f7dd Mon Sep 17 00:00:00 2001 From: joerivrij Date: Mon, 15 May 2023 09:02:30 +0200 Subject: [PATCH] Adds a method for creating the scheme with const --- api/router.go | 2 +- api/v1/procestype.go | 11 +++-------- api/v1/resultaat.go | 13 +++++-------- api/v1/resultaattypeomschrijving.go | 10 ++-------- api/v1/scheme.go | 21 +++++++++++++++++++++ 5 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 api/v1/scheme.go diff --git a/api/router.go b/api/router.go index 3470544..df1a643 100644 --- a/api/router.go +++ b/api/router.go @@ -15,7 +15,7 @@ func InitRoutes(loader *models.Loader) *mux.Router { Data: loader, } - serveMux.HandleFunc("/api/v1/health", middleware.Adapt(v1Handler.Health, middleware.ValidateRestMethod("GET"), middleware.LogRequestDetails(), middleware.SetCorsHeaders())) + serveMux.HandleFunc("/api/v1/health", middleware.Adapt(v1Handler.Health, middleware.ValidateRestMethod("GET"), middleware.SetCorsHeaders())) //resultaten serveMux.HandleFunc("/api/v1/resultaten", middleware.Adapt(v1Handler.ListResultaten, middleware.ValidateRestMethod("GET"), middleware.LogRequestDetails(), middleware.SetCorsHeaders())) diff --git a/api/v1/procestype.go b/api/v1/procestype.go index 3ebfa99..5787541 100644 --- a/api/v1/procestype.go +++ b/api/v1/procestype.go @@ -46,10 +46,8 @@ func (r *ReferentielijstenHandler) ListProcesTypen(w http.ResponseWriter, req *h var parsedYear int32 var response models.ProcesTypen - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + + scheme := getScheme(req) if year != "" { convertedYear, err := strconv.Atoi(year) @@ -132,10 +130,7 @@ func (r *ReferentielijstenHandler) GetProcesType(w http.ResponseWriter, req *htt pathParams := mux.Vars(req) uuid := pathParams[UUIDFromParam] - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + scheme := getScheme(req) for _, result := range r.Data.ProcesTypen { if result.URL == uuid { diff --git a/api/v1/resultaat.go b/api/v1/resultaat.go index 99b740b..3e43d0a 100644 --- a/api/v1/resultaat.go +++ b/api/v1/resultaat.go @@ -6,6 +6,7 @@ import ( "github.com/VNG-Realisatie/referentielijsten-api/models" "github.com/VNG-Realisatie/referentielijsten-api/validator" "github.com/gorilla/mux" + "log" "net/http" "net/url" "strconv" @@ -50,10 +51,9 @@ func (r *ReferentielijstenHandler) ListResultaten(w http.ResponseWriter, req *ht // 404: Fout // 405: Fout - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + scheme := getScheme(req) + + log.Printf("req.TLS: %v", req.TLS) pageId := req.URL.Query().Get(PageFromParam) procesType := req.URL.Query().Get(ProcesTypeFromParam) @@ -200,10 +200,7 @@ func (r *ReferentielijstenHandler) GetResultaat(w http.ResponseWriter, req *http pathParams := mux.Vars(req) uuid := pathParams[UUIDFromParam] - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + scheme := getScheme(req) for _, result := range r.Data.Resultaten { if result.URL == uuid { diff --git a/api/v1/resultaattypeomschrijving.go b/api/v1/resultaattypeomschrijving.go index c58d02c..34e612e 100644 --- a/api/v1/resultaattypeomschrijving.go +++ b/api/v1/resultaattypeomschrijving.go @@ -28,10 +28,7 @@ func (r *ReferentielijstenHandler) ListResultaattypeomschrijvingen(w http.Respon // 200: ResultaattypeOmschrijvingGeneriek // 405: Fout - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + scheme := getScheme(req) //TODO remove log.Print(req) @@ -81,10 +78,7 @@ func (r *ReferentielijstenHandler) GetResultaattypeomschrijving(w http.ResponseW pathParams := mux.Vars(req) uuid := pathParams[UUIDFromParam] - scheme := "https://" - if req.TLS == nil { - scheme = "http://" - } + scheme := getScheme(req) for _, result := range r.Data.ResultaattypenOmscrhijvingen { if result.URL == uuid { diff --git a/api/v1/scheme.go b/api/v1/scheme.go new file mode 100644 index 0000000..4095c8b --- /dev/null +++ b/api/v1/scheme.go @@ -0,0 +1,21 @@ +package v1 + +import "net/http" + +const ( + productionHostName string = "referentielijsten-api.vng.cloud" + // currently we do not have a test server but this is for future proofing + testHostName string = "referentielijsten-api.test.vng.cloud" +) + +// getScheme determines the scheme used for building up a url +// currently the apis do not run on https -> they will always return http when running on kube +// for production this scheme should always be https +func getScheme(req *http.Request) string { + scheme := "http://" + if req.TLS != nil || req.Host == productionHostName || req.Host == testHostName { + scheme = "https://" + } + + return scheme +}