Frank hace 9 meses
padre
commit
34a2dcb80a

+ 11 - 11
app/infra/app.ts

@@ -23,14 +23,14 @@ export const api = new sst.cloudflare.Worker("Api", {
   },
 })
 
-//new sst.cloudflare.StaticSite("Web", {
-//  path: "packages/web",
-//  environment: {
-//    VITE_API_URL: api.url,
-//  },
-//  errorPage: "fallback.html",
-//  build: {
-//    command: "bun run build",
-//    output: "dist/client",
-//  },
-//})
+new sst.cloudflare.StaticSite("Web", {
+  path: "packages/web",
+  environment: {
+    VITE_API_URL: api.url,
+  },
+  errorPage: "fallback.html",
+  build: {
+    command: "bun run build",
+    output: "dist/client",
+  },
+})

+ 13 - 13
app/packages/function/src/api.ts

@@ -21,20 +21,20 @@ export class SyncServer extends DurableObject {
     })
   }
 
-  async publish(filename: string, content: string) {
+  async publish(key: string, content: string) {
     console.log(
       "SyncServer publish",
-      filename,
+      key,
       content,
       "to",
       this.ctx.getWebSockets().length,
       "subscribers",
     )
-    this.files.set(filename, content)
-    await this.ctx.storage.put(filename, content)
+    this.files.set(key, content)
+    await this.ctx.storage.put(key, content)
 
     this.ctx.getWebSockets().forEach((client) => {
-      client.send(JSON.stringify({ filename, content }))
+      client.send(JSON.stringify({ key, content }))
     })
   }
 
@@ -57,8 +57,8 @@ export class SyncServer extends DurableObject {
     this.ctx.acceptWebSocket(server)
 
     setTimeout(() => {
-      this.files.forEach((content, filename) =>
-        server.send(JSON.stringify({ filename, content })),
+      this.files.forEach((content, key) =>
+        server.send(JSON.stringify({ key, content })),
       )
     }, 0)
 
@@ -107,12 +107,12 @@ export default {
       const body = await request.json()
       const sessionID = body.session_id
       const shareID = body.share_id
-      const filename = body.filename
+      const key = body.key
       const content = body.content
 
-      // validate filename
-      if (!filename.startsWith("info/") && !filename.startsWith("message/"))
-        return new Response("Error: Invalid filename", { status: 400 })
+      // validate key
+      if (!key.startsWith("info/") && !key.startsWith("message/"))
+        return new Response("Error: Invalid key", { status: 400 })
 
       const infoFile = `${shareID}/info/${sessionID}.json`
       const ret = await Resource.Bucket.get(infoFile)
@@ -122,10 +122,10 @@ export default {
       // send message to server
       const id = env.SYNC_SERVER.idFromName(sessionID)
       const stub = env.SYNC_SERVER.get(id)
-      await stub.publish(filename, content)
+      await stub.publish(key, content)
 
       // store message
-      await Resource.Bucket.put(`${shareID}/${filename}`, content)
+      await Resource.Bucket.put(`${shareID}/${key}`, content)
 
       return new Response(JSON.stringify({}), {
         headers: { "Content-Type": "application/json" },

+ 4 - 0
app/packages/function/sst-env.d.ts

@@ -6,6 +6,10 @@
 import "sst"
 declare module "sst" {
   export interface Resource {
+    "Web": {
+      "type": "sst.cloudflare.StaticSite"
+      "url": string
+    }
   }
 }
 // cloudflare 

+ 4 - 4
app/packages/web/src/routes/share/[id].tsx

@@ -3,7 +3,7 @@ import { createSignal, onCleanup, onMount, Show, For } from "solid-js"
 import { useParams } from "@solidjs/router"
 
 type Message = {
-  filename: string
+  key: string
   content: string
 }
 
@@ -39,7 +39,7 @@ export default function SharePage() {
       setConnectionStatus("Connecting...")
 
       // Always use secure WebSocket protocol (wss)
-      const wsBaseUrl = apiUrl.replace(/^https?:\/\//, 'wss://')
+      const wsBaseUrl = apiUrl.replace(/^https?:\/\//, "wss://")
       const wsUrl = `${wsBaseUrl}/share_poll?share_id=${shareId}`
       console.log("Connecting to WebSocket URL:", wsUrl)
 
@@ -78,7 +78,7 @@ export default function SharePage() {
         clearTimeout(reconnectTimer)
         reconnectTimer = window.setTimeout(
           setupWebSocket,
-          2000
+          2000,
         ) as unknown as number
       }
     }
@@ -133,7 +133,7 @@ export default function SharePage() {
                     }}
                   >
                     <div>
-                      <strong>Filename:</strong> {msg.filename}
+                      <strong>Key:</strong> {msg.key}
                     </div>
                     <div>
                       <strong>Content:</strong> {msg.content}

+ 4 - 0
app/sst-env.d.ts

@@ -12,6 +12,10 @@ declare module "sst" {
     "Bucket": {
       "type": "sst.cloudflare.Bucket"
     }
+    "Web": {
+      "type": "sst.cloudflare.StaticSite"
+      "url": string
+    }
   }
 }
 /// <reference path="sst-env.d.ts" />