(request: Request, env: Env, ctx: ExecutionContext)
| 87 | } |
| 88 | |
| 89 | async function handleManagementRequest(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| 90 | const authHeader = request.headers.get('authorization'); |
| 91 | if (!authHeader) { |
| 92 | return new Response('Authorization header is required', { status: 401 }); |
| 93 | } |
| 94 | |
| 95 | const authHeaderParts = authHeader.split(' '); |
| 96 | if (authHeaderParts.length !== 2) { |
| 97 | return new Response('Authorization header is invalid', { status: 401 }); |
| 98 | } |
| 99 | |
| 100 | const [authType, authKey] = authHeaderParts; |
| 101 | |
| 102 | if (authType !== 'Bearer') { |
| 103 | return new Response('Authorization header is invalid', { status: 401 }); |
| 104 | } |
| 105 | |
| 106 | if (authKey !== env.SECRET_KEY) { |
| 107 | return new Response('Authorization header is invalid', { status: 401 }); |
| 108 | } |
| 109 | |
| 110 | // Okay now we can actually handle the request |
| 111 | // We need to look at the path and see what we are doing |
| 112 | // POST /api/tunnels -> create a new tunnel |
| 113 | const url = new URL(request.url); |
| 114 | |
| 115 | if (url.pathname === '/api/tunnels' && request.method === 'POST') { |
| 116 | return handleCreateTunnel(request, env, ctx); |
| 117 | } |
| 118 | |
| 119 | return new Response('Not Found', { status: 404 }); |
| 120 | } |
| 121 | |
| 122 | async function handleCreateTunnel(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| 123 | const tunnelId = env.connections.newUniqueId(); |
no test coverage detected
searching dependent graphs…