diff --git a/env.go b/env.go index d35f1822..4013cacd 100644 --- a/env.go +++ b/env.go @@ -36,21 +36,11 @@ func DevServerURL() string { return devServerURL } -func allowInBandSync() bool { - val := os.Getenv(envKeyAllowInBandSync) - if val == "" { - // TODO: Default to true once in-band syncing is stable - return false - } - - return isTruthy(val) -} - -func isTruthy(val string) bool { +func isTrue(val string) bool { val = strings.ToLower(val) - if val == "false" || val == "0" || val == "" { - return false + if val == "true" || val == "1" { + return true } - return true + return false } diff --git a/handler.go b/handler.go index 8658ab82..f3f524ac 100644 --- a/handler.go +++ b/handler.go @@ -95,6 +95,10 @@ type HandlerOpts struct { // UseStreaming enables streaming - continued writes to the HTTP writer. This // differs from true streaming in that we don't support server-sent events. UseStreaming bool + + // AllowInBandSync allows in-band syncs to occur. If nil, in-band syncs are + // disallowed. + AllowInBandSync *bool } // GetSigningKey returns the signing key defined within HandlerOpts, or the default @@ -142,6 +146,19 @@ func (h HandlerOpts) GetRegisterURL() string { return *h.RegisterURL } +func (h HandlerOpts) IsInBandSyncAllowed() bool { + if h.AllowInBandSync != nil { + return *h.AllowInBandSync + } + + // TODO: Default to true once in-band syncing is stable + if isTrue(os.Getenv(envKeyAllowInBandSync)) { + return true + } + + return false +} + // Handler represents a handler which serves the Inngest API via HTTP. This provides // function registration to Inngest, plus the invocation of registered functions via // an HTTP POST. @@ -274,7 +291,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // by incoming events or schedules. func (h *handler) register(w http.ResponseWriter, r *http.Request) error { var err error - if r.Header.Get(HeaderKeySyncKind) == SyncKindInBand && allowInBandSync() { + if r.Header.Get(HeaderKeySyncKind) == SyncKindInBand && h.IsInBandSyncAllowed() { err = h.inBandSync(w, r) } else { err = h.outOfBandSync(w, r) diff --git a/handler_test.go b/handler_test.go index 8c37807f..1cc6b42b 100644 --- a/handler_test.go +++ b/handler_test.go @@ -555,9 +555,6 @@ func TestIntrospection(t *testing.T) { } func TestInBandSync(t *testing.T) { - os.Setenv(envKeyAllowInBandSync, "1") - defer os.Unsetenv(envKeyAllowInBandSync) - appID := "test-in-band-sync" fn := CreateFunction( @@ -568,7 +565,8 @@ func TestInBandSync(t *testing.T) { }, ) h := NewHandler(appID, HandlerOpts{ - Env: toPtr("my-env"), + AllowInBandSync: toPtr(true), + Env: toPtr("my-env"), }) h.Register(fn) server := httptest.NewServer(h)