Moving out Environments to own repo (#1810)
Moved out environments and examples out of main Fission repo to their own repo. This is to allow changes to environment releases to be independent of main Fission releases.
This commit is contained in:
@@ -1,131 +0,0 @@
|
||||
# Fission Node.js Examples
|
||||
|
||||
This is V2 example, check [here](README_V1.md) for V1.
|
||||
|
||||
This directory contains several examples to get you started using Node.js with Fission.
|
||||
|
||||
Before running any of these functions, make sure you have created a `nodejs` Fission environment:
|
||||
|
||||
```bash
|
||||
# Create an environment with default nodejs images
|
||||
$ fission env create --name nodeenv --image fission/node-env:latest --builder fission/node-builder:latest
|
||||
# Create zip file from our example
|
||||
$ zip -jr nodejs.zip nodejs/
|
||||
# Create a package with the zip file
|
||||
$ fission pkg create --sourcearchive nodejs.zip --env nodeenv
|
||||
```
|
||||
|
||||
## Function signature
|
||||
|
||||
Every Node.js function has the same basic form:
|
||||
|
||||
```javascript
|
||||
module.exports = async function(context) {
|
||||
return {
|
||||
status: 200,
|
||||
body: 'Your body here',
|
||||
headers: {
|
||||
'Foo': 'Bar'
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
## hello.js
|
||||
|
||||
This is a basic "Hello, World!" example. It simply returns a status of `200` and text body.
|
||||
|
||||
### Usage
|
||||
Since it is an `async` function, you can `await` `Promise`s, as demonstrated in the `weather.js` function.
|
||||
|
||||
```bash
|
||||
# Create a function
|
||||
$ fission fn create --name hello --pkg [pkgname] --entrypoint "hello"
|
||||
|
||||
# Test the function
|
||||
$ fission fn test --name hello
|
||||
```
|
||||
|
||||
## index.js
|
||||
|
||||
This file does nothing but for demonstrating `require` feature.
|
||||
|
||||
### Usage
|
||||
```bash
|
||||
# Create a function, you can skip `--entrypoint` as node will look for `index.js` by default
|
||||
$ fission fn create --name index --pkg [pkgname]
|
||||
|
||||
# Test the function
|
||||
$ fission fn test --name index
|
||||
```
|
||||
|
||||
## multi-entry.js
|
||||
|
||||
This is a multiple exports example. There are two exports: entry1 and entry2
|
||||
|
||||
### Usage
|
||||
```bash
|
||||
# Create a function for entry1
|
||||
$ fission fn create --name entry1 --pkg [pkgname] --entrypoint "multi-entry.entry1"
|
||||
|
||||
# Test the function
|
||||
$ fission fn test --name entry1
|
||||
|
||||
# Create a function for entry2
|
||||
$ fission fn create --name entry2 --pkg [pkgname] --entrypoint "multi-entry.entry2"
|
||||
|
||||
# Test the function
|
||||
$ fission fn test --name entry2
|
||||
```
|
||||
|
||||
## hello-callback.js
|
||||
|
||||
This is a basic "Hello, World!" example implemented with the legacy callback implementation. If you declare your function with two arguments (`context`, `callback`), a callback taking three arguments (`status`, `body`, `headers`) is provided.
|
||||
|
||||
⚠️️ Callback support is only provided for backwards compatibility! We recommend that you use `async` functions instead.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Create a function
|
||||
$ fission fn create --name hello-callback --pkg [pkgname] --entrypoint "hello-callback"
|
||||
|
||||
# Map GET /hello-callback to your new function
|
||||
$ fission route create --method GET --url /hello-callback --function hello-callback
|
||||
|
||||
# Run the function.
|
||||
$ curl http://$FISSION_ROUTER/hello-callback
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
## kubeEventsSlack.js
|
||||
|
||||
This example watches Kubernetes events and sends them to a Slack channel. To use this, create an incoming webhook for your Slack channel, and replace the `slackWebhookPath` in the example code.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission fn create --name kubeEventsSlack --pkg [pkgname] --entrypoint "hello-callback"
|
||||
|
||||
# Watch all services in the default namespace:
|
||||
$ fission watch create --function kubeEventsSlack --type service --ns default
|
||||
```
|
||||
|
||||
## weather.js
|
||||
|
||||
In this example, the Yahoo Weather API is used to current weather at a given location.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission function create --name weather --pkg [pkgname] --entrypoint "weather"
|
||||
|
||||
# Map GET /stock to your new function
|
||||
$ fission route create --method POST --url /weather --function weather
|
||||
|
||||
# Run the function.
|
||||
$ curl -H "Content-Type: application/json" -X POST -d '{"location":"Sieteiglesias, Spain"}' http://$FISSION_ROUTER/weather
|
||||
|
||||
{"text":"It is 2 celsius degrees in Sieteiglesias, Spain and Mostly Clear"}
|
||||
```
|
||||
@@ -1,105 +0,0 @@
|
||||
# Fission Node.js Examples
|
||||
|
||||
This directory contains several examples to get you started using Node.js with Fission.
|
||||
|
||||
## Environment
|
||||
|
||||
Before running any of these functions, make sure you have created a `nodejs` Fission environment:
|
||||
|
||||
```
|
||||
$ fission env create --name nodejs --image fission/node-env
|
||||
```
|
||||
|
||||
Note: The default `fission/node-env` image is based on Alpine, which is much smaller than the main Debian Node image (65MB vs 680MB) while still being suitable for most use cases.
|
||||
If you need to use the full Debian image use the `fission/node-env-debian` image instead.
|
||||
See the [official Node docker hub repo](https://hub.docker.com/_/node/) for considerations
|
||||
relating to this choice.
|
||||
|
||||
## Function signature
|
||||
|
||||
Every Node.js function has the same basic form:
|
||||
|
||||
```javascript
|
||||
module.exports = async function(context) {
|
||||
return {
|
||||
status: 200,
|
||||
body: 'Your body here',
|
||||
headers: {
|
||||
'Foo': 'Bar'
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Since it is an `async` function, you can `await` `Promise`s, as demonstrated in the `weather.js` function.
|
||||
|
||||
## hello.js
|
||||
|
||||
This is a basic "Hello, World!" example. It simply returns a status of `200` and text body.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission function create --name hello --env nodejs --code hello.js
|
||||
|
||||
# Map GET /hello to your new function
|
||||
$ fission route create --method GET --url /hello --function hello
|
||||
|
||||
# Run the function.
|
||||
$ curl http://$FISSION_ROUTER/hello
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
## hello-callback.js
|
||||
|
||||
This is a basic "Hello, World!" example implemented with the legacy callback implementation. If you declare your function with two arguments (`context`, `callback`), a callback taking three arguments (`status`, `body`, `headers`) is provided.
|
||||
|
||||
⚠️️ Callback support is only provided for backwards compatibility! We recommend that you use `async` functions instead.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission function create --name hello-callback --env nodejs --code hello-callback.js
|
||||
|
||||
# Map GET /hello-callback to your new function
|
||||
$ fission route create --method GET --url /hello-callback --function hello-callback
|
||||
|
||||
# Run the function.
|
||||
$ curl http://$FISSION_ROUTER/hello-callback
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
## kubeEventsSlack.js
|
||||
|
||||
This example watches Kubernetes events and sends them to a Slack channel. To use this, create an incoming webhook for your Slack channel, and replace the `slackWebhookPath` in the example code.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission fn create --name kubeEventsSlack --env nodejs --code kubeEventsSlack.js
|
||||
|
||||
# Watch all services in the default namespace:
|
||||
$ fission watch create --function kubeEventsSlack --type service --ns default
|
||||
```
|
||||
|
||||
## weather.js
|
||||
|
||||
In this example, the Yahoo Weather API is used to current weather at a given location.
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Upload your function code to fission
|
||||
$ fission function create --name weather --env nodejs --code weather.js
|
||||
|
||||
# Map GET /stock to your new function
|
||||
$ fission route create --method POST --url /weather --function weather
|
||||
|
||||
# Run the function.
|
||||
$ curl -H "Content-Type: application/json" -X POST -d '{"location":"Sieteiglesias, Spain"}' http://$FISSION_ROUTER/weather
|
||||
|
||||
{"text":"It is 2 celsius degrees in Sieteiglesias, Spain and Mostly Clear"}
|
||||
```
|
||||
@@ -1,4 +0,0 @@
|
||||
|
||||
module.exports = function(context, callback) {
|
||||
callback(200, "Hello, world callback!\n");
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
|
||||
module.exports = async function(context) {
|
||||
return {
|
||||
status: 200,
|
||||
body: "hello, world!\n"
|
||||
};
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = require('./hello');
|
||||
@@ -1,67 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
//
|
||||
// Watch kubernetes events and send them to a slack channel. This
|
||||
// uses Slack's incoming webhooks. To use this, create an incoming
|
||||
// webhook for your slack channel through Slack's UI, and populate the
|
||||
// relative path below.
|
||||
//
|
||||
// Create the function in fission:
|
||||
//
|
||||
// fission fn create --name kubeEventsSlack --env nodejs --code kubeEventsSlack.js
|
||||
//
|
||||
// Then, watch all services in the default namespace:
|
||||
//
|
||||
// fission watch create --function kubeEventsSlack --type service --ns default
|
||||
//
|
||||
|
||||
let https = require('https');
|
||||
|
||||
const slackWebhookPath = "YOUR RELATIVE PATH HERE"; // Something like "/services/XXX/YYY/zZz123"
|
||||
|
||||
function upcaseFirst(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
|
||||
}
|
||||
|
||||
async function sendSlackMessage(msg) {
|
||||
let postData = `{"text": "${msg}"}`;
|
||||
let options = {
|
||||
hostname: "hooks.slack.com",
|
||||
path: slackWebhookPath,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
let req = https.request(options, function(res) {
|
||||
console.log(`slack request status = ${res.statusCode}`);
|
||||
return resolve();
|
||||
});
|
||||
req.write(postData);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = async function(context) {
|
||||
console.log(context.request.headers);
|
||||
|
||||
let obj = context.request.body;
|
||||
let version = obj.metadata.resourceVersion;
|
||||
let eventType = context.request.get('X-Kubernetes-Event-Type');
|
||||
let objType = context.request.get('X-Kubernetes-Object-Type');
|
||||
|
||||
let msg = `${upcaseFirst(eventType)} ${objType} ${obj.metadata.name}`;
|
||||
console.log(msg, version);
|
||||
|
||||
if (eventType == 'DELETED' || eventType == 'ADDED') {
|
||||
console.log("sending event to slack")
|
||||
await sendSlackMessage(msg);
|
||||
}
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: ""
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
module.exports.entry1 = async function(context) {
|
||||
return {
|
||||
status: 200,
|
||||
body: "Hello, entry 1!\n"
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.entry2 = async function(context) {
|
||||
return {
|
||||
status: 200,
|
||||
body: "Hello, entry 2!\n"
|
||||
};
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
{
|
||||
"name": "fission-nodejs-example",
|
||||
"version": "0.1.0",
|
||||
"author": "Soam Vasani",
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Soam Vasani",
|
||||
"email": "soamvasani+1@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Gary Yeap",
|
||||
"email": "contact@garyyeap.com"
|
||||
}
|
||||
],
|
||||
"description": "Nodejs example for Fission framework",
|
||||
"engines": {
|
||||
"node": ">=7.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": "*",
|
||||
"co": "~4.6.0",
|
||||
"express": "*",
|
||||
"minimist": "*",
|
||||
"morgan": "*",
|
||||
"mz": "~2.7.0",
|
||||
"request": "^2.81.0",
|
||||
"request-promise-native": "^1.0.3",
|
||||
"underscore": ">=1.8.3"
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const rp = require('request-promise-native');
|
||||
|
||||
module.exports = async function (context) {
|
||||
const stringBody = JSON.stringify(context.request.body);
|
||||
const body = JSON.parse(stringBody);
|
||||
const location = body.location;
|
||||
|
||||
if (!location) {
|
||||
return {
|
||||
status: 400,
|
||||
body: {
|
||||
text: 'You must provide a location.'
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await rp(`https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="${location}") and u="c"&format=json`);
|
||||
const condition = JSON.parse(response).query.results.channel.item.condition;
|
||||
const text = condition.text;
|
||||
const temperature = condition.temp;
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
text: `It is ${temperature} celsius degrees in ${location} and ${text}`
|
||||
},
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return {
|
||||
status: 500,
|
||||
body: e
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user