UNPKG

4.73 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/backstage/functions.png?branch=master)](https://travis-ci.org/backstage/functions)
2[![Coverage Status](https://coveralls.io/repos/github/backstage/functions/badge.svg?branch=master)](https://coveralls.io/github/backstage/functions?branch=master)
3
4# Backstage Functions
5Backstage Functions is an Open Source [Serverless](http://martinfowler.com/articles/serverless.html) Platform able to store and execute code.
6
7## Benefits
8- Your code will be executed in an isolated environment
9- You don't have to worry about infrastructure
10- Functions can be called at any time by any project
11
12## FAQ
13- **Which languages are supported?**
14Currently, only Javascript.
15
16- **Is it based on events?**
17Not yet.
18
19- **How the code execution happens in an isolated way?**
20It uses the [Backstage Functions Sandbox](https://github.com/backstage/functions-sandbox).
21
22## Running locally without Docker
23### Requirements
24- Redis 3.0+
25- NodeJS 8.9.1
26
27### Download the project
28```bash
29git clone https://github.com/backstage/functions.git
30```
31
32### Setup
33```bash
34make setup
35```
36
37### Run
38```bash
39make run
40```
41
42## Running locally via Docker
43### Requirements
44- Docker 1.12+
45- Docker compose 1.8+
46
47### Download docker-compose.yml
48```bash
49mkdir functions
50cd functions
51curl 'https://raw.githubusercontent.com/backstage/functions/master/docker-compose.yml' > docker-compose.yml
52```
53
54### Run
55```bash
56docker-compose up
57```
58
59## How to use
60### Creating a function
61Your function will have a file, which you define any name you want, and it has to have a function called `main`, with two parameters: `req` and `res`. Req represents the `Request` and Res represents the `Response`.
62At the end of your code, you'll have to use the `send` method.
63
64#### Example of a function
65```javascript
66function main(req, res) {
67 const name = (req.body && req.body.name) || "World"
68 res.send({ say: `Hello ${name}!` })
69}
70```
71
72To store your function, you can make a `POST` request to `/functions/:namespace/:name`:
73```bash
74curl -i -X POST http://localhost:8100/functions/example/hello-world \
75 -H 'content-type: application/json' \
76 -d '{"code":"function main(req, res) {\n const name = (req.body && req.body.name) || \"World\"\n res.send({ say: `Hello ${name}!` })\n}\n"}'
77```
78
79*ps: if already exists, it will not be updated*
80
81### Updating a function
82To update your function, you can make a `PUT` request to `/functions/:namespace/:name`:
83```bash
84curl -i -X PUT http://localhost:8100/functions/example/hello-world \
85 -H 'content-type: application/json' \
86 -d '{"code":"function main(req, res) {\n const name = (req.body && req.body.name) || \"World\"\n res.send({ say: `Hello ${name}! Nice meeting you...` })\n}\n"}'
87```
88
89*ps: if it doesn't exists, it will be created*
90
91### Deleting a function
92To delete your function, you can make a `DELETE` request to `/functions/:namespace/:name`:
93```bash
94curl -i -X DELETE http://localhost:8100/functions/example/hello-world \
95 -H 'content-type: application/json'
96```
97
98### Executing a function
99To execute a function, you can make a `PUT` request to `/functions/:namespace/:name/run`:
100```bash
101curl -i -X PUT http://localhost:8100/functions/example/hello-world/run \
102 -H 'content-type: application/json'
103```
104
105The result will be something like:
106```bash
107HTTP/1.1 200 OK
108Content-Type: application/json; charset=utf-8
109Content-Length: 22
110ETag: W/"16-soBGetwJPBLt8CqWpBQu+A"
111Date: Tue, 11 Oct 2016 16:51:04 GMT
112Connection: keep-alive
113
114{"say":"Hello World!"}
115```
116
117If one needs to pass an object in the request, the payload is executed:
118```bash
119curl -i -X PUT http://localhost:8100/functions/example/hello-world/run \
120 -H 'content-type: application/json' \
121 -d '{"name": "Pedro"}'
122```
123
124The result will be something like:
125```bash
126HTTP/1.1 200 OK
127Content-Type: application/json; charset=utf-8
128Content-Length: 22
129ETag: W/"16-Ino2/umXaZ3xVEhoqyS8aA"
130Date: Tue, 11 Oct 2016 17:13:11 GMT
131Connection: keep-alive
132
133{"say":"Hello Pedro!"}
134```
135
136### Executing functions in a pipeline
137
138To execute many functions in a pipeline, you can make a `PUT` request to `/functions/pipeline`:
139```javascript
140// Function0
141function main(req, res) {\
142 res.send({x: req.body.x * 10});
143
144}
145
146// Function1
147function main(req, res) {
148 res.send({x: req.body.x * 20});
149}
150```
151
152```
153curl -g -i -X PUT 'http://localhost:8100/functions/pipeline?steps[0]=namespace/function0&steps[1]=namespace/function1' \
154 -H 'content-type: application/json'
155 -d '{"x": 1}'
156```
157
158Considering the curl above, the pipeline result would be like this:
159
160```bash
161HTTP/1.1 200 OK
162Content-Type: application/json; charset=utf-8
163Content-Length: 22
164ETag: W/"16-Ino2/umXaZ3xVEhoqyS8aA"
165Date: Tue, 11 Oct 2016 17:13:11 GMT
166Connection: keep-alive
167
168{"x": 200}
169```