-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
103 lines (86 loc) · 2.67 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/** @jsx h */
import { h } from 'preact'
import admin from 'firebase-admin'
import 'jsr:@std/dotenv/load'
import { updateRemoteConfigValue } from './utils/update-remote-config.ts'
import { connect_pusher } from './utils/kick-websocket.ts'
import { scrapeWebsite } from './utils/scrapper.ts'
import { render } from 'preact-render-to-string'
import { StatusPage } from './views/home/home.tsx'
import { NotFoundPage } from './views/404/page.tsx'
// Constants
const KICK_URL = 'https://kick.com/xqc'
const PORT = 8000
const CRON_SCHEDULE = '0 * * * *'
// State management
const state = {
latestKey: 'none',
latestCluster: 'none',
lastTimeUpdated: 'none',
}
// Initialize Firebase
const initializeFirebase = () => {
const privateKey = Deno.env.get('FIREBASE_PRIVATE_KEY')?.replace(
/\\n/gm,
'\n',
)
if (!privateKey) throw new Error('FIREBASE_PRIVATE_KEY is not set')
const config = {
credential: admin.credential.cert({
clientEmail: Deno.env.get('FIREBASE_CLIENT_EMAIL'),
privateKey,
projectId: Deno.env.get('FIREBASE_PROJECT_ID'),
}),
}
return admin.apps.length ? admin.app() : admin.initializeApp(config)
}
export const firebase = initializeFirebase()
// Main function to update Pusher configuration
export async function updatePusherConfig() {
try {
const { pusherAppKey, pusherCluster } = await scrapeWebsite(KICK_URL)
if (!pusherAppKey || !pusherCluster) {
throw new Error('No Pusher App Key or Cluster found')
}
await connect_pusher(pusherAppKey, pusherCluster)
console.log('%c🔌 Connected to Pusher successfully! ✅', 'color: green')
const remoteConfigKey = Deno.env.get('REMOTE_CONFIG_KEY')
if (!remoteConfigKey) {
throw new Error('REMOTE_CONFIG_KEY is not set')
}
await updateRemoteConfigValue(remoteConfigKey, pusherAppKey)
// Update state
Object.assign(state, {
latestKey: pusherAppKey,
latestCluster: pusherCluster,
lastTimeUpdated: new Date().toISOString(),
})
} catch (error) {
console.error('Error updating Pusher config:', error)
}
}
// Request handler
const handleRequest = (req: Request) => {
const isHome = req.url.endsWith('/')
const { latestKey, latestCluster, lastTimeUpdated } = state
if (isHome) {
return new Response(
render(
h(StatusPage, { latestKey, latestCluster, lastTimeUpdated }),
),
{ headers: { 'content-type': 'text/html' } },
)
}
return new Response(
render(h(NotFoundPage, {})),
{ status: 404, headers: { 'content-type': 'text/html' } },
)
}
if (import.meta.main) {
// Initial run
await updatePusherConfig()
// Schedule hourly updates
Deno.cron('Check for new Pusher App Key', CRON_SCHEDULE, updatePusherConfig)
// Start HTTP server
Deno.serve({ port: PORT }, handleRequest)
}