forked from yoonic/atlas
-
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
0 parents
commit 246910a
Showing
78 changed files
with
6,047 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,6 @@ | ||
node_modules | ||
rethinkdb_data | ||
heapdump-* | ||
logs | ||
uploads | ||
.idea |
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,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.
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,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 | ||
} | ||
} |
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,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; |
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,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 |
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,11 @@ | ||
/** | ||
* Automatically hook babel into all node requires. | ||
*/ | ||
require('babel/register')({ | ||
optional: ['es7.asyncFunctions', 'es7.classProperties', 'es7.decorators'] | ||
}); | ||
|
||
/** | ||
* Start application server. | ||
*/ | ||
require('./src/server'); |
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,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" | ||
} | ||
} |
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,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'); | ||
} |
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,2 @@ | ||
// Load config according to environment | ||
export default require('../config/' + process.env.NODE_ENV); |
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,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}; |
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,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}; |
Oops, something went wrong.