pages/internal/pageviews/middleware.go

107 lines
2.7 KiB
Go
Raw Normal View History

2022-06-03 19:39:22 +00:00
// Copyright (C) 2022 The pages authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package pageviews
2022-06-03 19:39:22 +00:00
import (
"net/http"
"net/url"
2022-06-03 19:39:22 +00:00
"github.com/gorilla/mux"
2022-07-03 03:01:26 +00:00
"code.pitz.tech/mya/pages/internal/excludes"
"code.pitz.tech/mya/pages/internal/geoip"
"code.pitz.tech/mya/pages/internal/metrics"
2022-06-03 19:39:22 +00:00
)
type opt struct {
excludes []excludes.Exclusion
2022-06-03 19:39:22 +00:00
}
// Option provides a way to configure elements of the Middleware.
type Option func(*opt)
2022-06-03 19:39:22 +00:00
// Exclusions appends the provided rules to the excludes list. Any path that matches an exclusion will not be measured.
func Exclusions(exclusions ...excludes.Exclusion) Option {
return func(o *opt) {
o.excludes = append(o.excludes, exclusions...)
2022-06-03 19:39:22 +00:00
}
}
// Middleware produces an HTTP middleware function that reports page views.
func Middleware(opts ...Option) mux.MiddlewareFunc {
o := opt{}
for _, opt := range opts {
opt(&o)
2022-06-03 19:39:22 +00:00
}
exclude := excludes.AnyExclusion(o.excludes...)
2022-06-03 19:39:22 +00:00
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if exclude(r.URL.Path) {
next.ServeHTTP(w, r)
return
}
url, err := url.Parse(r.RequestURI)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
return
}
domain := url.Hostname()
switch {
case r.Header.Get("Host") != "":
domain = r.Header.Get("Host")
case r.Header.Get("X-Forwarded-Host") != "":
domain = r.Header.Get("X-Forwarded-Host")
}
path := url.Path
referrer := r.Referer()
info := geoip.Extract(r.Context())
2022-06-03 19:39:22 +00:00
d := &writer{w, http.StatusOK}
defer func() {
if d.statusCode != http.StatusNotFound {
metrics.PageViewCount.WithLabelValues(domain, path, referrer, info.CountryCode).Inc()
2022-06-03 19:39:22 +00:00
}
}()
next.ServeHTTP(d, r)
})
}
}
type writer struct {
writer http.ResponseWriter
statusCode int
}
func (w *writer) Header() http.Header {
return w.writer.Header()
}
func (w *writer) Write(bytes []byte) (int, error) {
return w.writer.Write(bytes)
}
func (w *writer) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.writer.WriteHeader(statusCode)
}