Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
nickchisiu committed Aug 20, 2017
1 parent 2365937 commit 8482f00
Show file tree
Hide file tree
Showing 14 changed files with 623 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/*{.,-}min.js
**/node_modules/**
/_book
public/
test-projects/
/docs
coverage
68 changes: 68 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"extends": "airbnb",
"plugins": [],
"env": {
"node": true,
"browser": true,
"mocha": true
},
"rules": {
"brace-style": [
2,
"1tbs",
{
"allowSingleLine": true
}
],
"comma-dangle": [
2,
"never"
],
"complexity": [
2,
6
],
"curly": 2,
"eqeqeq": [
2,
"allow-null"
],
"max-statements": [
2,
30
],
"react/react-in-jsx-scope" : 0,
"react/jsx-filename-extension" : 0,
"arrow-body-style": 0,
"consistent-return": 0,
"no-shadow-restricted-names": 2,
"no-undef": 0,
"no-use-before-define": 2,
"radix": 2,
"semi": 2,
"space-infix-ops": 2,
"key-spacing": 0,
"no-multi-spaces": 0,
"import/extensions" : 0,
"object-shorthand": [
2,
"consistent"
],
"no-param-reassign": 0,
"prefer-rest-params": 0,
"class-methods-use-this": 0,
"strict": 0,
"import/no-extraneous-dependencies": "off",
"import/no-dynamic-require": "off",
"react/require-extension": "off",
"import/no-unresolved" : "off"
},
"globals": {
"AnalysisView": true,
"PollingView": true,
"Prism": true,
"Spinner": true,
"Timer": true,
"moment": true
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down Expand Up @@ -57,3 +56,5 @@ typings/
# dotenv environment variables file
.env

# IntelliJ IDEA
.idea
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is to avoid `npm install` encounter chmod ENOENT error.
# Reference: http://stackoverflow.com/questions/17990647/npm-install-errors-with-error-enoent-chmod

# Ignore there folders because they will not be used in production code.
# Reference: http://blog.xebia.com/2015/09/22/publishing-es6-code-to-npm/
docs/
md/
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: node_js

node_js:
- "6"

services:
- docker

before_install:
- pip install --user awscli
- export PATH=$PATH:$HOME/.local/bin

install:
- travis_retry npm install

script:
- set -e
- npm run eslint
- npm run jsinspect

notifications:
webhooks:
urls:
- https://webhooks.gitter.im/e/3c87287517b9672eb7d9
on_success: change
on_failure: always
on_start: never
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## v0.0.1 - 2017-08-18
- initial release
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# squeezer-serve
Squeezer Serve Plugin . This plugin enables serving support for local development within the Squeezer Framework. Edit Add topics
Squeezer Serve Plugin . This plugin enables serving support for local development within the Squeezer Framework.

[![Build Status](https://travis-ci.org/SqueezerIO/squeezer-serve.svg?branch=master)](https://travis-ci.org/SqueezerIO/squeezer-serve)
[![npm version](https://badge.fury.io/js/squeezer-serve.svg)](https://badge.fury.io/js/squeezer-serve)
[![npm version](https://badge.fury.io/js/squeezer-aws.svg)](https://badge.fury.io/js/squeezer-aws)
[![DUB](https://img.shields.io/dub/l/vibe-d.svg)]()

### Installation

`cd PROJECT_DIR`

`npm i squeezer-serve --save`

### Activate the plugin

*PROJECT_DIR/squeezer.yml*

```yaml
plugins:
- name: squeezer-serve
path: node_modules
```
```javascript
process.on('serveEvent', (event) => {
// event.req - express request hook
// event.res - express response hook
// event.data - called function data
});
```
4 changes: 4 additions & 0 deletions hooks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- identifier : 'serve:run'
path : 'lib'
function : 'run'

11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

class ServePlugin {
constructor(sqz) {
this.sqz = sqz;

this.commands = [];
}
}

module.exports = ServePlugin;
144 changes: 144 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
'use strict';

const _ = require('lodash');
const fs = require('fs');
const colors = require('colors');
const express = require('express');
const bodyParser = require('body-parser');
const UrlPattern = require('url-pattern');
const Watcher = require('./watcher');
const Livereload = require('./livereload');

/**
* Class that serves a Squeezer project
*/
class Express {
constructor(sqz) {
this.sqz = sqz;
}

/**
* Start the Node Express server
*/
run() {
return new Promise((resolve) => {
const app = express();
const projectType = this.sqz.vars.project.type;
const projectPath = this.sqz.vars.project.path;
const project = this.sqz.vars.project;
let port = 4001;

app.use((req, res, next) => {
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, '
+ 'max-stale=0, post-check=0, pre-check=0');
req.rawBody = '';

req.setEncoding('utf8');

req.on('data', (chunk) => {
req.rawBody += chunk;
});

req.on('end', () => {
next();
});
});

app.use(bodyParser.raw());

if (projectType === 'web') {
const livereload = new Livereload(this.sqz);
livereload.init(app);
}

const watcher = new Watcher(this.sqz);
watcher.init();

app.use('/.build', express.static(`${project.buildPath}`, { fallthrough : false, maxAge : 0 }));

app.all('*', (req, res) => {
// const data = this.find(req.url, req.method);
this.find(req.url, req.method).then((data) => {
if (!data) {
res.status(400).send({
error : 'Invalid URL path : There is no any event function configured with this url path'
});
} else {
const target = `${projectPath}/.build/development/microservices/${data.microservice.identifier}`;

if (!fs.existsSync(target)) {
this.sqz.cli.log.error(`Microservice ${colors.blue.bold(data.microservice.name)} is not compiled !`);
}

// callback.call(req, res, data);
process.emit('serveEvent', {
req : req,
res : res,
data : data
});
}
});
});

const httpServer = () => {
app.listen(port, () => {
const appHost = `localhost:${port}`;
const appUrl = `http://localhost:${port}`;
process.env.APP_HOST = appHost;
process.env.APP_URL = appUrl;
this.sqz.cli.log.info(`Listening on ${colors.blue.bold(appUrl)}`);
resolve();
}).on('error', (err) => {
if (err.code === 'EADDRINUSE') {
port += 1;
httpServer();
}
});
};

httpServer();
});
}

