Spaces:
Running
Running
update for deloy hf
Browse files- .env.example +3 -1
- app/config.py +1 -1
- app/main.py +34 -4
- app/survey/view.py +1 -1
.env.example
CHANGED
|
@@ -16,4 +16,6 @@ JWT_REFRESH_TOKEN_EXTENDED_EXPIRED_IN=
|
|
| 16 |
SAVE_DATA_PATH=
|
| 17 |
|
| 18 |
SPREADSHEET_ID =
|
| 19 |
-
|
|
|
|
|
|
|
|
|
| 16 |
SAVE_DATA_PATH=
|
| 17 |
|
| 18 |
SPREADSHEET_ID =
|
| 19 |
+
GOOGLE_CREDENTIALS_JSON=
|
| 20 |
+
|
| 21 |
+
ENV=
|
app/config.py
CHANGED
|
@@ -19,7 +19,7 @@ class Settings(BaseSettings):
|
|
| 19 |
SAVE_DATA_PATH: str = "reports"
|
| 20 |
|
| 21 |
SPREADSHEET_ID: str
|
| 22 |
-
|
| 23 |
|
| 24 |
class Config:
|
| 25 |
env_file = ".env"
|
|
|
|
| 19 |
SAVE_DATA_PATH: str = "reports"
|
| 20 |
|
| 21 |
SPREADSHEET_ID: str
|
| 22 |
+
GOOGLE_CREDENTIALS_JSON: str
|
| 23 |
|
| 24 |
class Config:
|
| 25 |
env_file = ".env"
|
app/main.py
CHANGED
|
@@ -6,11 +6,41 @@ from .survey.routers import SurveyRouters
|
|
| 6 |
from .mongo import init_db
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI(
|
| 16 |
docs_url="/docs",
|
|
|
|
| 6 |
from .mongo import init_db
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
+
ENV = os.getenv("ENV")
|
| 13 |
+
cred_json = os.getenv("GOOGLE_CREDENTIALS_JSON")
|
| 14 |
+
|
| 15 |
+
if ENV == "hf":
|
| 16 |
+
if cred_json:
|
| 17 |
+
try:
|
| 18 |
+
# Parse để đảm bảo JSON hợp lệ
|
| 19 |
+
json.loads(cred_json)
|
| 20 |
+
|
| 21 |
+
file_path = "google-credentials.json"
|
| 22 |
+
with open(file_path, "w") as f:
|
| 23 |
+
f.write(cred_json)
|
| 24 |
+
|
| 25 |
+
# Set lại env để google auth tự nhận
|
| 26 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = file_path
|
| 27 |
+
|
| 28 |
+
print("[INFO] Google credentials saved to", file_path)
|
| 29 |
+
|
| 30 |
+
except json.JSONDecodeError:
|
| 31 |
+
print("[ERROR] GOOGLE_CREDENTIALS_JSON is not valid JSON")
|
| 32 |
+
|
| 33 |
+
else:
|
| 34 |
+
print("[ERROR] GOOGLE_CREDENTIALS_JSON is missing")
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
# DEV mode (local)
|
| 38 |
+
print("[INFO] ENV != hf → skip Google credentials setup")
|
| 39 |
+
|
| 40 |
+
# @asynccontextmanager
|
| 41 |
+
# async def lifespan(app: FastAPI):
|
| 42 |
+
# await init_db()
|
| 43 |
+
# yield
|
| 44 |
|
| 45 |
app = FastAPI(
|
| 46 |
docs_url="/docs",
|
app/survey/view.py
CHANGED
|
@@ -33,7 +33,7 @@ class SurveyView:
|
|
| 33 |
|
| 34 |
def get_service(self):
|
| 35 |
creds = service_account.Credentials.from_service_account_file(
|
| 36 |
-
settings.
|
| 37 |
scopes=["https://www.googleapis.com/auth/spreadsheets"]
|
| 38 |
)
|
| 39 |
return build("sheets", "v4", credentials=creds)
|
|
|
|
| 33 |
|
| 34 |
def get_service(self):
|
| 35 |
creds = service_account.Credentials.from_service_account_file(
|
| 36 |
+
settings.GOOGLE_CREDENTIALS_JSON,
|
| 37 |
scopes=["https://www.googleapis.com/auth/spreadsheets"]
|
| 38 |
)
|
| 39 |
return build("sheets", "v4", credentials=creds)
|