github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/docs/function-format.md (about)

     1  # Open Function Format
     2  
     3  This document will describe the details of how a function works, inputs/outputs, etc.
     4  
     5  ## Formats
     6  
     7  ### STDIN and Environment Variables
     8  
     9  While wanting to keep things simple, flexible and expandable, we decided to go back to the basics, using Unix input and output. Standard in is easy to use in any language and doesn't require anything extra. It also allows streaming input so we can do things like keeping a container running some time and stream requests into the container.
    10  
    11  Configuration values, environment information and other things will be passed in through environment variables.
    12  
    13  The goals of the input format are the following:
    14  
    15  * Very easy to use and parse
    16  * Streamable for increasing performance (more than one call per container execution)
    17  * Ability to build higher level abstractions on top (ie: Lambda syntax compatible)
    18  
    19  The format is still up for discussion and in order to move forward and remain flexible, it's likely we will just allow different input formats and the function creator can decide what they want, on a per function basis. Default being the simplest format to use.
    20  
    21  #### Default I/O Format
    22  
    23  The default I/O format is simply the request body itself plus some environment variables. For instance, if someone were to post a JSON body, the unmodified body would be sent in via STDIN. The result comes via STDOUT. When task is done, pipes are closed and the container running the function is terminated.
    24  
    25  Pros:
    26  
    27  * Very simple to use
    28  
    29  Cons:
    30  
    31  * Not streamable
    32  
    33  #### HTTP I/O Format
    34  
    35  `--format http`
    36  
    37  HTTP format could be a good option as it is in very common use obviously, most languages have some semi-easy way to parse it, and it's streamable. The response will look like a HTTP response. The communication is still done via stdin/stdout, but these pipes are never closed unless the container is explicitly terminated. The basic format is:
    38  
    39  Request:
    40  ```
    41  GET / HTTP/1.1
    42  Content-Length: 5
    43  
    44  world
    45  ```
    46  
    47  Response:
    48  ```
    49  HTTP/1.1 200 OK
    50  Content-Length: 11
    51  
    52  hello world
    53  ```
    54  
    55  The header keys and values would be populated with information about the function call such as the request URL and query parameters.
    56  
    57  `Content-Length` is determined by the [Content-Length](https://tools.ietf.org/html/rfc7230#section-3.3.3) header, which is mandatory both for input and output. It is used by IronFunctions to know when stop writing to STDIN and reading from STDOUT.
    58  
    59  Pros:
    60  
    61  * Streamable
    62  * Common format
    63  
    64  Cons:
    65  
    66  * Requires a parsing library or fair amount of code to parse headers properly
    67  * Double parsing - headers + body (if body is to be parsed, such as json)
    68  
    69  #### JSON I/O Format (not implemented)
    70  
    71  `--format json`
    72  
    73  The idea here is to keep the HTTP base structure, but make it a bit easier to parse by making the `request line` and `headers` a JSON struct.
    74  Eg:
    75  
    76  ```
    77  {
    78    "request_url":"http://....",
    79    "params": {
    80      "blog_name": "yeezy"
    81    }
    82  }
    83  BLANK LINE
    84  BODY
    85  ```
    86  
    87  Pros:
    88  
    89  * Streamable
    90  * Easy to parse headers
    91  
    92  Cons:
    93  
    94  * New, unknown format
    95  
    96  ### STDERR
    97  
    98  Standard error is reserved for logging, like it was meant to be. Anything you output to STDERR will show up in the logs. And if you use a log
    99  collector like logspout, you can collect those logs in a central location. See [logging](logging.md).