forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HTTP events, schedule events and function chaining examples.
- Loading branch information
James Thomas
committed
Jan 25, 2017
1 parent
8e5ddc5
commit fe713a8
Showing
16 changed files
with
361 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Serverless Boilerplate - OpenWhisk - Node.js | ||
|
||
Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/). | ||
|
||
You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/). | ||
|
||
## 1. Install Provider Plugin | ||
`npm install -g serverless-openwhisk` | ||
|
||
## 2. Install Service Dependencies | ||
`npm install` in this directory to download the modules from `package.json`. | ||
|
||
## 3. Deploy | ||
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command | ||
|
||
Make a note of the API endpoint that is logged to the console during deployment. | ||
|
||
``` | ||
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service | ||
``` | ||
|
||
## 4. Invoke sequence function | ||
`serverless invoke --function chained_seq --data '{"message": "a b c d e"}'` | ||
|
||
`-f` is also shorthand for `--function` | ||
|
||
In your terminal window you should see the response from Apache OpenWhisk | ||
|
||
```bash | ||
{ | ||
"message": "e d c b a" | ||
} | ||
``` | ||
|
||
## 5. Invoke chained function | ||
`serverless invoke --function manual_seq --data '{"message": "a b c d e"}'` | ||
|
||
`-f` is also shorthand for `--function` | ||
|
||
In your terminal window you should see the response from Apache OpenWhisk | ||
|
||
```bash | ||
{ | ||
"message": "e d c b a" | ||
} | ||
``` | ||
|
||
**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
function chain(parameters) { | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
const ow = require('openwhisk')(); | ||
|
||
const invoke = (actionName, params) => ow.actions.invoke({ actionName, params, blocking: true }); | ||
return invoke('my_service-dev-split', parameters) | ||
.then(res => invoke('my_service-dev-reverse', res.response.result)) | ||
.then(res => invoke('my_service-dev-join', res.response.result)) | ||
.then(res => res.response.result); | ||
} | ||
|
||
module.exports.chain = chain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "openwhisk-node-chaining-functions", | ||
"version": "0.1.0", | ||
"description": "Example of chaining function calls using sequences and the sdk.", | ||
"scripts": { | ||
"postinstall": "npm link serverless-openwhisk", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Welcome to Serverless! | ||
# | ||
# This file is the main config file for your service. | ||
# It's very minimal at this point and uses default values. | ||
# You can always add more config options for more control. | ||
# We've included some commented out config examples here. | ||
# Just uncomment any of them to get that config option. | ||
# | ||
# For full config options, check the docs: | ||
# docs.serverless.com | ||
# | ||
# Happy Coding! | ||
|
||
service: my_service # NOTE: update this with your service name | ||
|
||
provider: | ||
name: openwhisk | ||
|
||
functions: | ||
split: | ||
handler: utils.split | ||
reverse: | ||
handler: utils.reverse | ||
join: | ||
handler: utils.join | ||
chained_seq: | ||
sequence: | ||
- split | ||
- reverse | ||
- join | ||
manual_seq: | ||
handler: handler.chain | ||
|
||
# remember to run npm install to download the provider plugin. | ||
plugins: | ||
- serverless-openwhisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
function split(params) { | ||
return { message: params.message.split(' ') }; | ||
} | ||
|
||
function join(params) { | ||
return { message: params.message.join(' ') }; | ||
} | ||
|
||
function reverse(params) { | ||
return { message: params.message.reverse() }; | ||
} | ||
|
||
module.exports.split = split; | ||
module.exports.join = join; | ||
module.exports.reverse = reverse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Serverless Boilerplate - OpenWhisk - Node.js | ||
|
||
Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/). | ||
|
||
You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/). | ||
|
||
## 1. Install Provider Plugin | ||
`npm install -g serverless-openwhisk` | ||
|
||
## 2. Install Service Dependencies | ||
`npm install` in this directory to download the modules from `package.json`. | ||
|
||
## 3. Deploy | ||
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command | ||
|
||
Make a note of the API endpoint that is logged to the console during deployment. | ||
|
||
``` | ||
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service | ||
``` | ||
|
||
## 4. Invoke deployed function | ||
`serverless invoke --function time` or `serverless invoke -f time` | ||
|
||
`-f` is shorthand for `--function` | ||
|
||
In your terminal window you should see the response from Apache OpenWhisk | ||
|
||
```bash | ||
{ | ||
"payload": "The time in Europe/London is: 16:01:14." | ||
} | ||
``` | ||
|
||
## 5. Test HTTP endpoint | ||
|
||
Use a HTTP client to access the endpoint for your function. The endpoint will | ||
be the API gateway root path, logged during deployment, and your configured | ||
function path. | ||
|
||
``` | ||
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time | ||
{ | ||
"payload": "The time in Europe/London is: 16:01:07." | ||
} | ||
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time?timezone=Europe/Berlin | ||
{ | ||
"payload": "The time in Europe/Berlin is: 17:01:11." | ||
} | ||
``` | ||
|
||
**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
function cron() { | ||
const time = new Date(); | ||
const name = '__OW_ACTION_NAME'; | ||
console.log(`Your cron function "${process.env[name]}" ran at ${time}`); // eslint-disable-line no-console | ||
} | ||
|
||
module.exports.cron = cron; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "openwhisk-node-scheduled-cron", | ||
"version": "0.1.0", | ||
"description": "Example of creating a function that runs as a cron job using the serverless schedule event.", | ||
"scripts": { | ||
"postinstall": "npm link serverless-openwhisk", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Welcome to Serverless! | ||
# | ||
# This file is the main config file for your service. | ||
# It's very minimal at this point and uses default values. | ||
# You can always add more config options for more control. | ||
# We've included some commented out config examples here. | ||
# Just uncomment any of them to get that config option. | ||
# | ||
# For full config options, check the docs: | ||
# docs.serverless.com | ||
# | ||
# Happy Coding! | ||
|
||
service: my_service # NOTE: update this with your service name | ||
|
||
provider: | ||
name: openwhisk | ||
|
||
functions: | ||
cron: | ||
handler: handler.cron | ||
events: | ||
- schedule: cron(* * * * *) | ||
|
||
# remember to run npm install to download the provider plugin. | ||
plugins: | ||
- serverless-openwhisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Serverless Boilerplate - OpenWhisk - Node.js | ||
|
||
Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/). | ||
|
||
You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/). | ||
|
||
## 1. Install Provider Plugin | ||
`npm install -g serverless-openwhisk` | ||
|
||
## 2. Install Service Dependencies | ||
`npm install` in this directory to download the modules from `package.json`. | ||
|
||
## 3. Deploy | ||
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command | ||
|
||
Make a note of the API endpoint that is logged to the console during deployment. | ||
|
||
``` | ||
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service | ||
``` | ||
|
||
## 4. Invoke deployed function | ||
`serverless invoke --function time` or `serverless invoke -f time` | ||
|
||
`-f` is shorthand for `--function` | ||
|
||
In your terminal window you should see the response from Apache OpenWhisk | ||
|
||
```bash | ||
{ | ||
"payload": "The time in Europe/London is: 16:01:14." | ||
} | ||
``` | ||
|
||
## 5. Test HTTP endpoint | ||
|
||
Use a HTTP client to access the endpoint for your function. The endpoint will | ||
be the API gateway root path, logged during deployment, and your configured | ||
function path. | ||
|
||
``` | ||
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time | ||
{ | ||
"payload": "The time in Europe/London is: 16:01:07." | ||
} | ||
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time?timezone=Europe/Berlin | ||
{ | ||
"payload": "The time in Europe/Berlin is: 17:01:11." | ||
} | ||
``` | ||
|
||
**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const moment = require('moment-timezone'); | ||
|
||
function time(params) { | ||
const timezone = params.timezone || 'Europe/London'; | ||
const timestr = moment().tz(timezone).format('HH:MM:ss'); | ||
|
||
return { payload: `The time in ${timezone} is: ${timestr}.` }; | ||
} | ||
|
||
module.exports.time = time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "openwhisk-node-simple-http", | ||
"version": "0.1.0", | ||
"description": "Example demonstrates how to setup a simple HTTP GET endpoint with OpenWhisk.", | ||
"scripts": { | ||
"postinstall": "npm link serverless-openwhisk", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"moment-timezone": "^0.5.11" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Welcome to Serverless! | ||
# | ||
# This file is the main config file for your service. | ||
# It's very minimal at this point and uses default values. | ||
# You can always add more config options for more control. | ||
# We've included some commented out config examples here. | ||
# Just uncomment any of them to get that config option. | ||
# | ||
# For full config options, check the docs: | ||
# docs.serverless.com | ||
# | ||
# Happy Coding! | ||
|
||
service: my_service # NOTE: update this with your service name | ||
|
||
provider: | ||
name: openwhisk | ||
|
||
functions: | ||
time: | ||
handler: handler.time | ||
events: | ||
- http: GET time | ||
|
||
# remember to run npm install to download the provider plugin. | ||
plugins: | ||
- serverless-openwhisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
# Serverless Boilerplate - OpenWhisk - Node.js | ||
|
||
A Serverless Framework Boilerplate for OpenWhisk support with Node.js | ||
Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/). | ||
|
||
**Please see [instructions](https://github.com/serverless/serverless-openwhisk) for getting starting with the OpenWhisk plugin for The Serverless Framework. The README walks you through setting up this project.** | ||
You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/). | ||
|
||
## 1. Install Provider Plugin | ||
`npm install -g serverless-openwhisk` | ||
|
||
## 2. Install Service Dependencies | ||
`npm install` in this directory to download the modules from `package.json`. | ||
|
||
## 3. Deploy | ||
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command | ||
|
||
## 4. Invoke deployed function | ||
`serverless invoke --function hello_world` or `serverless invoke -f hello_world` | ||
|
||
`-f` is shorthand for `--function` | ||
|
||
In your terminal window you should see the response from Apache OpenWhisk | ||
|
||
```bash | ||
{ | ||
"payload": "Hello, World!" | ||
} | ||
``` | ||
|
||
Congrats you have just deployed and run your Hello World function! | ||
|
||
**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).** | ||
|
||
https://github.com/serverless/serverless-openwhisk |
Oops, something went wrong.