Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"encoding/json"
"io"
"net/http"
fdk "github.com/fnproject/fdk-go"
)
func main() {
fdk.Handle(fdk.HandlerFunc(myHandler))
}
func main() { println("use main()") }
// TODO make http.Handler example
func myHandler(ctx context.Context, in io.Reader, out io.Writer) {
fnctx, ok := fdk.GetContext(ctx).(fdk.HTTPContext)
if !ok {
// optionally, this may be a good idea
fdk.WriteStatus(out, 400)
fdk.SetHeader(out, "Content-Type", "application/json")
io.WriteString(out, `{"error":"function not invoked via http trigger"}`)
return
}
contentType := fnctx.Header().Get("Content-Type")
if contentType != "application/json" {
// can assert content type for your api this way
fdk.WriteStatus(out, 400)
fdk.SetHeader(out, "Content-Type", "application/json")
io.WriteString(out, `{"error":"invalid content type"}`)
return
}
if fnctx.RequestMethod() != "PUT" {
// can assert certain request methods for certain endpoints
fdk.WriteStatus(out, 404)
fdk.SetHeader(out, "Content-Type", "application/json")
io.WriteString(out, `{"error":"route not found, method not supported"}`)
return
}
var person struct {
Name string `json:"name"`
}
json.NewDecoder(in).Decode(&person)
// this is where you might insert person into a database or do something else
all := struct {
Name string `json:"name"`
URL string `json:"url"`
Header http.Header `json:"header"`
Config map[string]string `json:"config"`
}{
Name: person.Name,
URL: fnctx.RequestURL(),
Header: fnctx.Header(),
Config: fnctx.Config(),
}
// you can write your own headers & status, if you'd like to
fdk.SetHeader(out, "Content-Type", "application/json")
fdk.WriteStatus(out, 201)
json.NewEncoder(out).Encode(&all)
}
Output:
Index ¶
- Constants
- func AddHeader(out io.Writer, key, value string)
- func Handle(handler Handler)
- func HandleContext(ctx context.Context, handler Handler)
- func SetHeader(out io.Writer, key, value string)
- func WithContext(ctx context.Context, fnctx Context) context.Context
- func WriteStatus(out io.Writer, status int)
- type Context
- type HTTPContext
- type Handler
- type HandlerFunc
Examples ¶
Constants ¶
const Version = "0.0.1"
Version is the FDK version
Variables ¶
This section is empty.
Functions ¶
func Handle ¶
func Handle(handler Handler)
Handle will run the event loop for a function. Handle should be invoked through main() in a user's function and can handle communication between the function and fn server via any of the supported formats.
func HandleContext ¶
HandleContext works the same as Handle, but takes a context that will exit the handler loop when canceled/timed out.
func WithContext ¶
WithContext adds an fn context to a context context. It is unclear why this is an exported method but hey here ya go don't hurt yourself.
func WriteStatus ¶
WriteStatus will set the status code to return in the function response
Types ¶
type Context ¶
type Context interface {
// Config is a map of all env vars set on a function, the base set of fn
// headers in addition to any app and function configuration
Config() map[string]string
// Header are the headers sent to this function invocation
Header() http.Header
// ContentType is Header().Get("Content-Type") but with 15 less chars, you are welcome
ContentType() string
// CallID is the call id for this function invocation
CallID() string
// AppName is Config()["FN_APP_ID"]
AppID() string
// FnID is Config()["FN_FN_ID"]
FnID() string
}
Context contains all configuration for a function invocation
func GetContext ¶
GetContext will return an fdk Context that can be used to read configuration and request information from an incoming request.
type HTTPContext ¶
type HTTPContext interface {
Context
// RequestURL is the request url from the gateway client http request
RequestURL() string
// RequestMethod is the request method from the gateway client http request
RequestMethod() string
}
HTTPContext contains all configuration for a function invocation sourced from an http gateway trigger, which will make the function appear to receive from the client request they were sourced from, with no additional headers.
type Handler ¶
type Handler interface {
// Serve contains a context with request configuration, the body of the
// request as a stream of bytes, and a writer to output to; user's may set
// headers via the resp writer using the fdk's SetHeader/AddHeader methods -
// if you've a better idea, pipe up.
Serve(ctx context.Context, body io.Reader, resp io.Writer)
}
Handler is a function handler, representing 1 invocation of a function
func HTTPHandler ¶
HTTPHandler makes a Handler from an http.Handler, if the function invocation is from an http trigger the request is identical to the client request to the http gateway (sans some hop headers).