mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Docs on writing functions and the functions format. (#203)
* WIP on writing functions and the functions format. * Added more docs on writing functions. * Running on windows docs. * Linked README to writing.md * Minor fixes from PR feedback.
This commit is contained in:
81
docs/writing.md
Normal file
81
docs/writing.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# Writing Functions
|
||||
|
||||
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](lambda.md).
|
||||
|
||||
## Code
|
||||
|
||||
The most basic code layout in any language is as follows, this is pseudo code and is not meant to run.
|
||||
|
||||
```ruby
|
||||
# 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
|
||||
|
||||
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).
|
||||
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
|
||||
* CONFIG_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.
|
||||
* 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.
|
||||
|
||||
Warning: these may change before release.
|
||||
|
||||
## Logging
|
||||
|
||||
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](http://www.jstorimer.com/blogs/workingwithcode/7766119-when-to-use-stderr-instead-of-stdout).
|
||||
|
||||
So to write output to logs, simply log to STDERR. Here are some examples in a few languages.
|
||||
|
||||
In Go, simply use the [log](https://golang.org/pkg/log/) package, it writes to STDERR by default.
|
||||
|
||||
```go
|
||||
log.Println("hi")
|
||||
```
|
||||
|
||||
In Node.js:
|
||||
|
||||
```node
|
||||
console.error("hi");
|
||||
```
|
||||
|
||||
[More details for Node.js here](http://stackoverflow.com/a/27576486/105562).
|
||||
|
||||
In Ruby:
|
||||
|
||||
```ruby
|
||||
STDERR.puts("hi")
|
||||
```
|
||||
|
||||
## Packaging
|
||||
|
||||
Packaging a function is essentially just creating a Docker image for your function with an ENTRYPOINT.
|
||||
|
||||
The basic Dockerfile for most languages is something like this:
|
||||
|
||||
```
|
||||
# Choose base image
|
||||
FROM iron/go
|
||||
# Set th working directory
|
||||
WORKDIR /function
|
||||
# Add your binary or code to the working directory
|
||||
ADD hello /function/
|
||||
# Set what will run when a container is started for this image
|
||||
ENTRYPOINT ["./hello"]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user