Skip to content

Commit

Permalink
adding serviceinstanceservice
Browse files Browse the repository at this point in the history
  • Loading branch information
komushi committed Jan 26, 2015
1 parent 298ff87 commit 639396e
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cfignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
logs/*
!.gitkeep
node_modules/
tmp
.DS_Store
.idea
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node ./server/app.js
6 changes: 6 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
applications:
- name: cf-mysql-node-broker
memory: 256M
instances: 1
host: cf-mysql-node-broker
36 changes: 36 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
0 info it worked if it ends with ok
1 verbose cli [ 'node', '/usr/local/bin/npm', 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose node symlink /usr/local/bin/node
5 verbose run-script [ 'prestart', 'start', 'poststart' ]
6 info prestart [email protected]
7 info start [email protected]
8 verbose unsafe-perm in lifecycle true
9 info [email protected] Failed to exec start script
10 verbose stack Error: [email protected] start: `node ./server/app.js`
10 verbose stack Exit status 8
10 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:212:16)
10 verbose stack at EventEmitter.emit (events.js:98:17)
10 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:14:12)
10 verbose stack at ChildProcess.emit (events.js:98:17)
10 verbose stack at maybeClose (child_process.js:756:16)
10 verbose stack at Process.ChildProcess._handle.onexit (child_process.js:823:5)
11 verbose pkgid [email protected]
12 verbose cwd /Users/xul15/Documents/work/nodejs_app/cf-mysql-node-broker
13 error Darwin 13.4.0
14 error argv "node" "/usr/local/bin/npm" "start"
15 error node v0.10.33
16 error npm v2.1.8
17 error code ELIFECYCLE
18 error [email protected] start: `node ./server/app.js`
18 error Exit status 8
19 error Failed at the [email protected] start script 'node ./server/app.js'.
19 error This is most likely a problem with the cf-mysql-node-broker package,
19 error not with npm itself.
19 error Tell the author that this fails on your system:
19 error node ./server/app.js
19 error You can get their info via:
19 error npm owner ls cf-mysql-node-broker
19 error There is likely additional logging output above.
20 verbose exit [ 1, true ]
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "cf-mysql-node-broker",
"version": "1.0.0",
"description": "mysql service broker built on node.js for cloud foundry",
"main": "index.js",
"scripts": {
"start": "node ./server/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/komushi/cf-mysql-node-broker.git"
},
"keywords": [
"cf",
"cloud",
"foundry",
"service",
"broker"
],
"author": "Lei Xu",
"license": "ISC",
"dependencies": {
"express": "~4.0.0",
"body-parser":"1.10.x",
"basic-auth":"~1.0.0",
"fs":"~0.0.2",
"js-yaml":"3.2.x",
"q":"1.1.x",
"mysql":"~2.5.0",
"cfenv":"1.0.x"

},
"bugs": {
"url": "https://github.com/komushi/cf-mysql-node-broker/issues"
},
"homepage": "https://github.com/komushi/cf-mysql-node-broker"
}
4 changes: 4 additions & 0 deletions resources/mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
host: localhost
port: 3306
user: root
password:
32 changes: 32 additions & 0 deletions resources/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file should not be used in deployed environments. Instead, set
# the SETTINGS_PATH environment variable to point to a configuration
# file that contains these settings.


services:
- name: p-mysql
id: 3101b971-1044-4816-a7ac-9ded2e028079
description: MySQL service for application development and testing
tags:
- mysql
- relational
max_db_per_node: 250
metadata:
displayName: MySQL for Pivotal CF
imageUrl: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAA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9FyAJRIgzIUk5KNeN0753jbHy2vQRicCdnwVtX27e4SMA2ADIsnV42gvOG4cAC0iUgyGqV5CbZ/Nmd5L3NfJ3eMMRlI82jjtNk2g4+AZjHKxkxuo+7qxfV4tUXOwkqk9wW7tbwEGAJwbJQSR3aMDAAAAAElFTkSuQmCC
longDescription: Provisioning a service instance creates a MySQL database. Binding applications to the instance creates unique credentials for each application to access the database
providerDisplayName: Pivotal Software
documentationUrl: http://docs.pivotal.io/p-mysql
supportUrl: http://support.pivotal.io
plans:
- name: 5mb
id: 2451fa22-df16-4c10-ba6e-1f682d3dcdc9
description: Shared MySQL Server, 5mb persistent disk, 40 max concurrent connections
max_storage_mb: 5 # in MB
metadata:
cost: 0.0
bullets:
- content: Shared MySQL server
- content: 5 MB storage
- content: 40 concurrent connections

44 changes: 44 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
console.log("process.cwd():" + process.cwd());

/**************************/
/* config */
var application_root = __dirname;
var express = require("express");
var path = require("path");
var Q = require("q");
var bodyParser = require('body-parser');
// var api = require('./controllers/catalogController');
var routes = require('./routes/routes');
var basicAuth = require('./utils/basicAuth');
var app = express();

