mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* add DateTime sans mgo * change all uses of strfmt.DateTime to common.DateTime, remove test strfmt usage * remove api tests, system-test dep on api test multiple reasons to remove the api tests: * awkward dependency with fn_go meant generating bindings on a branched fn to vendor those to test new stuff. this is at a minimum not at all intuitive, worth it, nor a fun way to spend the finite amount of time we have to live. * api tests only tested a subset of functionality that the server/ api tests already test, and we risk having tests where one tests some thing and the other doesn't. let's not. we have too many test suites as it is, and these pretty much only test that we updated the fn_go bindings, which is actually a hassle as noted above and the cli will pretty quickly figure out anyway. * fn_go relies on openapi, which relies on mgo, which is deprecated and we'd like to remove as a dependency. openapi is a _huge_ dep built in a NIH fashion, that cannot simply remove the mgo dep as users may be using it. we've now stolen their date time and otherwise killed usage of it in fn core, for fn_go it still exists but that's less of a problem. * update deps removals: * easyjson * mgo * go-openapi * mapstructure * fn_go * purell * go-validator also, had to lock docker. we shouldn't use docker on master anyway, they strongly advise against that. had no luck with latest version rev, so i locked it to what we were using before. until next time. the rest is just playing dep roulette, those end up removing a ton tho * fix exec test to work * account for john le cache
195 lines
4.2 KiB
Go
195 lines
4.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Handler interface {
|
|
Serve(ctx context.Context, in io.Reader, out io.Writer)
|
|
}
|
|
|
|
// 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 {
|
|
return ctx.Value(ctxKey).(*Ctx)
|
|
}
|
|
|
|
func WithContext(ctx context.Context, fnctx *Ctx) context.Context {
|
|
return context.WithValue(ctx, ctxKey, fnctx)
|
|
}
|
|
|
|
// Ctx provides access to Config and Headers from fn.
|
|
type Ctx struct {
|
|
Header http.Header
|
|
Config map[string]string
|
|
RequestURL string
|
|
Method string
|
|
}
|
|
|
|
type key struct{}
|
|
|
|
var ctxKey = new(key)
|
|
|
|
func Do(handler Handler, format string, in io.Reader, out io.Writer) {
|
|
ctx := BuildCtx()
|
|
switch format {
|
|
case "http":
|
|
DoHTTP(handler, ctx, in, out)
|
|
case "json":
|
|
DoJSON(handler, ctx, in, out)
|
|
case "cloudevent":
|
|
DoCloudEvent(handler, ctx, in, out)
|
|
case "default":
|
|
DoDefault(handler, ctx, in, out)
|
|
default:
|
|
panic("unknown format (fdk-go): " + format)
|
|
}
|
|
}
|
|
|
|
// doDefault only runs once, since it is a 'cold' function
|
|
func DoDefault(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
|
|
SetHeaders(ctx, BuildHeadersFromEnv())
|
|
fnDeadline, _ := os.LookupEnv("FN_DEADLINE")
|
|
|
|
ctx, cancel := CtxWithDeadline(ctx, fnDeadline)
|
|
defer cancel()
|
|
|
|
handler.Serve(ctx, in, out)
|
|
}
|
|
|
|
// doHTTP runs a loop, reading http requests from in and writing
|
|
// http responses to out
|
|
func DoHTTP(handler Handler, ctx context.Context, in io.Reader, out io.Writer) {
|
|
var buf bytes.Buffer
|
|
// maps don't get down-sized, so we can reuse this as it's likely that the
|
|
// user sends in the same amount of headers over and over (but still clear
|
|
// b/w runs) -- buf uses same principle
|
|
hdr := make(http.Header)
|
|
|
|
for {
|
|
err := DoHTTPOnce(handler, ctx, in, out, &buf, hdr)
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func CtxWithDeadline(ctx context.Context, fnDeadline string) (context.Context, context.CancelFunc) {
|
|
t, err := time.Parse(time.RFC3339, fnDeadline)
|
|
if err == nil {
|
|
return context.WithDeadline(ctx, t)
|
|
}
|
|
return context.WithCancel(ctx)
|
|
}
|
|
|
|
func ResetHeaders(m http.Header) {
|
|
for k := range m { // compiler optimizes this to 1 instruction now
|
|
delete(m, k)
|
|
}
|
|
}
|
|
|
|
// response is a general purpose response struct any format can use to record
|
|
// user's code responses before formatting them appropriately.
|
|
type Response struct {
|
|
Status int
|
|
Header http.Header
|
|
|
|
io.Writer
|
|
}
|
|
|
|
var (
|
|
BaseHeaders = map[string]struct{}{
|
|
`FN_APP_NAME`: struct{}{},
|
|
`FN_PATH`: struct{}{},
|
|
`FN_METHOD`: struct{}{},
|
|
`FN_FORMAT`: struct{}{},
|
|
`FN_MEMORY`: struct{}{},
|
|
`FN_TYPE`: struct{}{},
|
|
}
|
|
|
|
HeaderPrefix = `FN_HEADER_`
|
|
|
|
ExactHeaders = map[string]struct{}{
|
|
`FN_CALL_ID`: struct{}{},
|
|
`FN_METHOD`: struct{}{},
|
|
`FN_REQUEST_URL`: struct{}{},
|
|
}
|
|
)
|
|
|
|
func SetHeaders(ctx context.Context, hdr http.Header) {
|
|
fctx := ctx.Value(ctxKey).(*Ctx)
|
|
fctx.Header = hdr
|
|
}
|
|
|
|
func SetRequestURL(ctx context.Context, requestURL string) {
|
|
fctx := ctx.Value(ctxKey).(*Ctx)
|
|
fctx.RequestURL = requestURL
|
|
}
|
|
|
|
func SetMethod(ctx context.Context, method string) {
|
|
fctx := ctx.Value(ctxKey).(*Ctx)
|
|
fctx.Method = method
|
|
}
|
|
|
|
func BuildCtx() context.Context {
|
|
ctx := &Ctx{
|
|
Config: BuildConfig(),
|
|
// allow caller to build headers separately (to avoid map alloc)
|
|
}
|
|
|
|
return WithContext(context.Background(), ctx)
|
|
}
|
|
|
|
func BuildConfig() map[string]string {
|
|
cfg := make(map[string]string, len(BaseHeaders))
|
|
|
|
for _, e := range os.Environ() {
|
|
vs := strings.SplitN(e, "=", 2)
|
|
if len(vs) < 2 {
|
|
vs = append(vs, "")
|
|
}
|
|
cfg[vs[0]] = vs[1]
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func BuildHeadersFromEnv() http.Header {
|
|
env := os.Environ()
|
|
hdr := make(http.Header, len(env)-len(BaseHeaders))
|
|
|
|
for _, e := range env {
|
|
vs := strings.SplitN(e, "=", 2)
|
|
hdrKey := IsHeaderKey(vs[0])
|
|
if hdrKey == "" {
|
|
continue
|
|
}
|
|
if len(vs) < 2 {
|
|
vs = append(vs, "")
|
|
}
|
|
// rebuild these as 'http' headers
|
|
vs = strings.Split(vs[1], ", ")
|
|
for _, v := range vs {
|
|
hdr.Add(hdrKey, v)
|
|
}
|
|
}
|
|
return hdr
|
|
}
|
|
|
|
// for getting headers out of env
|
|
func IsHeaderKey(key string) string {
|
|
if strings.HasPrefix(key, HeaderPrefix) {
|
|
return strings.TrimPrefix(key, HeaderPrefix)
|
|
}
|
|
_, ok := ExactHeaders[key]
|
|
if ok {
|
|
return key
|
|
}
|
|
return ""
|
|
}
|