@@ -118,7 +118,7 @@ export class MyDurableObject extends DurableObject {
118
118
}
119
119
}
120
120
121
- const DO_ID = "foo" ;
121
+ const DO_NAME = "foo" ;
122
122
123
123
export default {
124
124
/**
@@ -144,37 +144,33 @@ export default {
144
144
// Create a `DurableObjectId` for an instance of the `MyDurableObject`
145
145
// class named "foo". Requests from all Workers to the instance named
146
146
// "foo" will go to a single globally unique Durable Object instance.
147
- const id = env . MY_DURABLE_OBJECT . idFromName ( DO_ID ) ;
147
+ const id = env . MY_DURABLE_OBJECT . idFromName ( DO_NAME ) ;
148
148
149
149
// Create a stub to open a communication channel with the Durable
150
150
// Object instance.
151
151
const stub = env . MY_DURABLE_OBJECT . get ( id ) ;
152
152
153
153
return stub . fetch ( request ) ;
154
154
} else if ( url . pathname . startsWith ( "/proxy/" ) ) {
155
- const id = env . MY_DURABLE_OBJECT . idFromName ( DO_ID ) ;
155
+ const id = env . MY_DURABLE_OBJECT . idFromName ( DO_NAME ) ;
156
156
157
157
// Create a stub to open a communication channel with the Durable
158
158
// Object instance.
159
159
const stub = env . MY_DURABLE_OBJECT . get ( id ) ;
160
160
return stub . proxy ( request ) ;
161
- } else if ( url . pathname . startsWith ( "/close" ) ) {
162
- const id = env . MY_DURABLE_OBJECT . idFromName ( DO_ID ) ;
163
-
164
- // Create a stub to open a communication channel with the Durable
165
- // Object instance.
161
+ } else if ( url . pathname == "/close" ) {
162
+ if ( request . method !== "POST" ) {
163
+ return new Response ( "Method not allowed" , { status : 405 } ) ;
164
+ }
165
+ const id = env . MY_DURABLE_OBJECT . idFromName ( DO_NAME ) ;
166
166
const stub = env . MY_DURABLE_OBJECT . get ( id ) ;
167
167
return stub . close ( ) ;
168
- } else {
169
- return new Response (
170
- "Websocket endpoint is listenning on <code>/tunnel</code>" ,
171
- {
172
- headers : {
173
- "Content-Type" : "text/html" ,
174
- } ,
175
- } ,
176
- ) ;
168
+ } else if ( url . pathname == "/" ) {
169
+ return new Response ( indexPage ( url . origin ) , {
170
+ headers : { "content-type" : "text/html" } ,
171
+ } ) ;
177
172
}
173
+ return new Response ( "Not found" , { status : 404 } ) ;
178
174
} ,
179
175
} satisfies ExportedHandler < Env > ;
180
176
@@ -191,3 +187,38 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
191
187
} ) ;
192
188
} ) ;
193
189
}
190
+
191
+ function indexPage ( root : string ) : string {
192
+ return `
193
+ <!doctype html>
194
+ <html lang="en">
195
+ <head>
196
+ <meta charset="UTF-8" />
197
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
198
+ <title>Hello, World!</title>
199
+ <link
200
+ rel="stylesheet"
201
+ href="/pico.min.css"
202
+ >
203
+ </head>
204
+ <body>
205
+ <main class="container">
206
+ <h1>Webhooks Proxy Tunnel</h1>
207
+ <p>Tunnel URL: <code>${ root } /tunnel</code></p>
208
+ <p>Proxy URL: <code>${ root } /proxy/</code></p>
209
+ <p>Force <button id="close" class="outline">close</button> the tunnel.</p>
210
+ </main>
211
+ <script>
212
+ async function closeTunnel() {
213
+ const response = await fetch("/close", {
214
+ method: "POST",
215
+ body: "",
216
+ })
217
+
218
+ console.log(response);
219
+ }
220
+ document.querySelector("#close").addEventListener("click", closeTunnel);
221
+ </script>
222
+ </body>
223
+ </html>` ;
224
+ }
0 commit comments