mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 03:20:27 +00:00
move should-collect logic into func so it can be tested
This commit is contained in:
parent
05a159da7f
commit
2c09050d74
@ -4,6 +4,7 @@ import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mssola/user_agent"
|
||||
@ -12,14 +13,26 @@ import (
|
||||
"github.com/usefathom/fathom/pkg/models"
|
||||
)
|
||||
|
||||
func ShouldCollect(r *http.Request) bool {
|
||||
// abort if this is a bot.
|
||||
ua := user_agent.New(r.UserAgent())
|
||||
if ua.Bot() {
|
||||
return false
|
||||
}
|
||||
|
||||
if r.Referer() == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/* middleware */
|
||||
func NewCollectHandler() http.Handler {
|
||||
go aggregate()
|
||||
|
||||
return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
// abort if this is a bot.
|
||||
ua := user_agent.New(r.UserAgent())
|
||||
if ua.Bot() {
|
||||
if !ShouldCollect(r) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -28,11 +41,6 @@ func NewCollectHandler() http.Handler {
|
||||
return err
|
||||
}
|
||||
|
||||
// abandon if HTTP referer was empty
|
||||
if u.Scheme == "" || u.Host == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
q := r.URL.Query()
|
||||
now := time.Now()
|
||||
|
||||
@ -40,7 +48,7 @@ func NewCollectHandler() http.Handler {
|
||||
pageview := &models.Pageview{
|
||||
SessionID: q.Get("sid"),
|
||||
Hostname: u.Scheme + "://" + u.Host,
|
||||
Pathname: q.Get("p"),
|
||||
Pathname: "/" + strings.TrimLeft(q.Get("p"), "/"),
|
||||
IsNewVisitor: q.Get("nv") == "1",
|
||||
IsNewSession: q.Get("ns") == "1",
|
||||
IsUnique: q.Get("u") == "1",
|
||||
|
15
pkg/api/collect_test.go
Normal file
15
pkg/api/collect_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShouldCollect(t *testing.T) {
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
r.Header.Add("User-Agent", "Mozilla/1.0")
|
||||
r.Header.Add("Referer", "http://usefathom.com/")
|
||||
if v := ShouldCollect(r); v != true {
|
||||
t.Errorf("Expected %#v, got %#v", true, false)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user