From a2609973b88848849d88e210a901e5213d857127 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 8 Nov 2023 19:01:01 -0800 Subject: [PATCH] Disable Subscription if Stripe environment not setup Deduplicate DJANGO_SECRET_KEY and KHOJ_DJANGO_SECRET_KEY to latter name as prefixed with KHOJ as KHOJ app specific --- src/app/settings.py | 2 +- src/khoj/configure.py | 4 +++- src/khoj/interface/web/config.html | 4 +++- src/khoj/routers/auth.py | 4 +++- src/khoj/routers/subscription.py | 4 +--- src/khoj/routers/web_client.py | 1 + src/khoj/utils/state.py | 6 ++++++ 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/app/settings.py b/src/app/settings.py index 44d1d3d6..1cef3c88 100644 --- a/src/app/settings.py +++ b/src/app/settings.py @@ -21,7 +21,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent.parent # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = os.getenv("DJANGO_SECRET_KEY") +SECRET_KEY = os.getenv("KHOJ_DJANGO_SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv("DJANGO_DEBUG", "False") == "True" diff --git a/src/khoj/configure.py b/src/khoj/configure.py index fd0c67fe..6f0589a8 100644 --- a/src/khoj/configure.py +++ b/src/khoj/configure.py @@ -150,7 +150,9 @@ def configure_routes(app): app.include_router(api, prefix="/api") app.include_router(api_beta, prefix="/api/beta") app.include_router(indexer, prefix="/api/v1/index") - app.include_router(subscription_router, prefix="/api/subscription") + if state.billing_enabled: + logger.info("💳 Enabled Billing") + app.include_router(subscription_router, prefix="/api/subscription") app.include_router(web_client) app.include_router(auth_router, prefix="/auth") diff --git a/src/khoj/interface/web/config.html b/src/khoj/interface/web/config.html index 082c5d01..b2b7fbb3 100644 --- a/src/khoj/interface/web/config.html +++ b/src/khoj/interface/web/config.html @@ -158,7 +158,8 @@ -
+ {% if billing_enabled %} +

Billing

@@ -219,6 +220,7 @@
+ {% endif %}
diff --git a/src/khoj/routers/auth.py b/src/khoj/routers/auth.py index ebabeb8e..4a3cbcef 100644 --- a/src/khoj/routers/auth.py +++ b/src/khoj/routers/auth.py @@ -24,7 +24,9 @@ logger = logging.getLogger(__name__) auth_router = APIRouter() if not state.anonymous_mode and not (os.environ.get("GOOGLE_CLIENT_ID") and os.environ.get("GOOGLE_CLIENT_SECRET")): - logger.info("Please set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables to use Google OAuth") + logger.warn( + "🚨 Use --anonymous-mode flag to disable Google OAuth or set GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET environment variables to enable it" + ) else: config = Config(environ=os.environ) diff --git a/src/khoj/routers/subscription.py b/src/khoj/routers/subscription.py index 5b6998c0..3457b671 100644 --- a/src/khoj/routers/subscription.py +++ b/src/khoj/routers/subscription.py @@ -12,13 +12,11 @@ import stripe # Internal Packages from database import adapters + # Stripe integration for Khoj Cloud Subscription stripe.api_key = os.getenv("STRIPE_API_KEY") endpoint_secret = os.getenv("STRIPE_SIGNING_SECRET") - - logger = logging.getLogger(__name__) - subscription_router = APIRouter() diff --git a/src/khoj/routers/web_client.py b/src/khoj/routers/web_client.py index b47f6537..229cee64 100644 --- a/src/khoj/routers/web_client.py +++ b/src/khoj/routers/web_client.py @@ -150,6 +150,7 @@ def config_page(request: Request): "conversation_options": all_conversation_options, "selected_conversation_config": selected_conversation_config.id if selected_conversation_config else None, "user_photo": user_picture, + "billing_enabled": state.billing_enabled, "subscription_state": user_subscription_state, "subscription_renewal_date": subscription_renewal_date, "khoj_cloud_subscription_url": os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL"), diff --git a/src/khoj/utils/state.py b/src/khoj/utils/state.py index 748ca15a..098ae35e 100644 --- a/src/khoj/utils/state.py +++ b/src/khoj/utils/state.py @@ -1,4 +1,5 @@ # Standard Packages +import os import threading from typing import List, Dict from collections import defaultdict @@ -35,3 +36,8 @@ khoj_version: str = None device = get_device() chat_on_gpu: bool = True anonymous_mode: bool = False +billing_enabled: bool = ( + os.getenv("STRIPE_API_KEY") is not None + and os.getenv("STRIPE_SIGNING_SECRET") is not None + and os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL") is not None +)