Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Bieser committed May 16, 2016
0 parents commit 8f8ad68
Show file tree
Hide file tree
Showing 27 changed files with 333 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Empty file added .foreverignore
Empty file.
22 changes: 22 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"node": true,
"es5": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> The place to record changes made in each new version.
#### 0.0.1 - May 16, 2016
- API generated.
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM docker-hub.8-network.com/node:4.2.2

MAINTAINER Demandforce <[email protected]>

#Want docker to cache node_modules, unless there is a change to package.json
ADD package.json /tmp/package.json
RUN cd /tmp && npm config set strict-ssl false && npm install

WORKDIR /d3-usr/src/app

RUN mkdir -p /usr/src/app/log && cp -ar /tmp/node_modules /d3-usr/src/app/node_modules

ADD . /d3-usr/src/app

CMD npm start
Empty file added README.md
Empty file.
53 changes: 53 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var express = require('express');
var path = require('path');
require("coffee-script/register")
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var v1Routes = require('./routes/v1');
var admin = require('./routes/admin');

var app = express();

var env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = env == 'development';

// app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(logger(env));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());

app.use('/v1', v1Routes);
app.use('/', v1Routes);
app.use('/admin', admin);


/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

/// error handler

// development error handler
// will print stacktrace
app.use(function(err, req, res, next) {
res.status(err.status || 500);
if (app.get('env') === 'development') {
res.send(err.message)
} else {
res.end();
}


});


module.exports = app;
3 changes: 3 additions & 0 deletions bin/ci/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# To be used in CI deploy steps
set -e
17 changes: 17 additions & 0 deletions bin/ci/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# To be used in CI publish steps
set -e

echo DOCKER_REGISTRY_HOST=${DOCKER_REGISTRY_HOST}
echo PROJECT_NAME=${PROJECT_NAME}
echo SHA=$SHA
echo ${DOCKER_REGISTRY_HOST}/${PROJECT_NAME}:$SHA
echo ""

docker build -t ${DOCKER_REGISTRY_HOST}/${PROJECT_NAME}:$SHA .
docker push ${DOCKER_REGISTRY_HOST}/${PROJECT_NAME}:$SHA

docker build -t ${DOCKER_REGISTRY_HOST}/${PROJECT_NAME}:production .
docker push ${DOCKER_REGISTRY_HOST}/${PROJECT_NAME}:production


5 changes: 5 additions & 0 deletions bin/ci/test-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# To be used in CI build steps
set -e
sleep 20
docker-compose run -T app npm test
10 changes: 10 additions & 0 deletions bin/ci/test-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# To be used in CI build steps
set -e

docker pull docker-hub.8-network.com/node

docker-compose stop && \
docker-compose rm --force

docker-compose build
5 changes: 5 additions & 0 deletions bin/ci/test-teardown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# To be used in CI build steps
set -e
docker-compose kill
docker-compose rm --force
8 changes: 8 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
app:
build: .
command: npm start
ports:
- "3000:3000"
volumes:
- ./:/dev-d3-usr
17 changes: 17 additions & 0 deletions lib/business.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
var db = require("../utils/db-connection")

function getBusinessById(id, callback){
let GET_BUSINESS = "SELECT ID as id, NAME as name, STATUS as status FROM Business WHERE ID = ?"
db.query({businessId:id}, GET_BUSINESS, [id], function(err, results){
if(err) {
callback(err)
} else {
callback(null, results[0])
}
});
};

module.exports = {
getBusinessById: getBusinessById
};
44 changes: 44 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var PORTAL_URL, config, log, request, url;
request = require('request');
config = require('../config/env.json')[process.env.NODE_ENV];
url = require("url");
log = require('../config/logger').mainLogger;

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
process.env.NODE_ENV = process.env.NODE_ENV || "development";

PORTAL_URL = config.portal.url;

module.exports = function(cookie) {
var options;
options = {
url: PORTAL_URL + "/bp2/session.jsp",
headers: {
cookie: "JSESSIONID=" + req.cookies.JSESSIONID
},
followRedirect: false
};
return request(options, function(error, response, body) {
var url_parts;
if (error) {
return res.status(500).end();
}
if (response.statusCode === 200) {
req.business = JSON.parse(body).business;
return next();
} else if (response.statusCode === 301 || response.statusCode === 302) {
url_parts = url.parse(response.headers.location);
log.error({
err: {
response: response.statusCode,
cookie: req.cookies,
sessionID: req.cookies.JSESSIONID,
url_parts: url_parts
}
}, 'session 300');
res.redirect("/bp2/secureRedirect.jsp?redirectUrl=/portal/settings/scheduling");
} else {
return res.status(500).end();
}
});
};
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "foo2",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node_modules/.bin/forever -w ./bin/www"
},
"dependencies": {
"body-parser": "^1.13.3",
"bunyan": "^1.7.1",
"coffee-script": "^1.10.0",
"cookie-parser": "^1.3.3",
"d3-database": "1.0.4",
"express": "^4.13.3",
"forever": "^0.15.1",
"morgan": "^1.6.1",
"serve-favicon": "^2.3.0"
},
"devDependencies": {
"mocha": "2.4.5"
}
}
18 changes: 18 additions & 0 deletions routeHandlers/businessHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var business = require("../lib/business")

function getBusinessId(req, res, next){
business.getBusinessById(req.params["businessId"], function(err, business){
if(err) {
console.log(err.message)
next(err)
} else if(business==null) {
next(new Error("Business not found"))
} else {
res.send(business)
}
});
}

module.exports = {
getBusinessId: getBusinessId
}
9 changes: 9 additions & 0 deletions routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET health */
router.get('/health', function(req, res) {
res.send('OK');
});

module.exports = router;
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = require("v1")

9 changes: 9 additions & 0 deletions routes/v1/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();

/* GET health */
router.get('/health', function(req, res) {
res.send('OK');
});

module.exports = router;
13 changes: 13 additions & 0 deletions routes/v1/business.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var express = require('express');
var router = express.Router();
var businessHandler = require("../../routeHandlers/businessHandler")

router.get('/:businessId', function(req, res, next) {
businessHandler.getBusinessById(req, res, next);
});

router.get("/", function(req, res){
res.send("Hello Business World!");
})

module.exports = router;
11 changes: 11 additions & 0 deletions routes/v1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var express = require('express');
var router = express.Router();
var businessRoutes = require("./business")

router.get('/', function(req, res) {
res.send("Hello World!")
});

router.use('/business', businessRoutes)

module.exports = router;
Empty file added test/.npmignore
Empty file.
Empty file added utils/README.md
Empty file.
10 changes: 10 additions & 0 deletions utils/db-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var logger = require("./logger");
var POOLSIZE = 10;
var routes = require("d3-database").routes;
var connection = require("d3-database").connection(routes[process.env.NODE_ENV || "development"], POOLSIZE, logger);

module.exports = {
nodegroups: connection.nodegroups,
query: connection.query,
ping: connection.ping
};
19 changes: 19 additions & 0 deletions utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var LOG_LEVEL, Logger, configs;

Logger = require('bunyan');

if (process.env.DEBUG) {
LOG_LEVEL = 'debug';
}

configs = {
name: process.env.NODE_PROCESS_NAME || 'business-api',
streams: [
{
stream: process.stdout,
level: LOG_LEVEL || 'info'
}
]
};

module.exports = new Logger(configs);

0 comments on commit 8f8ad68

Please sign in to comment.