-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
3,906 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/dist | ||
/logs | ||
/npm-debug.log | ||
/node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
FROM alpine:3.4 | ||
|
||
# File Author / Maintainer | ||
LABEL authors="Zouhir Chahoud <[email protected]>" | ||
|
||
# Update & install required packages | ||
RUN apk add --update nodejs bash git | ||
|
||
# Install app dependencies | ||
COPY package.json /www/package.json | ||
RUN cd /www; npm install | ||
|
||
# Copy app source | ||
COPY . /www | ||
|
||
# Set work directory to /www | ||
WORKDIR /www | ||
|
||
# set your port | ||
ENV PORT 8080 | ||
|
||
# expose the port to outside world | ||
EXPOSE 8080 | ||
|
||
# start command as per package.json | ||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Jason Miller | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: npm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "express-es6-rest-api", | ||
"version": "0.3.0", | ||
"description": "Starter project for an ES6 RESTful Express API", | ||
"main": "dist", | ||
"scripts": { | ||
"dev": "nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"", | ||
"build": "babel src -s -D -d dist --presets es2015,stage-0", | ||
"start": "node dist", | ||
"prestart": "npm run -s build", | ||
"test": "eslint src" | ||
}, | ||
"eslintConfig": { | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 7, | ||
"sourceType": "module" | ||
}, | ||
"env": { | ||
"node": true | ||
}, | ||
"rules": { | ||
"no-console": 0, | ||
"no-unused-vars": 1 | ||
} | ||
}, | ||
"repository": "developit/express-es6-rest-api", | ||
"author": "Jason Miller <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"body-parser": "^1.13.3", | ||
"compression": "^1.5.2", | ||
"cors": "^2.7.1", | ||
"express": "^4.13.3", | ||
"lodash": "^4.17.11", | ||
"morgan": "^1.8.0", | ||
"pusher": "^2.2.0", | ||
"resource-router-middleware": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.9.0", | ||
"babel-core": "^6.9.0", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"eslint": "^3.1.1", | ||
"nodemon": "^1.9.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import resource from 'resource-router-middleware'; | ||
import facets from '../models/facets'; | ||
|
||
export default ({ config, db }) => resource({ | ||
|
||
/** Property name to store preloaded entity on `request`. */ | ||
id : 'facet', | ||
|
||
/** For requests with an `id`, you can auto-load the entity. | ||
* Errors terminate the request, success sets `req[id] = data`. | ||
*/ | ||
load(req, id, callback) { | ||
let facet = facets.find( facet => facet.id===id ), | ||
err = facet ? null : 'Not found'; | ||
callback(err, facet); | ||
}, | ||
|
||
/** GET / - List all entities */ | ||
index({ params }, res) { | ||
res.json(facets); | ||
}, | ||
|
||
/** POST / - Create a new entity */ | ||
create({ body }, res) { | ||
body.id = facets.length.toString(36); | ||
facets.push(body); | ||
res.json(body); | ||
}, | ||
|
||
/** GET /:id - Return a given entity */ | ||
read({ facet }, res) { | ||
res.json(facet); | ||
}, | ||
|
||
/** PUT /:id - Update a given entity */ | ||
update({ facet, body }, res) { | ||
for (let key in body) { | ||
if (key!=='id') { | ||
facet[key] = body[key]; | ||
} | ||
} | ||
res.sendStatus(204); | ||
}, | ||
|
||
/** DELETE /:id - Delete a given entity */ | ||
delete({ facet }, res) { | ||
facets.splice(facets.indexOf(facet), 1); | ||
res.sendStatus(204); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Pusher from "pusher"; | ||
import _ from "lodash"; | ||
import { version } from "../../package.json"; | ||
import { Router } from "express"; | ||
import facets from "./facets"; | ||
|
||
var pusher = new Pusher({ | ||
appId: "675152", | ||
key: "d61e39bd8719a7ff7ea6", | ||
secret: "c7bd136c6855fb3500b1", | ||
cluster: "eu", | ||
encrypted: true | ||
}); | ||
|
||
let players = []; | ||
|
||
const updatedPlayersByUsername = (players, username, value) => { | ||
return players.map(player => { | ||
if (player.username === username) { | ||
return { ...player, ...value }; | ||
} | ||
return { ...player }; | ||
}); | ||
}; | ||
|
||
export default ({ config, db }) => { | ||
let api = Router(); | ||
|
||
// mount the facets resource | ||
api.use("/facets", facets({ config, db })); | ||
|
||
api.get("/", (req, res) => { | ||
res.json(players); | ||
}); | ||
|
||
api.post("/join", (req, res) => { | ||
const username = req.body.username; | ||
players.push({ | ||
username, | ||
score: 0, | ||
order: 0 | ||
}); | ||
players = _.orderBy(players, ["username"], ["asc"]); | ||
pusher.trigger("buzzer-channel", "players-update", { | ||
message: players | ||
}); | ||
res.json({ version }); | ||
}); | ||
|
||
api.post("/buzz", (req, res) => { | ||
const username = req.body.username; | ||
const lastPlayer = _.maxBy(players, "order"); | ||
|
||
players = updatedPlayersByUsername(players, username, { | ||
order: lastPlayer.order + 1 | ||
}); | ||
pusher.trigger("buzzer-channel", "players-update", { | ||
message: players | ||
}); | ||
res.json({ version }); | ||
}); | ||
|
||
api.post("/correct", (req, res) => { | ||
const username = req.body.username; | ||
const lastPlayer = _.maxBy(players, "order"); | ||
|
||
players = _.map(players, player => { | ||
if (player.order === 1) { | ||
return { ...player, order: 0, score: player.score + 1 }; | ||
} | ||
return { ...player, order: 0 }; | ||
}); | ||
pusher.trigger("buzzer-channel", "players-update", { | ||
message: players | ||
}); | ||
res.json({ version }); | ||
}); | ||
|
||
api.post("/incorrect", (req, res) => { | ||
const username = req.body.username; | ||
const lastPlayer = _.maxBy(players, "order"); | ||
|
||
players = _.map(players, player => { | ||
if (player.order === 1) { | ||
return { ...player, order: -1 }; | ||
} | ||
|
||
if (player.order > 1) { | ||
return { ...player, order: player.order - 1 }; | ||
} | ||
|
||
return player; | ||
}); | ||
|
||
pusher.trigger("buzzer-channel", "players-update", { | ||
message: players | ||
}); | ||
res.json({ version }); | ||
}); | ||
|
||
return api; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"port": 8080, | ||
"bodyLimit": "100kb", | ||
"corsHeaders": ["Link"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default callback => { | ||
// connect to a database if needed, then pass it to `callback`: | ||
callback(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import http from 'http'; | ||
import express from 'express'; | ||
import cors from 'cors'; | ||
import morgan from 'morgan'; | ||
import bodyParser from 'body-parser'; | ||
import initializeDb from './db'; | ||
import middleware from './middleware'; | ||
import api from './api'; | ||
import config from './config.json'; | ||
|
||
let app = express(); | ||
app.server = http.createServer(app); | ||
|
||
// logger | ||
app.use(morgan('dev')); | ||
|
||
// 3rd party middleware | ||
app.use(cors({ | ||
exposedHeaders: config.corsHeaders | ||
})); | ||
|
||
app.use(bodyParser.json({ | ||
limit : config.bodyLimit | ||
})); | ||
|
||
// connect to db | ||
initializeDb( db => { | ||
|
||
// internal middleware | ||
app.use(middleware({ config, db })); | ||
|
||
// api router | ||
app.use('/api', api({ config, db })); | ||
|
||
app.server.listen(process.env.PORT || config.port, () => { | ||
console.log(`Started on port ${app.server.address().port}`); | ||
}); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
/** Creates a callback that proxies node callback style arguments to an Express Response object. | ||
* @param {express.Response} res Express HTTP Response | ||
* @param {number} [status=200] Status code to send on success | ||
* | ||
* @example | ||
* list(req, res) { | ||
* collection.find({}, toRes(res)); | ||
* } | ||
*/ | ||
export function toRes(res, status=200) { | ||
return (err, thing) => { | ||
if (err) return res.status(500).send(err); | ||
|
||
if (thing && typeof thing.toObject==='function') { | ||
thing = thing.toObject(); | ||
} | ||
res.status(status).json(thing); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router } from 'express'; | ||
|
||
export default ({ config, db }) => { | ||
let routes = Router(); | ||
|
||
// add middleware here | ||
|
||
return routes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// our example model is just an Array | ||
const facets = []; | ||
export default facets; |
Oops, something went wrong.