@@ -76,10 +76,21 @@ async def lifespan(app: FastAPI):
76
76
app .mount ("/assets" , StaticFiles (directory = ASSETS_DIR ), name = "assets" )
77
77
app .mount ("/static" , StaticFiles (directory = STATIC_DIR ), name = "static" )
78
78
79
- async def serve_index_html (request : Request = None ):
79
+ async def serve_index_html (request : Request = None , response : Response = None , pad_id : Optional [ UUID ] = None ):
80
80
"""
81
81
Helper function to serve the index.html file or proxy to dev server based on PAD_DEV_MODE.
82
+ Optionally sets a pending_pad_id cookie if pad_id is provided.
82
83
"""
84
+ # Set cookie if pad_id is provided
85
+ if pad_id is not None and response is not None :
86
+ response .set_cookie (
87
+ key = "pending_pad_id" ,
88
+ value = str (pad_id ),
89
+ httponly = True ,
90
+ secure = True ,
91
+ samesite = "lax"
92
+ )
93
+
83
94
if PAD_DEV_MODE :
84
95
try :
85
96
# Proxy the request to the development server's root URL
@@ -89,72 +100,46 @@ async def serve_index_html(request: Request = None):
89
100
url = f"{ DEV_FRONTEND_URL } { request .url .path } "
90
101
91
102
async with httpx .AsyncClient () as client :
92
- response = await client .get (url )
93
- return Response (
94
- content = response .content ,
95
- status_code = response .status_code ,
96
- media_type = response .headers .get ("content-type" )
97
- )
103
+ proxy_response = await client .get (url )
104
+ response .body = proxy_response .content
105
+ response .status_code = proxy_response .status_code
106
+ response .media_type = proxy_response .headers .get ("content-type" )
107
+ return response
98
108
except Exception as e :
99
109
error_message = f"Error proxying to dev server: { e } "
100
110
print (error_message )
101
- return Response (
102
- status_code = 500 ,
103
- )
111
+ response .status_code = 500
112
+ return response
104
113
else :
105
114
# For production, serve the static build
106
- return FileResponse (os .path .join (STATIC_DIR , "index.html" ))
115
+ return FileResponse (os .path .join (STATIC_DIR , "index.html" ), background = response )
107
116
108
117
@app .get ("/pad/{pad_id}" )
109
118
async def read_pad (
110
119
pad_id : UUID ,
111
120
request : Request ,
121
+ response : Response ,
112
122
user : Optional [UserSession ] = Depends (optional_auth ),
113
123
session : AsyncSession = Depends (get_session )
114
124
):
115
125
if not user :
116
- print ("No user found" )
117
- return await serve_index_html (request )
126
+ return await serve_index_html (request , response , pad_id )
118
127
119
128
try :
120
129
pad = await Pad .get_by_id (session , pad_id )
121
130
if not pad :
122
131
print ("No pad found" )
123
- return await serve_index_html (request )
132
+ return await serve_index_html (request , response )
124
133
125
- # Check access permissions
126
134
if not pad .can_access (user .id ):
127
135
print ("No access to pad" )
128
- return await serve_index_html (request )
129
-
130
- # Worker assignment is now handled automatically in Pad.get_by_id()
131
- print (f"Pad { pad_id } accessed by user { user .id } , worker: { pad .worker_id [:8 ] if pad .worker_id else 'None' } " )
136
+ return await serve_index_html (request , response )
132
137
133
- # Add pad to user's open pads if not already there and update last selected pad
134
- user_obj = await User .get_by_id (session , user .id )
135
- if user_obj :
136
- # Convert all UUIDs to strings for comparison
137
- open_pads_str = [str (pid ) for pid in user_obj ._store .open_pads ]
138
- if str (pad_id ) not in open_pads_str :
139
- # Convert back to UUIDs for storage
140
- user_obj ._store .open_pads = [UUID (pid ) for pid in open_pads_str ] + [pad_id ]
141
- try :
142
- await user_obj .save (session )
143
- except Exception as e :
144
- print (f"Error updating user's open pads: { e } " )
145
- # Continue even if update fails - don't block pad access
146
-
147
- # Update last selected pad
148
- try :
149
- await user_obj .set_last_selected_pad (session , pad_id )
150
- except Exception as e :
151
- print (f"Error updating last selected pad: { e } " )
152
- # Continue even if update fails - don't block pad access
153
-
154
- return await serve_index_html (request )
138
+ # Just serve the page if user has access
139
+ return await serve_index_html (request , response , pad_id )
155
140
except Exception as e :
156
141
print (f"Error in read_pad endpoint: { e } " )
157
- return await serve_index_html (request )
142
+ return await serve_index_html (request , response , pad_id )
158
143
159
144
@app .get ("/" )
160
145
async def read_root (request : Request , auth : Optional [UserSession ] = Depends (optional_auth )):
0 commit comments