Skip to content

Commit

Permalink
Merge pull request #35 from dwyl/update
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
iteles authored Jul 17, 2016
2 parents ff92bf7 + 06aa89d commit f4413d3
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 98 deletions.
29 changes: 0 additions & 29 deletions .jshintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 0.12
- 4
services:
- redis-server
env:
Expand Down
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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 ...

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
Binary file renamed image_50x50.png → lib/1x1px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -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' });
});
});
2 changes: 1 addition & 1 deletion lib/hits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
20 changes: 20 additions & 0 deletions lib/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<title>Stats</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link rel="stylesheet" href="/style.css">
<link rel="icon"
type="image/png"
href="http://i.imgur.com/zBEQq4w.png">
</head>
<body>
<h2>Stats! </h2>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://cdn.rawgit.com/ScottHamper/Cookies/1.2.1/dist/cookies.min.js">
</script>
<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
<script src="/client.js"></script>
</body>
</html>
Empty file added lib/style.css
Empty file.
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
92 changes: 54 additions & 38 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 4 additions & 4 deletions test/hits.test.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down

0 comments on commit f4413d3

Please sign in to comment.