github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/docs/writing.md (about) 1 # Writing Functions 2 3 This will give you the basic overview of writing base level functions. You can also use higher level 4 abstractions that make it easier such as [lambda](lambda/README.md). 5 6 Also, for complete examples in various languages, see the [examples directory](/examples). 7 We have language libraries for [Go](https://github.com/iron-io/functions_go), [Javascript](https://github.com/iron-io/functions_js) and 8 [Ruby](https://github.com/iron-io/functions_ruby). 9 10 ## Code 11 12 The most basic code layout in any language is as follows, this is pseudo code and is not meant to run. 13 14 ```ruby 15 # Read and parse from STDIN 16 body = JSON.parse(STDIN) 17 18 # Do something 19 return_struct = doSomething(body) 20 21 # Respond if sync: 22 STDOUT.write(JSON.generate(return_struct)) 23 # or update something if async 24 db.update(return_struct) 25 ``` 26 27 ## Inputs 28 29 Inputs are provided through standard input and environment variables. We'll just talk about the default input format here, but you can find others [here](function-format.md). 30 To read in the function body, just read from STDIN. 31 32 You will also have access to a set of environment variables. 33 34 * REQUEST_URL - the full URL for the request 35 * ROUTE - the matched route 36 * METHOD - the HTTP method for the request 37 * HEADER_X - the HTTP headers that were set for this request. Replace X with the upper cased name of the header and replace dashes in the header with underscores. 38 * X - any configuration values you've set for the Application or the Route. Replace X with the upper cased name of the config variable you set. Ex: `minio_secret=secret` will be exposed via MINIO_SECRET env var 39 40 Warning: these may change before release. 41 42 ## Logging 43 44 Standard out is where you should write response data for synchronous functions. Standard error 45 is where you should write for logging, as [it was intended](http://www.jstorimer.com/blogs/workingwithcode/7766119-when-to-use-stderr-instead-of-stdout). 46 47 So to write output to logs, simply log to STDERR. Here are some examples in a few languages. 48 49 In Go, simply use the [log](https://golang.org/pkg/log/) package, it writes to STDERR by default. 50 51 ```go 52 log.Println("hi") 53 ``` 54 55 In Node.js: 56 57 ```node 58 console.error("hi"); 59 ``` 60 61 [More details for Node.js here](http://stackoverflow.com/a/27576486/105562). 62 63 In Ruby: 64 65 ```ruby 66 STDERR.puts("hi") 67 ``` 68 69 ## Next Steps 70 71 * [Packaging your function](packaging.md) 72