diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..5171c54083337f0b87926da2e8f52abefe19d70f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +npm-debug.log \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5f9a6c00ff13b3cd29741f72e8565c67b8962bac --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package-lock.json +db_data \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..1b4a9982e4e2765dde1131ec660ba2aef7356c8b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM node:10 +WORKDIR /usr/src/app +COPY package*.json ./ +RUN npm install \ No newline at end of file diff --git a/app.js b/app.js index 7d10b5395784f9ceb3fc9512d48ce9f3529f1b94..7d87ae80e4afa235814dcf68ba96513ea8864e2a 100644 --- a/app.js +++ b/app.js @@ -3,10 +3,7 @@ var path = require('path'); var logger = require('morgan'); var bodyParser = require('body-parser'); -var storage = require('./routes/storage')('mongodb://icarus:icarus@localhost:27017/icarus'); -var public_storage = require('./routes/storage')('mongodb://public:public@localhost:27017/public'); - -var services = require('./routes/services'); +var storage = require('./routes/storage')('mongodb://icarus:icarus@db/icarus'); var app = express(); @@ -33,8 +30,6 @@ app.use(function(req, res, next) { }); app.use('/icarus/str', storage); -app.use('/icarus/pub', public_storage); -app.use('/icarus/svc', services); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..5f3363dc52d9baf2d5d729e8b57bb49621bce3e8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3' + +services: + app: + working_dir: /usr/src/app + command: npm start + build: . + depends_on: + - db + volumes: + - node_modules:/usr/src/app/node_modules:cached + - ./:/usr/src/app:cached + ports: + - "8082:3000" #host:container, 3000 is the default port of Express.js + stdin_open: true + tty: true + + db: + image: bitnami/mongodb + volumes: + - ./db_data:/bitnami/mongodb + - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js + +volumes: + node_modules: + diff --git a/init-mongo.js b/init-mongo.js new file mode 100644 index 0000000000000000000000000000000000000000..b8593db2f3908a1fa945645db78ca189a64db288 --- /dev/null +++ b/init-mongo.js @@ -0,0 +1,13 @@ +db = db.getSiblingDB('icarus') //create and set db +db.createUser( + { + user: "icarus", + pwd: "icarus", + roles: [ + { + role: "dbAdmin", + db: "icarus" + } + ] + } +) \ No newline at end of file diff --git a/package.json b/package.json index 47d49ad0ef989e37f6d53b162b4ee04ef5092bcc..86aab910f07e1c958997832843592f1ebd761de1 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "debug": "~2.2.0", "express": "~4.13.1", "mongodb": "~2.0", - "morgan": "~1.6.1", - "sleep": "~3.0.0" + "morgan": "~1.6.1" } } diff --git a/routes/services.js b/routes/services.js deleted file mode 100644 index c92564313751d7e1fc08c1b0c1a4be4e09c5dbe7..0000000000000000000000000000000000000000 --- a/routes/services.js +++ /dev/null @@ -1,20 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -var ip_service = require('./services/invisiblepuzzle/invisiblepuzzle'); -var echo_service = require('./services/echo/echo'); - -router.use('/ip', ip_service); -router.use('/echo', echo_service); - -router.get('/', function(req, res, next) { - - res.send({ - - "services": ["echo","ip"] - - }); - -}); - -module.exports = router; \ No newline at end of file diff --git a/routes/services/echo/echo.js b/routes/services/echo/echo.js deleted file mode 100644 index 13b8059923d50f0149866f9748072e05a08d67fc..0000000000000000000000000000000000000000 --- a/routes/services/echo/echo.js +++ /dev/null @@ -1,11 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET root. */ -router.all('/', function(req, res, next) { - - res.send(req.body); - -}); - -module.exports = router; diff --git a/routes/services/invisiblepuzzle/invisiblepuzzle.js b/routes/services/invisiblepuzzle/invisiblepuzzle.js deleted file mode 100644 index c978aeae24a23b04cd58980c229e95320acc315c..0000000000000000000000000000000000000000 --- a/routes/services/invisiblepuzzle/invisiblepuzzle.js +++ /dev/null @@ -1,372 +0,0 @@ -// PROTOCOL SPECIFICATION -// ----------------------- -// /users/count - returns the number of users and how many of them are using VoiceOver -// /users/vib - returns objects representing who are using VoiceOver -// /users/sighted - returns objects representing who are not using VoiceOver - -var express = require('express'); -var router = express.Router(); - -var debug = require('debug')('Icarus:server'); - -var mongoClient = require('mongodb').MongoClient; - -var assert = assert = require('assert'); - -// Connection URL -var mongoUrl = 'mongodb://readOnly:readOnly@localhost:27017/icarus'; - -//Connect to the server and intialise the pool -var icarusDb; -mongoClient.connect(mongoUrl, function(err, db) { - - assert.equal(null, err, err); - debug("Connected to MongoDB database: "+db.databaseName); - - icarusDb = db; - -}); - -/* GET root . */ -router.get('/', function(req, res, next) { - - res.sendStatus(200); - -}); - -router.get('/users/count', function(req, res, next) { - - var collection = icarusDb.collection('resources'); - - var cursor = collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname":"Invisible Puzzle", - "debug":false - } - }, - - // Stage 2 - { - $group: { - "_id": "$appdata.uuid", - "docs": {$sum: 1}, - "voiceover": {$sum : {$cond: [{$eq:["$appdata.voiceover", true]},1,0]}} - } - }, - - // Stage 3 - { - $project: { - - "docs": 1, - "vo_ratio": {$divide:["$voiceover","$docs"]} - - } - }, - - // Stage 4 - { - $group: { - - "_id": 1, - "users": {$sum: 1}, - "vib": {$sum : {$cond: [{$gt:["$vo_ratio", 0]},1,0]}} - - } - }, - - // Stage 5 - { - - $project: { - - "_id": 0, - "users": 1, - "vib": 1 - - } - - } - ] - - ) - - return_cursor(cursor, req, res, next); - -}); - -router.get('/users/vib', function(req, res, next) { - - var cursor = user_ids_cursor(true); - return_cursor(cursor, req, res, next); - -}); - -router.get('/users/sighted', function(req, res, next) { - - var cursor = user_ids_cursor(false); - return_cursor(cursor, req, res, next); - -}); - -function user_ids_cursor(vib){ - - var filter_condition = {$eq: 0}; - - if(vib==true){ - - filter_condition = {$gt: 0} - - } - - var collection = icarusDb.collection('resources'); - - return collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname":"Invisible Puzzle", - "debug":false - } - }, - - // Stage 2 - { - $group: { - "_id": "$appdata.uuid", - "docs": {$sum: 1}, - "voiceover": {$sum : {$cond: [{$eq:["$appdata.voiceover", true]},1,0]}}, - "levels" : { - "$addToSet" : "$userdata.level.id" - }, - "sonifications" : { - "$addToSet" : "$userdata.sonification" - } - } - - }, - - // Stage 3 - { - $project: { - - "docs": 1, - "vo_ratio": {$divide:["$voiceover","$docs"]}, - "levels": 1, - "sonification": 1 - - } - }, - - // Stage 4 - { - $match: { - - "vo_ratio": filter_condition - - } - } - - ] - - ) - -} - -router.get('/explorations/hp', function(req, res, next) { - - var collection = icarusDb.collection('resources'); - - var cursor = collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname" : "Invisible Puzzle", - "userdata.event" : "exploration", - "appdata.voiceover": true, - "debug" : false - } - }, - - // Stage 2 - { - $group: { - "_id" : 1, - "explorations" : { - "$sum" : 1 - }, - "with_hp" : { - "$sum" : { - "$cond" : [ - { - "$eq" : [ - "$userdata.headphones", - true - ] - }, - 1, - 0 - ] - } - } - } - } - - ] - - ); - - return_cursor(cursor, req, res, next); - -}); - -router.get('/explorations/hpbylevel', function(req, res, next) { - - var collection = icarusDb.collection('resources'); - - var cursor = collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname" : "Invisible Puzzle", - "userdata.event" : "exploration", - "appdata.voiceover": true, - "debug" : false - } - }, - - // Stage 2 - { - $group: { - "_id" : "$userdata.level.id", - "explorations" : { - "$sum" : 1 - }, - "with_hp" : { - "$sum" : { - "$cond" : [ - { - "$eq" : [ - "$userdata.headphones", - true - ] - }, - 1, - 0 - ] - } - } - } - }, - - // Stage 3 - { - $project: { - "_id": 1, - "explorations": 1, - "hp_ratio": {$divide:["$with_hp","$explorations"]} - } - }, - - // Stage 4 - { - $sort: { - "_id": 1 - } - } - - ] - - ); - - - return_cursor(cursor, req, res, next); - -}); - -router.get('/sonifications/bylevel', function(req, res, next) { - - var collection = icarusDb.collection('resources'); - - var cursor = collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname" : "Invisible Puzzle", - "userdata.event" : "exploration", - "appdata.voiceover": true, - "debug" : false - } - }, - - // Stage 2 - { - $project: { - "identifier": { - "lvl": "$userdata.level.id", - "son": "$userdata.sonification" - } - } - }, - - // Stage 3 - { - $group: { - "_id": "$identifier", - "count": {$sum: 1} - } - } - - ] - - ); - - return_cursor(cursor, req, res, next); - -}); - -function return_cursor(cursor, req, res, next){ - - cursor.toArray(function(err, result) { - - if(err!=null){ - - debug(err); - next(err); - - }else{ - - if(result==undefined){ - - res.sendStatus(404); - - }else{ - - res.send(200,result); - - } - - } - - }); - -} - -module.exports = router; \ No newline at end of file