From f01c3a59ae812d8263c554aef1ae3cd9f5bd5f8d Mon Sep 17 00:00:00 2001 From: Akshay Gaikwad Date: Fri, 21 Jan 2022 16:18:54 +0530 Subject: [PATCH] Declare baseUrl at package level --- .../usermgmt/pkg/service/oidc_provider.go | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/components/usermgmt/pkg/service/oidc_provider.go b/components/usermgmt/pkg/service/oidc_provider.go index 929e9dd..38b6cb4 100644 --- a/components/usermgmt/pkg/service/oidc_provider.go +++ b/components/usermgmt/pkg/service/oidc_provider.go @@ -16,6 +16,20 @@ import ( "google.golang.org/protobuf/types/known/structpb" ) +var baseUrl *url.URL + +func init() { + base, ok := os.LookupEnv("APP_HOST_HTTP") + if !ok || len(base) == 0 { + panic("APP_HOST_HTTP env not set") + } + var err error + baseUrl, err = url.Parse(base) + if err != nil { + panic("Failed to get application url") + } +} + type OIDCProviderService interface { Create(context.Context, *userv3.OIDCProvider) (*userv3.OIDCProvider, error) GetByID(context.Context, *userv3.OIDCProvider) (*userv3.OIDCProvider, error) @@ -35,12 +49,8 @@ func NewOIDCProviderService(db *bun.DB) OIDCProviderService { } func generateCallbackUrl() (string, error) { - base, err := url.Parse(os.Getenv("APP_HOST_HTTP")) - if err != nil { - return "", err - } uuid := uuid.New() - return fmt.Sprintf("%s/auth/v3/sso/callback/%s", base, uuid), nil + return fmt.Sprintf("%s/auth/v3/sso/callback/%s", baseUrl.String(), uuid), nil } func (s *oidcProvider) Create(ctx context.Context, provider *userv3.OIDCProvider) (*userv3.OIDCProvider, error) {