Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeistel <6742251-feistel@users.noreply.gitlab.com>2021-09-16 19:18:54 +0300
committerfeistel <6742251-feistel@users.noreply.gitlab.com>2021-09-16 19:19:13 +0300
commit08d70aef345f1811f13cc990529f5114ccf3a92e (patch)
tree28aa7e492067703dd6b214a3d363292fd645ddca /internal/routing
parentfecd9ca44bfc63e82f1cb2fde515b1e348678f7e (diff)
refactor: move middlewares to corresponding packages
Diffstat (limited to 'internal/routing')
-rw-r--r--internal/routing/middleware.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/internal/routing/middleware.go b/internal/routing/middleware.go
new file mode 100644
index 00000000..5f065c61
--- /dev/null
+++ b/internal/routing/middleware.go
@@ -0,0 +1,41 @@
+package routing
+
+import (
+ "errors"
+ "net/http"
+
+ "gitlab.com/gitlab-org/gitlab-pages/internal/domain"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/logging"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/request"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/source"
+ "gitlab.com/gitlab-org/gitlab-pages/metrics"
+)
+
+// NewMiddleware returns middleware which determine the host and domain for the request, for
+// downstream middlewares to use
+func NewMiddleware(handler http.Handler, s source.Source) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // if we could not retrieve a domain from domains source we break the
+ // middleware chain and simply respond with 502 after logging this
+ host, d, err := getHostAndDomain(r, s)
+ if err != nil && !errors.Is(err, domain.ErrDomainDoesNotExist) {
+ metrics.DomainsSourceFailures.Inc()
+ logging.LogRequest(r).WithError(err).Error("could not fetch domain information from a source")
+
+ httperrors.Serve502(w)
+ return
+ }
+
+ r = request.WithHostAndDomain(r, host, d)
+
+ handler.ServeHTTP(w, r)
+ })
+}
+
+func getHostAndDomain(r *http.Request, s source.Source) (string, *domain.Domain, error) {
+ host := request.GetHostWithoutPort(r)
+ domain, err := s.GetDomain(r.Context(), host)
+
+ return host, domain, err
+}