Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AllowInBandSync handler option #58

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 18 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
Loading