/**
* Find a function's HTTP event that matches to the current requested URL
*
* @param url - http request url
* @param method - http method
*/
find(url, method) {
return new Promise((resolve) => {
this.sqz.lifecycle.run(['microservices:load']).then(() => {
let data = null;

_.forEach(this.sqz.vars.microservices, (microservice) => {
_.forEach(microservice.functions, (func, name) => {
func.name = name;
_.forEach(func.events, (val) => {
const type = Object.keys(val)[0];
const event = val[type];

if (type === 'http') {
const expressPathFormatted = `${event.path.replace(/{(.*?)}/g, ':$1')}`;
const pattern = new UrlPattern(expressPathFormatted);
const patternMatch = pattern.match(url.split('?')[0]);
if (patternMatch && event.method.toUpperCase() === method) {
data = {
microservice : microservice,
func : func,
event : event,
pathParameters : patternMatch
};
}
}
});
});
});

resolve(data);
});
});
}
}

module.exports = Express;
29 changes: 29 additions & 0 deletions lib/livereload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const livereload = require('livereload');
const livereloadConnect = require('connect-livereload');
const path = require('path');

/**
* Class that serves a Squeezer project
*/
class Livereload {
constructor(sqz) {
this.sqz = sqz;
}

init(app) {
const projectPath = this.sqz.vars.project.path;
const conf = this.sqz.yaml.parse(`${projectPath}/livereload.yml`, { projectPath : projectPath });
const serverConf = conf.server;
const watchConf = conf.watch.map(val => path.normalize(val));

const server = livereload.createServer(serverConf);
server.watch(watchConf);
app.use(livereloadConnect({
port : 35729
}));
}
}

module.exports = Livereload;
Loading

0 comments on commit 8482f00

Please sign in to comment.