diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index e33784d..0000000 --- a/.jshintrc +++ /dev/null @@ -1,29 +0,0 @@ -{ - "asi" : true, - "laxbreak" : true, - "bitwise" : true, - "boss" : false, - "curly" : true, - "eqeqeq" : true, - "eqnull" : false, - "evil" : false, - "expr" : false, - "forin" : false, - "immed" : true, - "indent" : 2, - "latedef" : true, - "loopfunc" : false, - "noarg" : true, - "node" : true, - "regexp" : true, - "regexdash" : false, - "strict" : false, - "scripturl" : true, - "shadow" : false, - "supernew" : false, - "sub" : true, - "undef" : true, - "white" : true, - "browser" : true, - "globals": { "$": true, "io": true, "Cookies": true } -} diff --git a/.travis.yml b/.travis.yml index 06063d8..f6be9cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - 0.12 + - 4 services: - redis-server env: diff --git a/Procfile b/Procfile deleted file mode 100644 index 4309650..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web node server.js diff --git a/README.md b/README.md index 4b3342e..07cbdf7 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,12 @@ What if there was a *simple+easy* way to see how many people have viewed your GitHub Repository? -[![Build Status](https://travis-ci.org/nelsonic/hits.svg)](https://travis-ci.org/nelsonic/hits) +[![Build Status](https://travis-ci.org/dwyl/hits.svg)](https://travis-ci.org/dwyl/hits) [![HitCount](https://hitt.herokuapp.com/nelsonic/hits.svg)](https://github.com/nelsonic/hits) -[![Code Climate](https://codeclimate.com/github/nelsonic/hits/badges/gpa.svg)](https://codeclimate.com/github/nelsonic/hits) -[![codecov.io](http://codecov.io/github/nelsonic/hits/coverage.svg?branch=master)](http://codecov.io/github/nelsonic/hits?branch=master) -[![Dependency Status](https://david-dm.org/nelsonic/hits.svg)](https://david-dm.org/nelsonic/hits) -[![devDependency Status](https://david-dm.org/nelsonic/hits/dev-status.svg)](https://david-dm.org/nelsonic/hits#info=devDependencies) +[![Code Climate](https://codeclimate.com/github/dwyl/hits/badges/gpa.svg)](https://codeclimate.com/github/dwyl/hits) +[![codecov.io](http://codecov.io/github/dwyl/hits/coverage.svg?branch=master)](http://codecov.io/github/dwyl/hits?branch=master) +[![Dependency Status](https://david-dm.org/dwyl/hits.svg)](https://david-dm.org/dwyl/hits) +[![devDependency Status](https://david-dm.org/dwyl/hits/dev-status.svg)](https://david-dm.org/dwyl/hits#info=devDependencies) ## Why? @@ -23,7 +23,7 @@ to know where we need to be investing our time. A simple way to add (*very basic*) analytics to your GitHub repos. -There are already *many* "Badges" available which people put in their repos: https://github.com/dwyl/repo-badges +There are already *many* "Badges" available which people put in their repos: https://github.com/dwyl/repo-badges But we haven't seen one that gives a "***hit counter***" of the number of times a page has been viewed ... @@ -65,6 +65,25 @@ We then have a user-agent hash where we can lookup the by id: Given that shields.io has a badge creation service, and it has acceptable latency, we are proxying the their service. +## Run it! + +Download (clone) the code to your local machine: +```sh +git clone https://github.com/dwyl/hits.git && cd hits +``` +> Note: you will need to have Redis running on your localhost, +> if you are new to Redis see: https://github.com/dwyl/learn-redis + +Install dependencies: +```sh +npm install +``` +Run locally: +```sh +npm run dev +``` +Visit: http://localhost:8000/any/url/count.svg + ## Research @@ -79,7 +98,7 @@ need to perform a lookup on each hit which requires network IO ... (*expensive*!) What if there was a way of *deriving* a `String` representation of the the user-agent string ... oh, that's right, here's one I made earlier... -https://github.com/ideaq/aguid +https://github.com/dwyl/aguid ### Log Formats diff --git a/image_50x50.png b/lib/1x1px.png similarity index 82% rename from image_50x50.png rename to lib/1x1px.png index 95e6148..e247364 100644 Binary files a/image_50x50.png and b/lib/1x1px.png differ diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..c77deb0 --- /dev/null +++ b/lib/client.js @@ -0,0 +1,9 @@ +// connect to websocket server +$( document ).ready(function() { + console.log('Ready!', window.location.host); + var socket = io(window.location.host); + socket.on('news', function (data) { + console.log(data); + socket.emit('my other event', { my: 'data' }); + }); +}); diff --git a/lib/hits.js b/lib/hits.js index c5ea879..7da52be 100644 --- a/lib/hits.js +++ b/lib/hits.js @@ -31,7 +31,7 @@ module.exports.add = function add (hit, callback) { */ module.exports.count = function count (url, callback) { console.log(url); - url = url.replace('https://github.com', ''); // don't waste RAM! + url = url.replace('https://github.com', ''); // don't waste space in Redis redisClient.llen(url, function(err, data){ callback(err, data); }); diff --git a/lib/index.html b/lib/index.html new file mode 100644 index 0000000..e00c1da --- /dev/null +++ b/lib/index.html @@ -0,0 +1,20 @@ + + + + Stats + + + + + + +

Stats!

+ + + + + + diff --git a/lib/style.css b/lib/style.css new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index 3c9e574..1a90384 100644 --- a/package.json +++ b/package.json @@ -1,46 +1,46 @@ { "name": "hits", - "version": "2.0.2", + "version": "2.0.3", "description": "all the hits", "main": "lib/hits.js", "directories": { "test": "test" }, "scripts": { - "quick": "./node_modules/tape/bin/tape ./test/*.js", + "nocov": "./node_modules/tape/bin/tape ./test/*.js", "test": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./test/*.js", "coverage": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./test/*.js && ./node_modules/.bin/istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", - "jshint": "./node_modules/jshint/bin/jshint -c .jshintrc --exclude-path .gitignore .", - "start": "node server.js" + "start": "node server.js", + "dev": "PORT=8000 ./node_modules/.bin/nodemon ./server.js" }, "repository": { "type": "git", - "url": "git+https://github.com/nelsonic/hits.git" + "url": "git+https://github.com/dwyl/hits.git" }, "keywords": [ - "hits" + "hits", + "hit counter" ], "author": "this guy", "license": "GPL-2.0", "bugs": { - "url": "https://github.com/nelsonic/hits/issues" + "url": "https://github.com/dwyl/hits/issues" }, - "homepage": "https://github.com/nelsonic/hits#readme", + "homepage": "https://github.com/dwyl/hits#readme", "dependencies": { - "env2": "^2.0.4", - "redis-connection": "^4.0.1", + "redis-connection": "^5.0.0", + "socket.io": "^1.4.8", "uniki": "^1.0.3", - "wreck": "^7.0.0" + "wreck": "^8.0.0" }, "devDependencies": { - "decache": "^3.0.3", - "istanbul": "^0.4.2", - "jshint": "^2.9.1", - "pre-commit": "^1.1.2", - "tape": "^4.4.0" + "decache": "^4.0.0", + "istanbul": "^0.4.4", + "nodemon": "^1.9.2", + "pre-commit": "^1.1.3", + "tape": "^4.6.0" }, "pre-commit": [ - "jshint", "coverage" ] } diff --git a/server.js b/server.js index cdd4e5c..95f22cd 100644 --- a/server.js +++ b/server.js @@ -3,62 +3,78 @@ var hits = require('./lib/hits'); var port = process.env.PORT || 8000; var wreck = require('wreck'); var fs = require('fs'); -var png = fs.readFileSync('./image_50x50.png'); -http.createServer(function handler(req, res) { +var png = fs.readFileSync('./lib/1x1px.png'); + +var HEADERS = { // headers see: http://stackoverflow.com/a/2068407/1148249 + "Cache-Control": "no-cache, no-store, must-revalidate", // HTTP 1.1 + "Pragma": "no-cache", // HTTP 1.0 + "Expires": "0", // Proxies + "Content-Type":"image/svg+xml" // default to svg +}; + +var app = http.createServer(function handler(req, res) { var url = req.url; - var agent = req.headers.user-agent var r = req.headers; r.ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; r.url = url.replace('.svg', '').replace('.png', ''); - console.log('>>'+url); + if (url.match(/svg/)) { hits.add(r, function(err, count) { console.log(r.url, ' >> ', count); - var newurl = "https://img.shields.io/badge/hits-" + count +"-brightgreen.svg" - wreck.get(newurl, function (error, response, html) { - // expiry headers see: http://stackoverflow.com/a/2068407/1148249 - var head = { - "Cache-Control": "no-cache, no-store, must-revalidate", // HTTP 1.1 - "Pragma": "no-cache", // HTTP 1.0 - "Expires": "0", // Proxies - "Location": newurl, - "Content-Type":"image/svg+xml" - } - res.writeHead(200, head); - res.end(html); + var newurl = 'https://img.shields.io/badge/hits-' + count +'-brightgreen.svg'; + wreck.get(newurl, function (error, response, raw) { + res.writeHead(200, Object.assign(HEADERS, {"Location": newurl})); + res.end(raw); }); - }) - + }); } else if (url.match(/png/)) { hits.add(r, function(err, count) { - console.log(r.url, ' >> ', count) - // var newurl = "https://img.shields.io/badge/hits-" + count +"-brightgreen.png" - // wreck.get(newurl, function (error, response, html) { - // expiry headers see: http://stackoverflow.com/a/2068407/1148249 - // console.og() - var head = { - "Cache-Control": "no-cache, no-store, must-revalidate", // HTTP 1.1 - "Pragma": "no-cache", // HTTP 1.0 - "Expires": "0", // Proxies - "Content-Type":"image/png" - } - res.writeHead(200, head); - res.end(png); - // }); + console.log(r.url, ' >> ', count); + res.writeHead(200, Object.assign(HEADERS, {"Content-Type": "image/png"})); + res.end(png); }) - } else if(url === '/favicon.ico') { - var fav = 'https://www.google.com/images/google_favicon_128.png' - res.writeHead(301, {"Location": fav }); + var favicon = 'http://i.imgur.com/zBEQq4w.png'; // dwyl favicon + res.writeHead(301, { "Location": favicon }); res.end(); } - else { + else if(url === '/stats') { + fs.readFile('./lib/index.html', 'utf8', function (err, data) { + res.writeHead(200, {"Content-Type": "text/html"}); + res.end(data); + }); + } + else if(url === '/client.js') { + fs.readFile('./lib/client.js', 'utf8', function (err, data) { + res.writeHead(200, {"Content-Type": "application/javascript"}); + res.end(data); + }); + } + else if(url === '/style.css') { + fs.readFile('./lib/style.css', 'utf8', function (err, data) { + res.writeHead(200, {"Content-Type": "text/css"}); + res.end(data); + }); + } + else { // echo the record without saving it console.log(" - - - - - - - - - - record:", r); res.writeHead(200, {"Content-Type": "text/plain"}); - res.end(JSON.stringify(r, null, " ")); // see next line - } // For pretty JSON in Browser see: http://stackoverflow.com/a/5523967/1148249 + res.end(JSON.stringify(r, null, " ")); + } // pretty JSON in Browser see: http://stackoverflow.com/a/5523967/1148249 }).listen(port); +var io = require('socket.io')(app); + +io.on('connection', function (socket) { + console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '); + console.log(socket.client.conn); + console.log(' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '); + socket.emit('news', { msg: 'welcome to stats-ville!' }); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + console.log('Visit http://localhost:' + port); diff --git a/test/hits.test.js b/test/hits.test.js index 39fe995..4aede85 100644 --- a/test/hits.test.js +++ b/test/hits.test.js @@ -1,7 +1,7 @@ -var dir = __dirname.split('/')[__dirname.split('/').length-1]; -var file = dir + __filename.replace(__dirname, '') + " > "; -var test = require('tape'); -var hits = require('../lib/hits'); +var dir = __dirname.split('/')[__dirname.split('/').length-1]; +var file = dir + __filename.replace(__dirname, '') + " > "; +var test = require('tape'); +var hits = require('../lib/hits'); test(file+'Add a hit to the list for that url', function(t){ var req = {