Skip to content

Instantly share code, notes, and snippets.

@reesericci
Last active July 7, 2022 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reesericci/2875f2049993c0fea7b67abf1eb1984a to your computer and use it in GitHub Desktop.
Save reesericci/2875f2049993c0fea7b67abf1eb1984a to your computer and use it in GitHub Desktop.
JB live serverless function
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"
import { serve } from "https://deno.land/std@0.140.0/http/server.ts";
import { connect } from "https://deno.land/x/redis@v0.26.0/mod.ts";
import "https://deno.land/std@0.140.0/dotenv/load.ts";
import * as jose from 'https://deno.land/x/jose@v4.8.3/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
})
});
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