Skip to content

Commit

Permalink
Added post handler (#32)
Browse files Browse the repository at this point in the history
* Remove podcasts endpoint

* Added dialog close

* Add subjects endpoint

* Added post handler
  • Loading branch information
cultpodcasts authored Aug 3, 2024
1 parent 6c3b15d commit e69345f
Showing 1 changed file with 77 additions and 18 deletions.
95 changes: 77 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Env = {
auth0Issuer: string;
auth0Audience: string;
secureSubmitEndpoint: URL;
securePodcastsEndpoint: URL;
secureEpisodeEndpoint: URL;
secureDiscoveryCurationEndpoint: URL;
}

Expand Down Expand Up @@ -80,16 +80,13 @@ app.get('/homepage', async (c) => {
object = await c.env.Content.get("homepage");
} catch (e) {
}

if (object === null) {
return new Response("Object Not Found", { status: 404 });
}

c.header("etag", object.httpEtag);
c.header("Cache-Control", "max-age=600");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "GET,OPTIONS");

return stream(c, async (stream) => {
stream.onAbort(() => {
console.log('Aborted!')
Expand All @@ -98,6 +95,34 @@ app.get('/homepage', async (c) => {
})
});

app.get('/subjects', auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');

if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
let object: R2ObjectBody | null = null;
try {
object = await c.env.Content.get("subjects");
} catch (e) {
}

if (object === null) {
return new Response("Object Not Found", { status: 404 });
}
c.header("etag", object.httpEtag);
c.header("Cache-Control", "max-age=600");
c.header("Access-Control-Allow-Origin", getOrigin(c.req.header("Origin")));
c.header("Access-Control-Allow-Methods", "GET,OPTIONS");
return stream(c, async (stream) => {
stream.onAbort(() => {
console.log('Aborted!')
})
await stream.pipe(object.body)
})
} else {
return c.json({ message: "Unauthorised" }, 401);
}
});

app.post("/search", async (c) => {
const leechHandlingActive: boolean = false;
const url = `${c.env.apihost}`;
Expand Down Expand Up @@ -133,7 +158,6 @@ app.post("/search", async (c) => {
}
}
}

if (!isLeech) {
return c.req
.json()
Expand Down Expand Up @@ -313,7 +337,7 @@ app.post("/submit", auth0Middleware, async (c) => {
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cultvault-podcasts-api",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.secureSubmitEndpoint).host
},
body: JSON.stringify(data),
Expand All @@ -329,7 +353,6 @@ app.post("/submit", auth0Middleware, async (c) => {
console.log(`Failed to use secure-submit-endpoint. Response code: '${resp.status}'.`);
}
}

console.log(`Storing submission in d1.`);
const adapter = new PrismaD1(c.env.apiDB);
const prisma = new PrismaClient({ adapter });
Expand All @@ -343,7 +366,6 @@ app.post("/submit", auth0Middleware, async (c) => {
} catch {
return c.json({ error: `Invalid url '${data.url}'.` }, 400);
}

try {
const record = {
url: url.toString(),
Expand All @@ -363,35 +385,73 @@ app.post("/submit", auth0Middleware, async (c) => {
return c.json({ success: "Submitted" });
});

app.get("/podcasts", auth0Middleware, async (c) => {
app.get("/episode/:id", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id')
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')) {
if (auth0Payload?.permissions && auth0Payload.permissions.includes('curate')) {
const authorisation: string = c.req.header("Authorization")!;
console.log(`Using auth header '${authorisation.slice(0, 20)}..'`);
const resp = await fetch(c.env.securePodcastsEndpoint, {
const url = `${c.env.secureEpisodeEndpoint}/${id}`;
console.log(url);
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cultvault-podcasts-api",
"Host": new URL(c.env.securePodcastsEndpoint).host
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.secureEpisodeEndpoint).host
},
method: "GET"
});
if (resp.status == 200) {
console.log(`Successfully used secure-podcasts-endpoint.`);
console.log(`Successfully used secure-episode-endpoint.`);

return new Response(resp.body);
} else {
console.log(`Failed to use secure-podcasts-endpoint. Response code: '${resp.status}'.`);
console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`);
}
}
return c.json({ error: "Unauthorised" }, 403);
});

app.post("/episode/:id", auth0Middleware, async (c) => {
const auth0Payload: Auth0JwtPayload = c.var.auth0('payload');
const id = c.req.param('id')
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('curate')) {
const authorisation: string = c.req.header("Authorization")!;
const url = `${c.env.secureEpisodeEndpoint}/${id}`;
const data: any = await c.req.json();
const body: string = JSON.stringify(data)
const resp = await fetch(url, {
headers: {
'Accept': "*/*",
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.secureEpisodeEndpoint).host
},
method: "POST",
body: body
});
if (resp.status == 202) {
console.log(`Successfully used secure-episode-endpoint.`);
return new Response(resp.body);
} else {
console.log(`Failed to use secure-episode-endpoint. Response code: '${resp.status}'.`);
return c.json({ error: "Unauthorised" }, 500);
}
}
return c.json({ error: "Unauthorised" }, 403);
});
Expand All @@ -412,7 +472,7 @@ app.get("/discovery-curation", auth0Middleware, async (c) => {
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cultvault-podcasts-api",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.secureDiscoveryCurationEndpoint).host
},
method: "GET"
Expand All @@ -426,7 +486,6 @@ app.get("/discovery-curation", auth0Middleware, async (c) => {
} else {
console.log(`Failed to use secure-discovery-curation-endpoint. Response code: '${resp.status}'.`);
}

}
return c.json({ error: "Unauthorised" }, 403);
});
Expand All @@ -450,7 +509,7 @@ app.post("/discovery-curation", auth0Middleware, async (c) => {
'Authorization': authorisation,
"Content-type": "application/json",
"Cache-Control": "no-cache",
"User-Agent": "cultvault-podcasts-api",
"User-Agent": "cult-podcasts-api",
"Host": new URL(c.env.secureDiscoveryCurationEndpoint).host
},
method: "POST",
Expand Down

0 comments on commit e69345f

Please sign in to comment.