@@ -76,10 +76,21 @@ async def lifespan(app: FastAPI):
7676app .mount ("/assets" , StaticFiles (directory = ASSETS_DIR ), name = "assets" )
7777app .mount ("/static" , StaticFiles (directory = STATIC_DIR ), name = "static" )
7878
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 ):
8080 """
8181 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.
8283 """
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+
8394 if PAD_DEV_MODE :
8495 try :
8596 # Proxy the request to the development server's root URL
@@ -89,72 +100,46 @@ async def serve_index_html(request: Request = None):
89100 url = f"{ DEV_FRONTEND_URL } { request .url .path } "
90101
91102 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
98108 except Exception as e :
99109 error_message = f"Error proxying to dev server: { e } "
100110 print (error_message )
101- return Response (
102- status_code = 500 ,
103- )
111+ response .status_code = 500
112+ return response
104113 else :
105114 # 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 )
107116
108117@app .get ("/pad/{pad_id}" )
109118async def read_pad (
110119 pad_id : UUID ,
111120 request : Request ,
121+ response : Response ,
112122 user : Optional [UserSession ] = Depends (optional_auth ),
113123 session : AsyncSession = Depends (get_session )
114124):
115125 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 )
118127
119128 try :
120129 pad = await Pad .get_by_id (session , pad_id )
121130 if not pad :
122131 print ("No pad found" )
123- return await serve_index_html (request )
132+ return await serve_index_html (request , response )
124133
125- # Check access permissions
126134 if not pad .can_access (user .id ):
127135 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 )
132137
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 )
155140 except Exception as e :
156141 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 )
158143
159144@app .get ("/" )
160145async def read_root (request : Request , auth : Optional [UserSession ] = Depends (optional_auth )):
0 commit comments