aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2020-02-20 02:55:05 +0000
committerFelix Hanley <felix@userspace.com.au>2020-02-20 02:55:05 +0000
commitdcc9d4bf599eeeffee615cf198b2b74734bcaa48 (patch)
tree1e5173e89989db8f75d8f85d067270bab04afde3
parentcc933f411e5d40c16bf07e80a20ded70a26a6a64 (diff)
downloadsws-dcc9d4bf599eeeffee615cf198b2b74734bcaa48.tar.gz
sws-dcc9d4bf599eeeffee615cf198b2b74734bcaa48.tar.bz2
Change counter API
-rw-r--r--cmd/server/helpers.go5
-rw-r--r--cmd/server/main.go1
-rw-r--r--cmd/server/sites.go21
-rw-r--r--counter/sws.js35
-rw-r--r--package.json2
-rw-r--r--yarn.lock18
6 files changed, 43 insertions, 39 deletions
diff --git a/cmd/server/helpers.go b/cmd/server/helpers.go
index 3038b91..f3733c5 100644
--- a/cmd/server/helpers.go
+++ b/cmd/server/helpers.go
@@ -33,6 +33,11 @@ var funcMap = template.FuncMap{
},
}
+func httpError(w http.ResponseWriter, code int, msg string) {
+ log(msg)
+ http.Error(w, http.StatusText(500), 500)
+}
+
func extractTimeRange(r *http.Request) (*time.Time, *time.Time) {
begin := timePtr(time.Now().Round(time.Hour).Add(-168 * time.Hour))
end := timePtr(time.Now())
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 24a706e..7f9041e 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -148,7 +148,6 @@ func main() {
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, "/")
- log("serving", p)
if b, err := StaticLoadTemplate(p); err == nil {
name := filepath.Base(p)
http.ServeContent(w, r, name, time.Now(), bytes.NewReader(b))
diff --git a/cmd/server/sites.go b/cmd/server/sites.go
index 59bb1f0..d59eb1c 100644
--- a/cmd/server/sites.go
+++ b/cmd/server/sites.go
@@ -11,8 +11,8 @@ func handleSites(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sites, err := db.GetSites()
if err != nil {
- log(err)
- http.Error(w, http.StatusText(500), 500)
+ httpError(w, 500, err.Error())
+ return
}
payload := struct {
Sites []*sws.Site
@@ -20,8 +20,8 @@ func handleSites(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
Sites: sites,
}
if err := rndr.Render(w, "sites", payload); err != nil {
- log(err)
- http.Error(w, http.StatusText(500), 500)
+ httpError(w, 500, err.Error())
+ return
}
}
}
@@ -31,14 +31,12 @@ func handleSite(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
ctx := r.Context()
site, ok := ctx.Value("site").(*sws.Site)
if !ok {
- log("no site in context")
- http.Error(w, http.StatusText(422), 422)
+ httpError(w, 422, "no site in context")
return
}
begin, end := extractTimeRange(r)
if begin == nil || end == nil {
- log("invalid time range")
- http.Error(w, http.StatusText(406), 406)
+ httpError(w, 406, "invalid time range")
return
}
@@ -47,7 +45,8 @@ func handleSite(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
"end": *end,
})
if err != nil {
- log(err)
+ httpError(w, 500, err.Error())
+ return
}
pages := sws.PagesFromHits(hits)
@@ -68,8 +67,8 @@ func handleSite(db sws.SiteStore, rndr Renderer) http.HandlerFunc {
Hits: buckets,
}
if err := rndr.Render(w, "site", payload); err != nil {
- log(err)
- http.Error(w, http.StatusText(500), 500)
+ httpError(w, 500, err.Error())
+ return
}
}
}
diff --git a/counter/sws.js b/counter/sws.js
index e935b0a..6995bbb 100644
--- a/counter/sws.js
+++ b/counter/sws.js
@@ -8,20 +8,20 @@ var me = document.currentScript
console.log('me:', me)
console.log('me.sws:', me.dataset.sws)
-var _sws = w._sws || {xhr:true}
+var _sws = w._sws || {noxhr: false, noauto: false}
console.log('_sws:', _sws)
_sws.d = _sws.d || me.dataset.sws || 'http://sws.userspace.com.au/sws.gif'
_sws.site = _sws.site || me.dataset.site
console.log('using', _sws.d)
-function send (p, obj) {
+function count (p, obj) {
console.log('sending', p, JSON.stringify(obj))
var qs = Object.keys(obj)
.map(function (k) {
return esc(k) + '=' + esc(obj[k])
})
.join('&')
- if (_sws.xhr) {
+ if (!_sws.xhr) {
var r = new w.XMLHttpRequest()
r.open('GET', p + '?' + qs, true)
r.send()
@@ -41,20 +41,21 @@ function ready (fn) {
}
}
-var viewPort = (w.innerWidth || de.clientWidth || d.body.clientWidth)
- + 'x'
- + (w.innerHeight || de.clientHeight || d.body.clientHeight)
+var viewPort = (w.innerWidth || de.clientWidth || d.body.clientWidth) + 'x' +
+ (w.innerHeight || de.clientHeight || d.body.clientHeight)
ready(function () {
- send(_sws.d, {
- i: _sws.site,
- s: l.protocol,
- h: l.host,
- p: l.pathname,
- q: l.search + l.hash,
- t: _sws.title || d.title,
- r: d.referrer,
- u: n.userAgent,
- v: viewPort
- })
+ if (!_sws.noauto) {
+ count(_sws.d, {
+ i: _sws.site,
+ s: l.protocol,
+ h: l.host,
+ p: l.pathname,
+ q: l.search + l.hash,
+ t: _sws.title || d.title,
+ r: d.referrer,
+ u: n.userAgent,
+ v: viewPort
+ })
+ }
})
diff --git a/package.json b/package.json
index 8da06ad..bfcbf55 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"license": "MIT",
"dependencies": {
- "uglify-js": "3.4.9"
+ "uglify-js": "3.8.0"
}
}
diff --git a/yarn.lock b/yarn.lock
index 7f67a01..3919ef4 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2,20 +2,20 @@
# yarn lockfile v1
-commander@~2.17.1:
- version "2.17.1"
- resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"
- integrity sha512-wPMUt6FnH2yzG95SA6mzjQOEKUU3aLaDEmzs1ti+1E9h+CsrZghRlqEM/EJ4KscsQVG8uNN4uVreUeT8+drlgg==
+commander@~2.20.3:
+ version "2.20.3"
+ resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
+ integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
-uglify-js@3.4.9:
- version "3.4.9"
- resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3"
- integrity sha512-8CJsbKOtEbnJsTyv6LE6m6ZKniqMiFWmm9sRbopbkGs3gMPPfd3Fh8iIA4Ykv5MgaTbqHr4BaoGLJLZNhsrW1Q==
+uglify-js@3.8.0:
+ version "3.8.0"
+ resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.8.0.tgz#f3541ae97b2f048d7e7e3aa4f39fd8a1f5d7a805"
+ integrity sha512-ugNSTT8ierCsDHso2jkBHXYrU8Y5/fY2ZUprfrJUiD7YpuFvV4jODLFmb3h4btQjqr5Nh4TX4XtgDfCU1WdioQ==
dependencies:
- commander "~2.17.1"
+ commander "~2.20.3"
source-map "~0.6.1"