fdk

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2019 License: Apache-2.0 Imports: 16 Imported by: 52

README

GoDoc

Go FDK Documentation

This is documentation for the Go function development kit (FDK) which provides convenience functions for writing Go Fn code.

For getting started using the Go FDK , see the tutorial here.

To get the Go FDK simply import it in your program github.com/fnproject/fdk-go, and use go get to get it.

User Information

Go FDK Development

See CONTRIBUTING for information on contributing to the project.

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)
}

Index

Examples

Constants

View Source
const Version = "0.0.1"

Version is the FDK version

Variables

This section is empty.

Functions

func AddHeader

func AddHeader(out io.Writer, key, value string)

AddHeader will add a header onto the function response

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

func HandleContext(ctx context.Context, handler Handler)

HandleContext works the same as Handle, but takes a context that will exit the handler loop when canceled/timed out.

func SetHeader

func SetHeader(out io.Writer, key, value string)

SetHeader will set a header on the function response

func WithContext

func WithContext(ctx context.Context, fnctx Context) context.Context

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

func WriteStatus(out io.Writer, status int)

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

func GetContext(ctx context.Context) Context

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

func HTTPHandler(h http.Handler) Handler

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).

type HandlerFunc

type HandlerFunc func(ctx context.Context, in io.Reader, out io.Writer)

HandlerFunc makes a Handler so that you don't have to!

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(ctx context.Context, in io.Reader, out io.Writer)

Serve implements Handler

Directories

Path Synopsis
images

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL