Skip to content

Commit c3b6257

Browse files
committed
update mongodb issue
1 parent 3f41231 commit c3b6257

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ MONGO_URI=mongodb://<username>:<password>@127.0.0.1:27017/?authSource=admin&retr
33
DOMAIN=https://rzro.link
44
PORT=8001
55
API_VERSION=""
6+
APP_NAMe="LOCAL"
67

78
MONGO_URI="mongodb://<username>@<password>:127.0.0.1:27017/?retryWrites=true&w=majority&appName=<APP-NAME>"
89
DATABASE_NAME="<DB_NAME"

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ tiny/
157157
poetry install
158158
```
159159

160-
### 3. Install with MongoDB Support (Optional)
160+
### 3. Install with other dependent packages
161161

162162
```sh
163-
poetry install --with mongodb
163+
poetry install --all-extras --with dev
164164
```
165165

166166
---
@@ -178,7 +178,7 @@ poetry run tiny dev
178178
```
179179

180180
Open:
181-
http://127.0.0.1:8000
181+
<http://127.0.0.1:8000>
182182

183183
---
184184

@@ -260,11 +260,11 @@ poetry run tiny dev
260260

261261
### API Base URL
262262

263-
http://127.0.0.1:8000/api
263+
<http://127.0.0.1:8000/api>
264264

265265
### Swagger Docs
266266

267-
http://127.0.0.1:8000/api/docs
267+
<http://127.0.0.1:8000/api/docs>
268268

269269
### Shorten URL
270270

app/api/fast_api.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@
22
import re
33
import traceback
44
from datetime import datetime, timezone
5+
from typing import Any
56

67
from fastapi import APIRouter, FastAPI, Request
78
from fastapi.responses import HTMLResponse, JSONResponse
89
from pydantic import BaseModel, Field
910

10-
from typing import TYPE_CHECKING
11-
12-
if TYPE_CHECKING:
13-
from pymongo.errors import PyMongoError
14-
else:
15-
try:
16-
from pymongo.errors import PyMongoError
17-
except ImportError:
18-
19-
class PyMongoError(Exception):
20-
pass
21-
22-
2311
from app import __version__
2412
from app.db import data as db_data
2513
from app.utils.helper import generate_code, is_valid_url, sanitize_url
2614

15+
PyMongoError: Any
16+
try:
17+
from pymongo.errors import PyMongoError as _RealPyMongoError
18+
19+
PyMongoError = _RealPyMongoError
20+
except (ImportError, ModuleNotFoundError):
21+
# 2. Fallback: Define our own only if the real one fails
22+
class _FallbackPyMongoError(Exception):
23+
pass
24+
25+
# Assign our fallback to the same local name
26+
PyMongoError = _FallbackPyMongoError
27+
2728
SHORT_CODE_PATTERN = re.compile(r"^[A-Za-z0-9]{6}$")
2829
MAX_URL_LENGTH = 2048
2930

app/main.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,55 @@
22
import os
33
from contextlib import asynccontextmanager
44
from pathlib import Path
5-
from typing import Optional
5+
from typing import Any, Optional
66

77
from fastapi import FastAPI, Form, Request
88
from fastapi.responses import HTMLResponse, PlainTextResponse, RedirectResponse
99
from fastapi.staticfiles import StaticFiles
1010
from fastapi.templating import Jinja2Templates
1111
from starlette.middleware.sessions import SessionMiddleware
1212

13-
from typing import TYPE_CHECKING
14-
15-
if TYPE_CHECKING:
16-
from pymongo.errors import PyMongoError
17-
else:
18-
try:
19-
from pymongo.errors import PyMongoError
20-
except ImportError:
21-
22-
class PyMongoError(Exception):
23-
pass
24-
25-
26-
# fallback for offline mode
27-
2813
from app.api.fast_api import app as api_app
2914
from app.db import data as db_data
30-
from app.utils.qr import generate_qr_with_logo
31-
3215
from app.utils.config import load_env
3316
from app.utils.helper import (
3417
format_date,
3518
generate_code,
3619
is_valid_url,
3720
sanitize_url,
3821
)
22+
from app.utils.qr import generate_qr_with_logo
23+
24+
load_env() # ✅ load env ONCE
25+
26+
RED = "\033[31m"
27+
GREEN = "\033[32m"
28+
BLUE = "\033[34m"
29+
RESET = "\033[0m"
30+
31+
app_name = os.getenv("APP_NAME", "TinyURL")
32+
print(f"Environment loaded as {BLUE}{app_name}{RESET}")
33+
34+
# 1. MongoDB error handling: Try to import the real exception class first
35+
PyMongoError: Any
36+
try:
37+
from pymongo.errors import PyMongoError as _RealPyMongoError
38+
39+
PyMongoError = _RealPyMongoError
40+
except (ImportError, ModuleNotFoundError):
41+
# 2. Fallback: Define our own only if the real one fails
42+
class _FallbackPyMongoError(Exception):
43+
pass
44+
45+
# Assign our fallback to the same local name
46+
PyMongoError = _FallbackPyMongoError
3947

4048

4149
# -----------------------------
4250
# Lifespan: env + DB connect ONCE
4351
# -----------------------------
4452
@asynccontextmanager
4553
async def lifespan(app: FastAPI):
46-
load_env() # ✅ load env ONCE
4754
connected = db_data.connect_db() # ✅ connect DB ONCE
4855
app.state.db_available = connected
4956
yield

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies = [
2929
"validators>=0.35.0,<0.36.0",
3030
"itsdangerous>=2.2.0,<3.0.0",
3131
"python-multipart>=0.0.22,<0.0.23",
32-
"jinja2 = ^3.1.2",
32+
"jinja2>=3.1.2",
3333
]
3434
[project.optional-dependencies]
3535
mongodb = ["pymongo>=4.16.0,<5.0.0"]

0 commit comments

Comments
 (0)