Skip to content

Commit

Permalink
Init project
Browse files Browse the repository at this point in the history
  • Loading branch information
Araan Branco committed Mar 7, 2017
1 parent 9515f70 commit 93b6cce
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["latest"]
}
102 changes: 102 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

# Created by https://www.gitignore.io/api/node,webstorm

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### WebStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### WebStorm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# End of https://www.gitignore.io/api/node,webstorm
# All files tmp
tmp/
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM rafakato/alpine-node-media:latest

RUN apk add --virtual .build-dependencies --no-cache --update alpine-sdk git python

WORKDIR /code

EXPOSE 3000

CMD ["node", "index.js"]
6 changes: 6 additions & 0 deletions controllers/series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import * as db from '../helpers/db';

export const findAll = (request, reply) => {
reply('Series OK');
};
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '2'
services:
mongodb:
image: mongo:3.4.2
volumes:
- ./tmp/mongodb:/data/db
ports:
- "27017:27017"
restart: always
63 changes: 63 additions & 0 deletions helpers/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

import mongoose from 'mongoose';
import Bluebird from 'bluebird';
import Debug from 'debug';

mongoose.Promise = Bluebird;

const debug = Debug('sofanerd.db.mongodb');
const connections = {};

export const connect = (connectionName = Date.now().toString(), connectionUrl = process.env.MONGODB_URL, options = { server: { poolSize: 10 } }) => {
if(!connections.hasOwnProperty(connectionName)) {
debug('Connection to MongoDB');

const connection = mongoose.createConnection(connectionUrl, options);

connections[connectionName] = connection;

if(process.env.DEBUG) {
mongoose.set('debug', true);
}

// Connection Open
connection.once('open', () => {
debug(`Connection open with ${connectionUrl}`);
});

// Connected
connection.once('connected', () => {
debug(`Connected to ${connectionUrl}`);
});

// Disconnect
connection.once('disconnected', () => {
debug(`Disconnected from ${connectionUrl}`);
});

// Error
connection.once('error', (error) => {
debug('Connection error', error);
});

process.on('SIGINT', () => {
connection.close(() => {
debug('Connection closed by ctrl+c command');
process.exit(0);
});
});
}

return {
name: connectionName,
connection: connections[connectionName]
};
};

export const disconnect = async (connectionName) => {
if(connections.hasOwnProperty(connectionName) === false) {
throw new Error(`Connection name ${connectionName} does not exists`);
}

await connections[connectionName].close();
};
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import Hapi from 'hapi';
import dotenv from 'dotenv';
import Debug from 'debug';
import Routes from './plugins/routes';

dotenv.config();

const debug = Debug('sofanerd.server');
const server = new Hapi.Server({
debug: {
request: ['error', 'uncaught']
}
});

server.connection({
port: process.env.NODE_PORT || 3000
});

let initializer = [
Routes
];

try {
server.register(initializer, (error) => {
if(error) {
debug(error);
} else {
debug('Starting server');

server.start((err) => {
if(err) {
debug(err);
} else {
debug(`Server running at: ${server.info.uri}`);
}
});
}
}
);
} catch (err) {
debug(err);
throw err;
}

export default server;
8 changes: 8 additions & 0 deletions models/schemas/serie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import mongoose from 'mongoose';

const Schema = mongoose.Schema;

const schema = new Schema();

export default schema;
8 changes: 8 additions & 0 deletions models/schemas/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import mongoose from 'mongoose';

const Schema = mongoose.Schema;

const schema = new Schema();

export default schema;
4 changes: 4 additions & 0 deletions models/serie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

import Debug from 'debug';

const debug = Debug('sofanerd.models.serie');
4 changes: 4 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

import Debug from 'debug';

const debug = Debug('sofanerd.models.user');
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "sofanerd",
"version": "0.0.1",
"description": "Mediatracker Sofanerd",
"main": "index.js",
"scripts": {
"start": "DEBUG=sofanerd.* babel-node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AraanBranco/sofanerd.git"
},
"keywords": [
"nodejs",
"mediatracker",
"hapijs"
],
"author": "Araan Branco <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/AraanBranco/sofanerd/issues"
},
"homepage": "https://github.com/AraanBranco/sofanerd#readme",
"dependencies": {
"babel-cli": "^6.23.0",
"babel-preset-latest": "^6.22.0",
"bluebird": "^3.5.0",
"debug": "^2.6.1",
"dotenv": "^4.0.0",
"glob": "^7.1.1",
"hapi": "^16.1.0",
"joi": "^10.2.2",
"mongoose": "^4.8.6"
}
}
27 changes: 27 additions & 0 deletions plugins/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import Debug from 'debug';
import Routes from '../routes';

const debug = Debug('sofanerd.routes');

const register = async (server, options, next) => {
try {
debug('Inicialize plugins');

let loadedRoutes = await Routes.load();

server.route(loadedRoutes);

return next();
} catch (err) {
debug(err);
}
};

register.attributes = {
name: 'routes'
};

export default {
register
}
32 changes: 32 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

import Debug from 'debug';
import Bluebird from 'bluebird';
import glob from 'glob';
import path from 'path';

const debug = Debug('sofanerd.routes');
const globAsync = Bluebird.promisify(glob);

export default {
async load() {
try {
let routes = [];
debug('Load all routes');

let files = await globAsync('**/!(index).js', {
cwd: path.resolve(__dirname)
});

files.forEach((file) => {
let allRoutes = require(`./${file}`).default;

routes.push(...allRoutes);
});

debug('Routes loaded');
return routes;
} catch (err) {
debug(err);
}
}
}
13 changes: 13 additions & 0 deletions routes/series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

import Debug from 'debug';
import * as Controller from '../controllers/series';

const debug = Debug('sofanerd.routes.serie');

export default [
{
method: 'GET',
path: '/api/series',
handler: Controller.findAll
}
];

0 comments on commit 93b6cce

Please sign in to comment.