var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};

app.disable('etag');
app.use(allowCrossDomain);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(path.join(application_root, "public")));
app.use('/v2', basicAuth.auth('test', 'test'));
app.use('/', routes);

/* config */
/**************************/

var server = app.listen((process.env.PORT || 9000), function() {
console.log('Express server listening on port ' + server.address().port);
});
12 changes: 12 additions & 0 deletions server/controllers/catalogController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var resourceManager = require('../utils/resourceManager');

exports.getCatalog = function(req, res) {

resourceManager.getResource("./resources/settings.yml", function (err, jsonString) {

res.set('Content-Type', 'application/json');
res.send(jsonString);
});

};

22 changes: 22 additions & 0 deletions server/controllers/serviceInstanceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var service = require('../services/serviceInstanceService.js');

exports.getMySQL = function(req, res) {
service.executeQuery("select 1 + 1 as result", function (err, result) {

console.log("result:" + JSON.stringify(result));
res.set('Content-Type', 'application/json');
res.send(result);
});

};


exports.create = function(req, res) {
service.update(req.params.id, function (err, result) {

console.log("result:" + JSON.stringify(result));
res.set('Content-Type', 'application/json');
res.send(result);
});

};
47 changes: 47 additions & 0 deletions server/routes/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var express=require('express');
var catalogController = require('../controllers/catalogController');
var serviceInstanceController = require('../controllers/serviceInstanceController');

//configure routes

var router=express.Router();

/**************************/
/* REST API hello */
router.route('/v2')
.get(function (req, res) {
res.send('REST API is running.');
console.log("REST API is running.");
});
/* REST API hello */
/**************************/

/**************************/
/* REST API /v2/catalog */
router.route('/v2/catalog')
.get(function (req, res) {
console.log("Begin: /v2/catalog");

catalogController.getCatalog(req, res);

console.log("End: /v2/catalog");
});

/* REST API getCountByCat */
/**************************/

/**************************/
/* REST API /v2/catalog */
router.route('/v2/service_instances/:instance_id')
.put(function (req, res) {
console.log("Begin: /v2/service_instances/:instance_id");

serviceInstanceController.getMySQL(req, res);

console.log("End: /v2/service_instances/:instance_id");
});

/* REST API getCountByCat */
/**************************/

module.exports=router;
21 changes: 21 additions & 0 deletions server/services/serviceInstanceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var mysqlManager = require('../utils/mysqlManager');


exports.executeQuery = function(queryText, next) {


mysqlManager.executeQuery(queryText, next);


};


exports.update = function(instanceId, next) {


var queryText = "CREATE DATABASE " + instanceId;

mysqlManager.executeQuery(queryText, next);


};
29 changes: 29 additions & 0 deletions server/utils/basicAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/**
* Your utility library for express
*/

var basicAuth = require('basic-auth');

/**
* Simple basic auth middleware for use with Express 4.x.
*
* @example
* app.use('/api-requiring-auth', basicAuth.auth('username', 'password'));
*
* @param {string} username Expected username
* @param {string} password Expected password
* @returns {function} Express 4 middleware requiring the given credentials
*/
exports.auth = function(username, password) {
return function(req, res, next) {
var user = basicAuth(req);

if (!user || user.name !== username || user.pass !== password) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.send(401);
}

next();
};
};
Loading

0 comments on commit 639396e

Please sign in to comment.