Skip to content

Commit

Permalink
Add logger and fix architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
debugger22 committed Sep 30, 2016
1 parent ee81ef6 commit 33acf4c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 51 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
# github-audio
GitHub Audio
============

Listen to music generated by events happening across GitHub


Installation on OSX
-------------------

```bash
$ brew install nodejs
$ brew npm
$ brew install redis
```

Installation on Linux
---------------------

```bash
$ sudo apt-get updade
$ sudo apt-get install nodejs
$ sudo apt-get install npm
$ sudo apt-get install redis-server
```

Install node packages
---------------------

Navigate to the project directory and run

```bash
$ npm install
```

Environment variables
---------------------

```
$ export GITHUB_OAUTH_KEY=<your_github_oauth_key>
```

Note: Without the GitHub oauth key the number of requests is throttled at 60 per hour. It can be increased to 5000 per hour by using an oauth key.

Run server
----------

```bash
$ node app/server.js
```

Note: For production run `export NODE_ENV="production"` before starting the server.
47 changes: 0 additions & 47 deletions app/server.js

This file was deleted.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "github-audio",
"version": "1.0.0",
"description": "Listen to music generated by events across GitHub",
"main": "app/server.js",
"main": "index.js",
"scripts": {
"test": "test"
"test": "test",
"start": "cross-env NODE_ENV=development node server"
},
"repository": {
"type": "git",
Expand All @@ -21,8 +22,11 @@
},
"homepage": "https://github.com/debugger22/github-audio#readme",
"dependencies": {
"chalk": "^1.1.3",
"express": "^4.14.0",
"node-redis": "^0.1.7",
"ip": "^1.1.3",
"minimist": "^1.2.0",
"path": "^0.12.7",
"redis": "^2.6.2",
"request": "^2.75.0",
"socket.io": "^1.4.8"
Expand Down
65 changes: 65 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env nodejs

var app = require('express')();
var request = require("request"); // To make HTTP requests at the server side

var server = require('http').Server(app);
var io = require('socket.io')(server);

// To temporarily store JSON data from GitHub and also
// the number of connected users
var redis = require("redis"),
redis_client = redis.createClient();

var path = require('path');

const logger = require('./logger');
const argv = require('minimist')(process.argv.slice(2));
const isDev = process.env.NODE_ENV !== 'production';

// Get the intended port number, use port 8000 if not provided
const port = argv.port || process.env.PORT || 8000;
server.listen(port, (err) => {
if(err){
return logger.error(err.message);
}
});
if(isDev)
logger.appStarted(port, 'http://localhost');
else
logger.appStarted(port);

// Load main web page
app.get('/', function (req, res) {
res.sendFile(path.resolve('app/index.html'));
});


// When a socket connection is created
io.on('connection', function (socket) {


// Function to get events from GitHub API
function fetchDataFromGithub(){
var options = {
url: 'https://api.github.com/events',
headers: {
'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36',
'Authorization': 'token ' + process.env.GITHUB_OAUTH_KEY
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
socket.emit('github', body);
}else{
logger.d("GitHib request: " + options.headers.Authorization);
logger.error("GitHub status code: " + response.statusCode);
}
})

setTimeout(fetchDataFromGithub, 2000);
}

setTimeout(fetchDataFromGithub, 2000);

});
44 changes: 44 additions & 0 deletions server/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable no-console */

const chalk = require('chalk');
const ip = require('ip');

const divider = chalk.gray('\n-----------------------------------');

const logger = {

// Called whenever there's an error on the server we want to print
error: (err) => {
console.error(chalk.red(err));
},

// Called whenever there's a debug log we want to print
d: (log) => {
console.log(chalk.blue(log));
},

// Called whenever there's a verbose log we want to print
v: (log) => {
console.log(chalk.white(log));
},

// Called when express.js app starts on given port w/o errors
appStarted: (port, tunnelStarted) => {
console.log(`Server started ${chalk.green('✓')}`);

// If the tunnel started, log that and the URL it's available at
if (tunnelStarted) {
console.log(`Tunnel initialised ${chalk.green('✓')}`);
}

console.log(`
${chalk.bold('Access URLs:')}${divider}
Localhost: ${chalk.magenta(`http://localhost:${port}`)}
LAN: ${chalk.magenta(`http://${ip.address()}:${port}`) +
(tunnelStarted ? `\n Proxy: ${chalk.magenta(tunnelStarted)}` : '')}${divider}
${chalk.blue(`Press ${chalk.italic('CTRL-C')} to stop`)}
`);
},
};

module.exports = logger;

0 comments on commit 33acf4c

Please sign in to comment.