-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shifted everything to using webpack to build a single file for cloudf…
…lare
- Loading branch information
jiaming
committed
Mar 20, 2019
1 parent
db18073
commit 71bcfb8
Showing
6 changed files
with
99 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
addEventListener('fetch', event => { | ||
event.respondWith(fetchAndApply(event.request)) | ||
}) | ||
|
||
const mailGetKey = require("./src/cloudflare-api/mailGetKey") | ||
const mailGetHtml = require("./src/cloudflare-api/mailGetHtml") | ||
const mailList = require("./src/cloudflare-api/mailList") | ||
|
||
async function fetchAndApply(request) { | ||
let url = new URL(request.url) | ||
if (request.method === "OPTIONS") { | ||
return handleOptions(request) | ||
} else if(url.pathname === "/api/v1/mail/list"){ | ||
return mailList(url) | ||
} else if(url.pathname === "/api/v1/mail/getKey") { | ||
return mailGetKey(url) | ||
} else if (url.pathname === "/api/v1/mail/getHtml") { | ||
return mailGetHtml(url) | ||
} | ||
|
||
return new Response('Invalid endpoint - ' + url.pathname, | ||
{ status: 400, statusText: 'INVALID_ENDPOINT' }); | ||
} | ||
|
||
let config = require("./config/mailgunConfig") | ||
|
||
const corsHeaders = { | ||
"Access-Control-Allow-Origin": config.corsOrigin, | ||
"Access-Control-Allow-Methods": "GET, POST, OPTIONS", | ||
"Access-Control-Allow-Headers": "Content-Type", | ||
} | ||
|
||
function handleOptions(request) { | ||
if (request.headers.get("Origin") !== null && | ||
request.headers.get("Access-Control-Request-Method") !== null && | ||
request.headers.get("Access-Control-Request-Headers") !== null) { | ||
// Handle CORS pre-flight request. | ||
return new Response(null, { | ||
headers: corsHeaders | ||
}) | ||
} else { | ||
// Handle standard OPTIONS request. | ||
return new Response(null, { | ||
headers: { | ||
"Allow": "GET, POST, OPTIONS", | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters