Skip to content

Commit

Permalink
Merge branch 'support_proxy'
Browse files Browse the repository at this point in the history
  • Loading branch information
u-minor committed Nov 17, 2016
2 parents d50709c + f488af2 commit ae68fe8
Show file tree
Hide file tree
Showing 31 changed files with 460 additions and 2,498 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "0.12"
- "4.4"
- "5.11"
- "6.1"
Expand Down
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Aglex is a support tool for building serverless web applications using [Amazon A
[Express] is the most famous web framework for [Node.js].
Now you can use the same way to develop your API Gateway-Lambda web app.

**UPDATE**: now 2.x uses [aws-serverless-express] and new config yaml.


## Installation

Global install
Expand All @@ -27,11 +30,12 @@ or install and add to current package.
$ npm install aglex --save-dev
```


## Features

Aglex is not a web framework, just a small CLI tool which provides following features.

- Generate a small handler code for lambda which fake http request object and launch express app
- Generate a small lambda handler code
- Create, update lambda function
- Add execute-api permission to the function
- Create, update and deploy API
Expand Down Expand Up @@ -82,30 +86,22 @@ Aglex is not a web framework, just a small CLI tool which provides following fea
## API Gateway configuration
apiGateway:
- name: YOUR_API_NAME
- description: YOUR_API_DESCRIPTION
+ name: myapp
+ description: myapp
## Method template definitions for each httpMethod
methodDefinitions:
@@ -51,10 +51,5 @@
## API resources
resources:
- /path/to/static/endpoint:
+ /users:
- GET
- /path/to/dynamic/endpoint/{param}:
- - GET
- - PUT
- - OPTIONS
swagger: 2.0
info:
- title: YOUR_API_NAME
- description: YOUR_API_DESCRIPTION
+ title: myapp
+ description: myapp
basePath: /prod
schemes:
- https
```

4. Generate small lambda handler code
4. Generate lambda handler code and install [aws-serverless-express]

```bash
$ aglex generate lambda-handler > lambda.js
$ npm install -S aws-serverless-express
```

5. Create lambda zip
Expand Down Expand Up @@ -146,7 +142,7 @@ For more information, please see wiki docs.

## See Also

* [aws-serverless-express](https://github.com/awslabs/aws-serverless-express) is developed by AWS to make it easy to run Express apps on Lambda.
* [aws-serverless-express] is developed by AWS to make it easy to run Express apps on Lambda.

[aglex-img]: https://raw.githubusercontent.com/u-minor/aglex/master/logo.png
[npm-img]: https://img.shields.io/npm/v/aglex.svg
Expand All @@ -163,3 +159,4 @@ For more information, please see wiki docs.
[AWS Lambda]: https://aws.amazon.com/lambda/
[Express]: http://expressjs.com/
[Node.js]: https://nodejs.org/
[aws-serverless-express]: https://github.com/awslabs/aws-serverless-express
67 changes: 34 additions & 33 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,37 @@ lambda:

## API Gateway configuration
apiGateway:
name: YOUR_API_NAME
description: YOUR_API_DESCRIPTION

## Method template definitions for each httpMethod
methodDefinitions:
_DEFAULT_:
request:
type: Lambda
responses:
200:
responseHeaders: &defaultHeaders
Access-Control-Allow-Origin: "'*'"
OPTIONS:
request:
type: MOCK
requestTemplates:
application/json: '{"statusCode": 200}'
responses:
200:
responseHeaders:
<<: *defaultHeaders
Access-Control-Max-Age: "'86400'"
Access-Control-Allow-Headers: "'Origin, Authorization, Accept, Content-Type'"
Access-Control-Allow-Methods: "'POST, GET, PUT, DELETE, OPTIONS'"

## API resources
resources:
/path/to/static/endpoint:
- GET
/path/to/dynamic/endpoint/{param}:
- GET
- PUT
- OPTIONS
swagger: 2.0
info:
title: YOUR_API_NAME
description: YOUR_API_DESCRIPTION
basePath: /prod
schemes:
- https
paths:
/{proxy+}:
x-amazon-apigateway-any-method:
produces:
- application/json
parameters:
- name: proxy
in: path
required: true
type: string
responses:
200:
description: 200 response
schema:
$ref: "#/definitions/Empty"
x-amazon-apigateway-integration:
httpMethod: POST
passthroughBehavior: when_no_match
responses:
default:
statusCode: 200
type: aws_proxy
uri: REPLACED_AUTOMATICALLY_WITH_YOUR_LAMBDA_INVOCATION_ARN
definitions:
Empty:
title: Empty Schema
type: object
46 changes: 0 additions & 46 deletions config/lambda.coffee

This file was deleted.

7 changes: 7 additions & 0 deletions config/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
23 changes: 0 additions & 23 deletions config/requestTemplate.txt

This file was deleted.

149 changes: 76 additions & 73 deletions examples/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,82 @@
'use strict';

// See gulp help for overview of commands.
// Requires added the following modules to your dependencies:
// del, aglex, yargs, gulp-help, run-sequence, config, gulp-rename, gulp-zip, gulp-live-server

var fs = require('fs');
var del = require('del');
var aglex = require('aglex');
var argv = require('yargs')
.default('env','development')
.argv;

var gulp = require('gulp-help')(require('gulp'));
var runSequence = require('run-sequence');

process.env.NODE_ENV = argv.env;

// Load config from config/
var config = require('config');

var sourceFiles = ['app.js', 'config/*', 'public/**', 'routes/**'];

gulp.task('serve', 'Start a local development server', () => {
var gls = require('gulp-live-server');
var server = gls.new('bin/www', {env: {NODE_ENV: 'staging'}});
server.start();

gulp.watch(sourceFiles, () => {
console.log("restart server");
server.start.bind(server)();
});
});

gulp.task('build', 'clean and updateLambda', () => runSequence('clean','updateLambda'))

gulp.task('clean', 'remove the build directory', (done) => del('build', done));

gulp.task('copyPackages', 'copy dependences from node_modules into build directory', () => {
var pkg = require('./package.json');
var depsRe = Object.keys(pkg.dependencies).join('|');

gulp.src("node_modules/@("+depsRe+")/**")
.pipe(gulp.dest('build/node_modules'));
});

gulp.task('copyConfig', 'copy active config into build directory', () => {
var rename = require('gulp-rename');

gulp.src("config/production.yml")
.pipe(rename('default.yml'))
.pipe(gulp.dest('build/config'));
});

gulp.task('zip', 'create zipfile to upload', ['copyConfig','copyPackages'], () => {
var zip = require('gulp-zip');

'use strict'
const fs = require('fs')
const gulp = require('gulp')
const $ = require('gulp-load-plugins')()
const argv = require('yargs').default('env','development').argv
const yaml = require('js-yaml')

const aglex = (() => {
const aglexConfig = yaml.safeLoad(fs.readFileSync(`aglex-${argv.env}.yml`, 'utf8'))
return require('aglex')(aglexConfig, 'info')
})()

const sourceFiles = ['src/**/*.js']

gulp.task('serve', () => {
const server = $.liveServer('src/www.js', {env: {NODE_ENV: argv.env}}, false)
server.start()
return gulp.watch(sourceFiles, () => {
console.log('restart server')
return server.start.bind(server)()
})
})

gulp.task('build', () =>
require('run-sequence')('clean', 'zip')
)

gulp.task('clean', done =>
require('del')(['build/*', '!build/node_modules', 'dist/*'], done)
)

gulp.task('lint', () =>
gulp.src(sourceFiles)
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failAfterError())
)

gulp.task('prepareCode', ['lint'], () =>
gulp.src(sourceFiles)
.pipe(gulp.dest('build'))
)

gulp.task('prepareConfig', () =>
gulp.src(`config/${argv.env}.yml`)
.pipe($.rename('default.yml'))
.pipe(gulp.dest('build/config'))
)

gulp.task('preparePackages', () =>
gulp.src(['./package.json', './npm-shrinkwrap.json'])
.pipe(gulp.dest('build'))
.pipe($.install({production: true}))
)

gulp.task('zip', ['prepareCode', 'prepareConfig', 'preparePackages'], () =>
gulp.src('build/**')
.pipe(zip('lambda.zip'))
.pipe(gulp.dest('dist'));
});
.pipe($.zip('lambda.zip'))
.pipe(gulp.dest('dist'))
)

gulp.task('updateLambda', 'create zip file and update Lambda', ['zip'], (done) => {
aglex.updateLambda('dist/lambda.zip').then(done);
});
gulp.task('updateLambda', ['zip'], () => aglex.updateLambda('dist/lambda.zip'))

gulp.task('updateLambdaPermission', 'update Lambda permissions', (done) => {
aglex.addLambdaPermission().then(done);
});
gulp.task('addLambdaPermission', () => aglex.addLambdaPermission())

// Requires --stage, --desc and --stagedesc are optional
gulp.task('deployApi', 'deploy the API. requires --stage. --desc and --stagedesc optional.', (done) => {
gulp.task('updateApi', () => aglex.updateApi())

gulp.task('deployApi', () => {
if (!argv.stage) {
console.log("Please use --stage STAGENAME");
return;
console.log('Please use --stage STAGENAME')
return
}

aglex.deployApi(argv.desc, argv.stage, argv.stagedesc).then(done);
});
return aglex.deployApi(argv.desc, argv.stage, argv.stagedesc)
})

gulp.task('listStages', () =>
aglex.getApiStages().then(stages => {
for (let stage of stages) {
console.log(`${stage.stageName}:${stage.description || ''} (${stage.invokeUrl})`)
}
})
)
Loading

0 comments on commit ae68fe8

Please sign in to comment.