mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* update vendor directory, add go.opencensus.io * update imports * oops * s/opentracing/opencensus/ & remove prometheus / zipkin stuff & remove old stats * the dep train rides again * fix gin build * deps from last guy * start in on the agent metrics * she builds * remove tags for now, cardinality error is fussing. subscribe instead of register * update to patched version of opencensus to proceed for now TODO switch to a release * meh fix imports * println debug the bad boys * lace it with the tags * update deps again * fix all inconsistent cardinality errors * add our own logger * fix init * fix oom measure * remove bugged removal code * fix s3 measures * fix prom handler nil
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package fdk
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/fnproject/fdk-go/utils"
|
|
)
|
|
|
|
type Handler interface {
|
|
Serve(ctx context.Context, in io.Reader, out io.Writer)
|
|
}
|
|
|
|
type HandlerFunc func(ctx context.Context, in io.Reader, out io.Writer)
|
|
|
|
func (f HandlerFunc) Serve(ctx context.Context, in io.Reader, out io.Writer) {
|
|
f(ctx, in, out)
|
|
}
|
|
|
|
// Context will return an *fn.Ctx that can be used to read configuration and
|
|
// request information from an incoming request.
|
|
func Context(ctx context.Context) *Ctx {
|
|
utilsCtx := utils.Context(ctx)
|
|
return &Ctx{
|
|
Header: utilsCtx.Header,
|
|
Config: utilsCtx.Config,
|
|
}
|
|
}
|
|
|
|
func WithContext(ctx context.Context, fnctx *Ctx) context.Context {
|
|
utilsCtx := &utils.Ctx{
|
|
Header: fnctx.Header,
|
|
Config: fnctx.Config,
|
|
}
|
|
return utils.WithContext(ctx, utilsCtx)
|
|
}
|
|
|
|
// Ctx provides access to Config and Headers from fn.
|
|
type Ctx struct {
|
|
Header http.Header
|
|
Config map[string]string
|
|
}
|
|
|
|
// AddHeader will add a header on the function response, for hot function
|
|
// formats.
|
|
func AddHeader(out io.Writer, key, value string) {
|
|
if resp, ok := out.(*utils.Response); ok {
|
|
resp.Header.Add(key, value)
|
|
}
|
|
}
|
|
|
|
// SetHeader will set a header on the function response, for hot function
|
|
// formats.
|
|
func SetHeader(out io.Writer, key, value string) {
|
|
if resp, ok := out.(*utils.Response); ok {
|
|
resp.Header.Set(key, value)
|
|
}
|
|
}
|
|
|
|
// WriteStatus will set the status code to return in the function response, for
|
|
// hot function formats.
|
|
func WriteStatus(out io.Writer, status int) {
|
|
if resp, ok := out.(*utils.Response); ok {
|
|
resp.Status = status
|
|
}
|
|
}
|
|
|
|
// 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 Handle(handler Handler) {
|
|
format, _ := os.LookupEnv("FN_FORMAT")
|
|
utils.Do(handler, format, os.Stdin, os.Stdout)
|
|
}
|