From 1bef762caf1ff49e6e84febb1776305cc24b6945 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Tue, 31 Oct 2023 16:20:14 +0530 Subject: [PATCH] Add #412 Default application mode should be single-tenant --- internal/cmd/start_api.go | 2 +- internal/middleware/application_mode.go | 26 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/cmd/start_api.go b/internal/cmd/start_api.go index acead65..c50471c 100644 --- a/internal/cmd/start_api.go +++ b/internal/cmd/start_api.go @@ -84,7 +84,7 @@ func StartApiCmdHandler(cmd *cobra.Command, args []string) { tenant.SingleTenantConfiguration(loadedConfig) case config.MultiTenant: default: - panic("Application mode is mandatory. Specify either 'single-tenant' or 'multi-tenant'.") + tenant.SingleTenantConfiguration(loadedConfig) } // Router diff --git a/internal/middleware/application_mode.go b/internal/middleware/application_mode.go index fe735cf..8c263a5 100644 --- a/internal/middleware/application_mode.go +++ b/internal/middleware/application_mode.go @@ -23,16 +23,23 @@ func SetApplicationMode() Middleware { // Define the http.HandlerFunc return func(w http.ResponseWriter, r *http.Request) { - if ApplicationMode == config.SingleTenant { + switch ApplicationMode { + case config.SingleTenant: + err := singleTenantConfig(r) + if err != nil { + m := "Failed to find organization" + common.HandleError(w, http.StatusBadRequest, m, err) + return + } - organization, err := org.GetFirstOrganization() + case config.MultiTenant: + default: + err := singleTenantConfig(r) if err != nil { m := "Failed to find organization" common.HandleError(w, http.StatusBadRequest, m, err) return } - organizationId := organization.ID.Hex() - r.Header.Set(config.OrganizationId, organizationId) } // Call the next middleware/handler in chain @@ -40,3 +47,14 @@ func SetApplicationMode() Middleware { } } } + +// singleTenantConfig +func singleTenantConfig(r *http.Request) error { + organization, err := org.GetFirstOrganization() + if err != nil { + return err + } + organizationId := organization.ID.Hex() + r.Header.Set(config.OrganizationId, organizationId) + return nil +}