Lucii1 commited on
Commit
f068bd1
·
1 Parent(s): 08c16b6
Files changed (3) hide show
  1. app/config.py +3 -1
  2. app/main.py +1 -31
  3. app/survey/view.py +9 -3
app/config.py CHANGED
@@ -19,8 +19,10 @@ class Settings(BaseSettings):
19
 
20
  SAVE_DATA_PATH: Optional[str] = "reports"
21
 
 
 
22
  SPREADSHEET_ID: str
23
- GOOGLE_CREDENTIALS_JSON: Optional[str] = None
24
 
25
  class Config:
26
  env_file = ".env"
 
19
 
20
  SAVE_DATA_PATH: Optional[str] = "reports"
21
 
22
+ ENV: Optional[str] = None
23
+
24
  SPREADSHEET_ID: str
25
+ GOOGLE_CREDENTIALS_JSON: str
26
 
27
  class Config:
28
  env_file = ".env"
app/main.py CHANGED
@@ -6,36 +6,6 @@ from .survey.routers import SurveyRouters
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_CREDENTIALS_JSON"] = 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):
@@ -46,7 +16,7 @@ app = FastAPI(
46
  docs_url="/docs",
47
  redoc_url=None,
48
  openapi_url="/openapi.json",
49
- lifespan=lifespan
50
  )
51
 
52
  app.add_middleware(
 
6
  from .mongo import init_db
7
  from contextlib import asynccontextmanager
8
  from fastapi.middleware.cors import CORSMiddleware
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # @asynccontextmanager
11
  # async def lifespan(app: FastAPI):
 
16
  docs_url="/docs",
17
  redoc_url=None,
18
  openapi_url="/openapi.json",
19
+ # lifespan=lifespan
20
  )
21
 
22
  app.add_middleware(
app/survey/view.py CHANGED
@@ -1,10 +1,10 @@
1
 
2
- import os
3
  from fastapi.params import Header
4
  from app.config import settings
5
  from google.oauth2 import service_account
6
  from googleapiclient.discovery import build
7
  from .schemas import SurveyPayload
 
8
 
9
  HEADER_RANGE = "Sheet1!A1:Q1"
10
  DATA_RANGE = "Sheet1!A2:Q"
@@ -33,10 +33,16 @@ class SurveyView:
33
  self.service = self.get_service()
34
 
35
  def get_service(self):
36
- creds = service_account.Credentials.from_service_account_file(
37
- os.environ.get("GOOGLE_CREDENTIALS_JSON"),
 
 
 
 
 
38
  scopes=["https://www.googleapis.com/auth/spreadsheets"]
39
  )
 
40
  return build("sheets", "v4", credentials=creds)
41
 
42
  def ensure_header(self):
 
1
 
 
2
  from fastapi.params import Header
3
  from app.config import settings
4
  from google.oauth2 import service_account
5
  from googleapiclient.discovery import build
6
  from .schemas import SurveyPayload
7
+ import json
8
 
9
  HEADER_RANGE = "Sheet1!A1:Q1"
10
  DATA_RANGE = "Sheet1!A2:Q"
 
33
  self.service = self.get_service()
34
 
35
  def get_service(self):
36
+ if not settings.GOOGLE_CREDENTIALS_JSON:
37
+ raise RuntimeError("Missing GOOGLE_CREDENTIALS_JSON")
38
+
39
+ creds_dict = json.loads(settings.GOOGLE_CREDENTIALS_JSON)
40
+
41
+ creds = service_account.Credentials.from_service_account_info(
42
+ creds_dict,
43
  scopes=["https://www.googleapis.com/auth/spreadsheets"]
44
  )
45
+
46
  return build("sheets", "v4", credentials=creds)
47
 
48
  def ensure_header(self):