<commit_before>[![Build Status](https://travis-ci.org/backstage/functions.png?branch=master)](https://travis-ci.org/backstage/functions)
[![Coverage Status](https://coveralls.io/repos/github/backstage/functions/badge.svg?branch=master)](https://coveralls.io/github/backstage/functions?branch=master)


# Backstage Functions!
Beat your code for you with precision!

## Requirements

- Redis 3.0+
- NodeJS 6.6.0

## Setup

    make setup

## Run local

    make run

# Examples

## Create function

```javascript
function main(req, res) {
  const name = (req.body && req.body.name) || "World"
  res.send({ say: `Hello ${name}!` })
}
```

Send the function as curl to `/functions/:namespace/:name`

`curl -XPUT localhost:8100/functions/example/hello-world -d '{"code":"function main(req, res) {\n  const name = (req.body && req.body.name) || \"World\"\n  res.send({ say: `Hello ${name}!` })\n}\n"}' -H 'content-type: application/json'`

Run the function send a -XPUT to `/functions/:namespace/:name/run`

```bash
curl -XPUT localhost:8100/functions/example/hello-world/run  -H 'content-type: application/json'
# { "say": "Hello World" }
```

If one pass an object at the request payload with name the payload is executed

```bash
curl -XPUT localhost:8100/functions/example/hello-world/run  -H 'content-type: application/json' -d '{"name": "John"}'
# { "say": "Hello John" }
```<commit_msg>Format and more information about how to use<commit_after>[![Build Status](https://travis-ci.org/backstage/functions.png?branch=master)](https://travis-ci.org/backstage/functions)
[![Coverage Status](https://coveralls.io/repos/github/backstage/functions/badge.svg?branch=master)](https://coveralls.io/github/backstage/functions?branch=master)


# Backstage Functions

Backstage Functions is an Open Source [Serverless](http://martinfowler.com/articles/serverless.html) Plataform able to store and execute JavaScript code.

## Requirements

- Redis 3.0+
- NodeJS 6.6.0

## Setup

```bash
make setup
```

## Run local

```bash
make run
```

## How to use

### Create function

```javascript
function main(req, res) {
  const name = (req.body && req.body.name) || "World"
  res.send({ say: `Hello ${name}!` })
}
```

Send the function as curl to `/functions/:namespace/:name`

```bash
curl -i -XPUT http://localhost:8100/functions/example/hello-world \
    -H 'content-type: application/json' \
    -d '{"code":"function main(req, res) {\n  const name = (req.body && req.body.name) || \"World\"\n  res.send({ say: `Hello ${name}!` })\n}\n"}'
```

Run the function send a `PUT` request to `/functions/:namespace/:name/run`:

```bash
curl -i -H 'content-type: application/json' -XPUT http://localhost:8100/functions/example/hello-world/run
```

Results in something like:

```bash
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 22
ETag: W/"16-soBGetwJPBLt8CqWpBQu+A"
Date: Tue, 11 Oct 2016 16:51:04 GMT
Connection: keep-alive

{"say":"Hello World!"}
```

If one pass an object at the request payload with name the payload is executed

```bash
curl -i -XPUT http://localhost:8100/functions/example/hello-world/run \
    -H 'content-type: application/json' \
    -d '{"name": "Pedro"}'
```

Results in something like:

```bash
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 22
ETag: W/"16-Ino2/umXaZ3xVEhoqyS8aA"
Date: Tue, 11 Oct 2016 17:13:11 GMT
Connection: keep-alive

{"say":"Hello Pedro!"}
```
{'fraction_non_alphanumeric': 0.17611159546643418, 'fraction_numerical': 0.023830281894798022, 'mean_word_length': 4.403453689167975, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1283193', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1183, 'n_words': 300}