-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunner.go
38 lines (30 loc) · 880 Bytes
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package fdk
import (
"context"
"fmt"
"log/slog"
"os"
)
// Runner defines the runtime that executes the request/response handler lifecycle.
type Runner func(ctx context.Context, newHandlerFn func(context.Context, *slog.Logger) Handler)
// RegisterRunner registers a runner.
func RegisterRunner(runnerType string, r Runner) {
if _, ok := runners[runnerType]; ok {
panic(fmt.Sprintf("runner type already exists: %q", runnerType))
}
runners[runnerType] = r
}
func run(ctx context.Context, newHandlerFn func(context.Context, *slog.Logger) Handler) {
rt := os.Getenv("CS_RUNNER_TYPE")
if rt == "" {
rt = "http"
}
r := runners[rt]
if r == nil {
panic(fmt.Sprintf("invalid RUNNER_TYPE provided: %q", rt))
}
r(ctx, newHandlerFn)
}
var runners = map[string]func(ctx context.Context, newHandlerFn func(context.Context, *slog.Logger) Handler){
"http": runHTTP,
}