Skip to content

Commit

Permalink
Merge pull request #1 from oSoc15/master
Browse files Browse the repository at this point in the history
Added config file and CORS
  • Loading branch information
Pieter Colpaert committed Aug 2, 2015
2 parents 0b7babc + 3f60452 commit 9fa00c2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
.idea/

# config file for deployment
config.json
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

Show the real-time data of the Belgian railway company in a simple overview

## install

After cloning this repo, perform
```bash
npm install
```

Before running the server, make sure your config.json is set. See the config-example.json for an example.

Run the server using

```bash
nodejs script.js
```
7 changes: 7 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"port" : "3000",
"gtfs-urls" : {
"service-alerts" : "http://irail.gent/service_alerts.pb",
"trip-updates" : "http://irail.gent/trip_updates.pb"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"url": "https://github.com/oSoc15/gtfs-rt-demo/issues"
},
"dependencies": {
"cors": "^2.7.1",
"express": "^4.13.1",
"fs": "0.0.2",
"gtfs-realtime-bindings": "0.0.4",
"protocol-buffers": "^3.1.2",
"request": "^2.60.0"
Expand Down
13 changes: 5 additions & 8 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
$(document).ready(function(){
console.log('Hi there');

$.ajax({url: "http://localhost:3000/service_alerts.json", success: function(result){
$.ajax({url: "/service_alerts.json", success: function(result){
console.log('Hi data');
console.log(result.header);
parseServiceAlertData(result);
}});
$.ajax({url: "http://localhost:3000/trip_updates.json", success: function(result){
$.ajax({url: "/trip_updates.json", success: function(result){
console.log('Hi data');
console.log(result.header);
parseTripUpdateData(result);
Expand All @@ -20,10 +20,10 @@ console.log('Hi there');
window.setInterval(function() {
console.log('Hi Minute');

$.ajax({url: "http://localhost:3000/service_alerts.json", success: function(result){
$.ajax({url: "/service_alerts.json", success: function(result){
parseServiceAlertData(result);
}});
$.ajax({url: "http://localhost:3000/trip_updates.json", success: function(result){
$.ajax({url: "/trip_updates.json", success: function(result){
parseTripUpdateData(result);
}});

Expand Down Expand Up @@ -108,9 +108,6 @@ console.log('Hi there');
totalDelay+
"</td>"+
"</tr>");



}
}
});
});
41 changes: 15 additions & 26 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/**
* Created by timtijssens on 29/07/15.
*/
var GtfsRealtimeBindings = require('gtfs-realtime-bindings'),
request = require('request'),
express = require('express'),
cors = require('cors'),
fs = require('fs');


var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
var request = require('request');
var express = require('express');
var app = express();
app.use('/', express.static('public'));
fs = require('fs');
app.use('/service_alerts.json',cors());
app.use('/trip_updates.json',cors());

var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));

var requestSettingsTripUpdates = {
method: 'GET',
//url: 'http://localhost:8000/trip_updates.pb',
url: 'http://irail.gent/trip_updates.pb',
url: config['gtfs-urls']['trip-updates'],
encoding: null
};
var requestSettingsServiceAlerts = {
method: 'GET',
//url: 'http://localhost:8000/service_alerts.pb',
url: 'http://irail.gent/service_alerts.pb',
url: config['gtfs-urls']['service-alerts'],
encoding: null
};

Expand All @@ -31,26 +32,21 @@ request(requestSettingsServiceAlerts, function (error, response, body) {

fs.writeFile('public/service_alerts.json', JSON.stringify(feed), function (err) {
if (err) return console.log(err);

});


}
});
request(requestSettingsTripUpdates, function (error, response, body) {

request(requestSettingsTripUpdates, function (error, response, body) {
if (!error && response.statusCode == 200) {

var feed = GtfsRealtimeBindings.FeedMessage.decode(body);

fs.writeFile('public/trip_updates.json', JSON.stringify(feed), function (err) {
if (err) return console.log(err);
//console.log('Hello World > helloworld.txt');
});
//console.log(feed.entity[0]);

}
});

var minutes = 1, the_interval = minutes * 60 * 1000;
setInterval(function() {
console.log("I am doing my minute check");
Expand Down Expand Up @@ -83,18 +79,11 @@ setInterval(function() {
//console.log('Hello World > helloworld.txt');
});
//console.log(feed.entity[0]);

}
});



}, the_interval);





app.get('/servicesAlerts', function (req, res) {
console.log("Got Service Alerts Request");

Expand Down Expand Up @@ -134,7 +123,7 @@ app.get('/tripUpdates', function (req, res) {
});
});

var server = app.listen(3000, function () {
var server = app.listen(config.port, function () {
var host = server.address().address;
var port = server.address().port;

Expand Down

0 comments on commit 9fa00c2

Please sign in to comment.