Last active
July 7, 2022 15:02
-
-
Save reesericci/2875f2049993c0fea7b67abf1eb1984a to your computer and use it in GitHub Desktop.
JB live serverless function
This file contains hidden or 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
REDIS_HOSTNAME="" | |
REDIS_PORT="" | |
REDIS_PASSWORD="" | |
PUBLIC_KEY="RSA 256 public key" | |
DEFAULT_CHATROOM="website URL to live chatroom" | |
DEFAULT_TITLE="Live Stream Title" | |
DEFAULT_URL="website URL to live stream location" |
This file contains hidden or 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
import { serve } from "https://deno.land/[email protected]/http/server.ts"; | |
import { connect } from "https://deno.land/x/[email protected]/mod.ts"; | |
import "https://deno.land/[email protected]/dotenv/load.ts"; | |
import * as jose from 'https://deno.land/x/[email protected]/index.ts' | |
import Show from "./Show.ts"; | |
const redis = await connect({ | |
hostname: Deno.env.get("REDIS_HOSTNAME") ?? "127.0.0.1", | |
port: Deno.env.get("REDIS_PORT"), | |
password: Deno.env.get("REDIS_PASSWORD"), | |
tls: true | |
}); | |
const pubkey = Deno.env.get("PUBLIC_KEY") ?? (function() { | |
throw new Error("Public key was not provided") | |
})() | |
async function get(): Promise<Response> { | |
const obj = await redis.get("SHOW") | |
return new Response(obj ?? JSON.stringify(null), { | |
headers: { "content-type": "application/json" }, | |
}); | |
} | |
async function post(body: {title: string, chatroom: string, token: string }) { | |
try { | |
await jose.jwtVerify(body.token, await jose.importSPKI(pubkey, "RS256")) | |
} catch { | |
return new Response("Unauthorized", { | |
status: 401 | |
}) | |
} | |
await redis.set("SHOW", new Show(body.title ?? Deno.env.get("DEFAULT_TITLE"), body.chatroom ?? Deno.env.get("DEFAULT_CHATROOM"), body.chatroom ?? Deno.env.get("DEFAULT_URL")).toJSON()) | |
return new Response(await redis.get("SHOW"), { | |
headers: { "content-type": "applicat1ion/json" } | |
}) | |
} | |
async function del(body: {token: string}) { | |
try { | |
await jose.jwtVerify(body.token, await jose.importSPKI(pubkey, "RS256")) | |
} catch { | |
return new Response("Unauthorized", { | |
status: 401 | |
}) | |
} | |
await redis.set("SHOW", JSON.stringify(null)) | |
return new Response(JSON.stringify(null), { | |
headers: { "content-type": "application/json" } | |
}) | |
} | |
serve(async (_req: Request) => { | |
if(_req.method == "GET") return await get() | |
if(_req.method == "POST" || _req.method == "DELETE") { | |
try { | |
const body = await _req.json() | |
if(_req.method == "POST") return await post(body) | |
if(_req.method == "DELETE") return await del(body) | |
} catch { | |
return new Response("Bad Request", { | |
status: 400 | |
}) | |
} | |
} | |
return new Response("Method Not Allowed", { | |
status: 403 | |
}) | |
}); |
This file contains hidden or 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
export default class Show { | |
title: string; | |
chatroom: string; | |
url: string; | |
constructor(title: string, chatroom: string, url: string) { | |
this.title = title; | |
this.chatroom = chatroom; | |
this.url = url; | |
} | |
toJSON(): string { | |
return JSON.stringify({ | |
...this | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment