Skip to content

Commit

Permalink
Added new get-podcasts endpoint (#23)
Browse files Browse the repository at this point in the history
* Added new get-podcasts endpoint

* Fix typo

---------

Co-authored-by: Jon Breen <[email protected]>
  • Loading branch information
cultpodcasts and cultpodcasts committed May 15, 2024
1 parent a65dee0 commit 67ff35f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 42 deletions.
78 changes: 39 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20240423.0",
"typescript": "^5.0.4",
"wrangler": "^3.53.1"
"wrangler": "^3.56.0"
},
"dependencies": {
"@cfworker/jwt": "^4.0.6",
Expand Down
38 changes: 36 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type Env = {
gatewayKey: string;
auth0Issuer: string;
auth0Audience: string;
secureSubmitEndpoint: string;
secureSubmitEndpoint: URL;
securePodcastsEndpoint: URL;
}

const allowedOrigins: Array<string> = [
Expand Down Expand Up @@ -326,7 +327,7 @@ app.post("/submit", auth0Middleware, async (c) => {
try {
const record = {
url: url.toString(),
ip_address: c.req.header("CF-Connecting-IP") ?? null,
ip_address: c.req.header("CF-Connecting-IP") ?? "Unkown",
user_agent: c.req.header("User-Agent") ?? null,
country: c.req.header("CF-IPCountry") ?? null
};
Expand All @@ -342,5 +343,38 @@ app.post("/submit", auth0Middleware, async (c) => {
return c.json({ success: "Submitted" });
});

app.get("/podcasts", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
c.header("Cache-Control", "max-age=600");
c.header("Content-Type", "application/json");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "POST,GET,OPTIONS");

if (auth0Payload?.permissions && auth0Payload.permissions.includes('submit')) {
const authorisation: string = c.req.header("Authorization")!;
console.log(`Using auth header '${authorisation.slice(0, 20)}..'`);
const resp = await fetch(c.env.securePodcastsEndpoint, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cultvault-podcasts-api",
"Host": new URL(c.env.securePodcastsEndpoint).host
},
method: "GET"
});
if (resp.status == 200) {
console.log(`Successfully used secure enpoint.`);

return new Response(resp.body);
} else {
console.log(`Failed to use secure submit endpoint. Response code: '${resp.status}'.`);
}

}
return c.json({ error: "Unauthorised" }, 403);
});


export default app;

0 comments on commit 67ff35f

Please sign in to comment.