-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.66 KB
/
index.js
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
import { google } from 'googleapis'
import dotenv from 'dotenv'
import Koa from 'koa'
import koaCash from 'koa-cash';
import TTLCache from '@isaacs/ttlcache';
dotenv.config()
const getLatestOffering = async () => {
var client = new google.auth.JWT(
process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
null,
Buffer.from(process.env.GOOGLE_PRIVATE_KEY, 'base64').toString('utf-8'),
['https://www.googleapis.com/auth/spreadsheets'],
null,
process.env.GOOGLE_PRIVATE_KEY_ID
)
await client.authorize()
var service = google.sheets({
version: 'v4',
auth: client,
})
return (await service.spreadsheets.values.get({
spreadsheetId: "1jzCZQFueFo6hHRZ0apPrc8InIHZhSnJaFTxuVqVIvn0",
range: `C18`,
})).data.values[0][0]
}
const time = 30 * 60 * 1000
const cache = new TTLCache({ttl: time})
const app = new Koa()
app.use(koaCash({
maxAge: time,
get: (key) => cache.get(key),
set: (key, value) => cache.set(key, value),
hash: () => 1,
}))
app.use(async (ctx) => {
if (ctx.path === '/favicon.ico') {
ctx.status = 204
return
}
if (ctx.request.query.type !== 'refresh' && await ctx.cashed()) return;
ctx.type = 'application/rss+xml'
ctx.body = `<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"><channel><title>Wollongong Seventh-day Adventist Church</title><link>https://www.illawarraadventist.org/</link><description>Wollongong Seventh-day Adventist Church</description><item><title>${await getLatestOffering() || "Unknown"}</title><link>https://www.illawarraadventist.org/</link><description></description></item></channel></rss>`
})
app.listen(3100, ()=>{console.log('Server running on port 3100 (inside the container)')});