Skip to content

Commit

Permalink
First commit! Hello, World \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecrt committed Aug 15, 2016
0 parents commit 246910a
Show file tree
Hide file tree
Showing 78 changed files with 6,047 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
rethinkdb_data
heapdump-*
logs
uploads
.idea
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM debian:jessie

RUN apt-get update && apt-get install -y \
build-essential \
curl \
nano \
supervisor

# Install Node.js
RUN curl -sL https://deb.nodesource.com/setup_4.x | bash -
RUN apt-get install -y nodejs

# Improve cache invalidations by only running npm if requirements have indeed changed
WORKDIR /app
COPY package.json /app/
RUN npm install

# Supervisor settings
COPY docker/supervisord.conf /etc/supervisor/conf.d/atlas.conf

# Application source code
COPY config/ /app/config
COPY src/ /app/src
COPY index.js /app/
COPY scripts.js /app/

# Expose application server port
EXPOSE 8000

CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf", "-n"]
Empty file added README.md
Empty file.
54 changes: 54 additions & 0 deletions config/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import path from 'path';

export default {
app: {
host: '0.0.0.0',
port: 8000,
jwtKey: process.env.JWT_KEY,
defaultCurrency: 'EUR'
},
database: {
servers: [
{
host: process.env.DB_PORT_28015_TCP_ADDR || 'localhost',
port: process.env.DB_PORT_28015_TCP_PORT || 28015
}
],
name: 'atlas'
},
logs: {
folder: path.join(__dirname, '../logs/'),
streams: [
{
level: 'debug',
stream: process.stdout // log INFO and above to stdout
}
]
},
uploads: {
provider: 'atlas',
folder: path.join(process.cwd(), 'uploads'),
baseUrl: 'localhost:8000/uploads'
},
emails: {
from: {
name: 'Nicistore.com',
email: '[email protected]'
}
},
storefront: {
label: 'nicistore.com',
baseUrl: 'http://localhost:3000',
defaultLocale: 'pt'
},
switchPayments: {
enabled: true,
baseUrl: 'https://api-test.switchpayments.com/v2',
accountId: process.env.SWITCH_ACCOUNT_ID,
privateKey: process.env.SWITCH_PRIVATE_KEY
},
mailgun: {
domain: process.env.MAILGUN_DOMAIN,
apiKey: process.env.MAILGUN_API_KEY
}
}
19 changes: 19 additions & 0 deletions config/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Base configuration
import config from './development';

// Override configurations for Production environment
config.app.routePrefix = '/api';
config.logs.folder = '/var/log';
config.logs.streams = [
{
level: 'info',
path: config.logs.folder + '/atlas.log'
}
];
config.uploads.folder = '/uploads';
config.uploads.baseUrl = 'nicistore.com/files';
config.storefront.baseUrl = 'https://nicistore.com';
config.switchPayments.baseUrl = 'https://api.switchpayments.com/v2';

// Export
export default config;
8 changes: 8 additions & 0 deletions docker/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[program:atlas]
command=node index.js
directory=/app
autostart=true
autorestart=true
environment=NODE_ENV=production
stderr_logfile=/var/log/atlas.err.log
stdout_logfile=/var/log/atlas.out.log
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Automatically hook babel into all node requires.
*/
require('babel/register')({
optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators']
});

/**
* Start application server.
*/
require('./src/server');
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "atlas",
"version": "1.0.0",
"description": "E-Commerce Backend API",
"private": false,
"main": "index.js",
"scripts": {
"dev": "NODE_ENV=development nodemon index | bunyan"
},
"author": "André Tavares",
"engines": {
"node": "4.2.5",
"npm": "3.5.3"
},
"dependencies": {
"babel": "5.8.38",
"bcrypt": "0.8.5",
"bunyan": "1.8.0",
"fast-csv": "1.1.0",
"handlebars": "^4.0.5",
"hapi": "11.1.4",
"hapi-async-handler": "1.0.3",
"hapi-auth-jwt2": "5.8.0",
"inert": "3.2.0",
"joi": "6.10.1",
"jsonwebtoken": "5.7.0",
"node-uuid": "1.4.7",
"request": "2.71.0",
"rethinkdbdash": "2.2.18"
},
"devDependencies": {
"nodemon": "1.9.1"
}
}
17 changes: 17 additions & 0 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Automatically hook babel into all node requires.
*/
require('babel/register')({
optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators']
});

/**
* Execute requested script
*/
var resource = process.argv[2];
var script = process.argv[3] || '';
try {
require('./src/resources/' + resource + '/scripts/' + script)(process.argv.slice(4));
} catch (err) {
console.log('Invalid script');
}
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Load config according to environment
export default require('../config/' + process.env.NODE_ENV);
42 changes: 42 additions & 0 deletions src/core/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Imports
*/
import config from '../config';
import log from './logging';
import {UserStatus, User} from '../resources/users/models';

/**
* JWT Authentication
*/
class JWTAuthentication {

/**
* Returns the secret key used to check the signature of the token or a key lookup function.
*/
static getPrivateKey() {
return config.app.jwtKey;
}

/**
* Validates if the given JWT (e.g. user exists, session hasn't expired, etc)
*/
static async validate(decoded, request, callback) {
try {
var user = await User.get(decoded.id);
} catch (err) {
log.error(err, 'Unable to validate JWT');
return callback(null, false);
}

if (!user || user.status !== UserStatus.ACTIVE) {
return callback(null, false);
} else {
return callback(null, true, user);
}
};
}

/**
* Export
*/
export {JWTAuthentication};
68 changes: 68 additions & 0 deletions src/core/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Imports
*/
import config from '../config';

/**
* Import and initialize RethinkDB connection pool
*/
const rethinkdb = require('rethinkdbdash')({servers: config.database.servers});

/**
* Test if application's database exists
*/
async function testDatabase() {
return new Promise(async function (resolve, reject) {
try {
let dbExists = !!(await rethinkdb.dbList()).includes(config.database.name);
if (!dbExists) {
return reject({detail: 'Database does not exist'});
} else {
return resolve();
}
} catch (err) {
reject(err);
}

});
}

/**
* Decorators
*/
class Decorators {

/**
* @param tableName
* @returns {Function}
*/
static table(tableName) {
let wrapper = function (tableName, method) {
if (typeof method != 'function') {
throw {
message: 'decorator must be applied to a function'
}
}
return async function () {
try {
let method_ = method.bind({
table: rethinkdb.db(config.database.name).table(tableName)
});
var result = await method_.call(this, ...arguments); // call method
} catch (err) {
throw err;
}
return result;
}
};
return function decorator(target, key, descriptor) {
descriptor.value = wrapper(tableName, descriptor.value);
return descriptor;
};
}
}

/**
* Exports
*/
export {rethinkdb, testDatabase, Decorators};
Loading

0 comments on commit 246910a

Please sign in to comment.