This will give you the basic overview of writing base level functions. You can also use higher level abstractions that make it easier such as lambda.
Also, for complete examples in various languages, see the examples directory. We have language libraries for Go, Javascript and Ruby.
The most basic code layout in any language is as follows, this is pseudo code and is not meant to run.
# Read and parse from STDIN
body = JSON.parse(STDIN)
# Do something
return_struct = doSomething(body)
# Respond if sync:
STDOUT.write(JSON.generate(return_struct))
# or update something if async
db.update(return_struct)
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. To read in the function body, just read from STDIN.
You will also have access to a set of environment variables.
- REQUEST_URL - the full URL for the request
- ROUTE - the matched route
- METHOD - the HTTP method for the request
- 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.
- 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
Warning: these may change before release.
Standard out is where you should write response data for synchronous functions. Standard error is where you should write for logging, as it was intended.
So to write output to logs, simply log to STDERR. Here are some examples in a few languages.
In Go, simply use the log package, it writes to STDERR by default.
log.Println("hi")
In Node.js:
console.error("hi");
More details for Node.js here.
In Ruby:
STDERR.puts("hi")