-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (107 loc) · 3.33 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const admin = require('firebase-admin')
const cors = require('cors')
const express = require('express')
const { logger } = require('firebase-functions')
const { https, pubsub } = require('firebase-functions/v1')
const {
getWidgetContent,
validWidgetIds
} = require('./lib/get-widget-content')
const syncGoodreadsData = require('./jobs/sync-goodreads-data')
const syncInstagramData = require('./jobs/sync-instagram-data')
const syncSpotifyData = require('./jobs/sync-spotify-data')
const syncSteamData = require('./jobs/sync-steam-data')
const firebaseServiceAccountToken = require('./token.json')
admin.initializeApp({
credential: admin.credential.cert(firebaseServiceAccountToken),
databaseURL: 'https://personal-stats-chrisvogt.firebaseio.com',
})
admin.firestore().settings({
// Firestore throws when saving documents containing null values and the
// Goodreads response object contains null values for unset fields. The
// Firestore `ignoreUndefinedProperties` option enables support for fields
// with null values.
ignoreUndefinedProperties: true,
})
exports.syncGoodreadsData = pubsub
.schedule('every day 02:00')
.onRun(() => syncGoodreadsData())
exports.syncSpotifyData = pubsub
.schedule('every day 02:00')
.onRun(() => syncSpotifyData())
exports.syncSteamData = pubsub
.schedule('every day 02:00')
.onRun(() => syncSteamData())
exports.syncInstagramData = pubsub
.schedule('every day 02:00')
.onRun(() => syncInstagramData())
const buildSuccessResponse = (payload) => ({
ok: true,
payload,
})
const buildFailureResponse = (err = {}) => ({
ok: false,
error: err.message || err,
})
const app = express()
const corsAllowList = [
/https?:\/\/([a-z0-9]+[.])*chrisvogt[.]me$/,
/https?:\/\/([a-z0-9]+[.])*dev-chrisvogt[.]me:?(.*)$/,
/\.netlify\.app$/
]
const corsOptions = {
origin: corsAllowList
}
const syncHandlersByProvider = {
goodreads: syncGoodreadsData,
instagram: syncInstagramData,
spotify: syncSpotifyData,
steam: syncSteamData
}
app.get(
'/api/widgets/sync/:provider',
async (req, res) => {
const provider = req.params.provider
const handler = syncHandlersByProvider[provider]
if (!handler) {
logger.log(`Attempted to sync an unrecognized provider: ${provider}`)
res.status(400).send('Unrecognized or unsupported provider.')
}
try {
const result = await handler()
res.status(200).send(result)
} catch (err) {
logger.error('Error syncing data manually.', err)
res.status(500).send({ error: err })
}
}
)
app.get(
'/api/widgets/:provider',
cors(corsOptions),
async (req, res) => {
const provider = req.params.provider
if (!provider || !validWidgetIds.includes(provider)) {
const response = buildFailureResponse({
message: 'A valid provider type is required.',
})
res.status(404).send(response)
return res.end()
}
try {
const widgetContent = await getWidgetContent(provider)
const response = buildSuccessResponse(widgetContent)
res.set('Cache-Control', 'public, max-age=3600, s-maxage=7200')
res.status(200).send(response)
} catch (err) {
const response = buildFailureResponse(err)
res.status(400).send(response)
}
return res.end()
}
)
app.get('*', (req, res) => {
res.sendStatus(404)
return res.end()
})
exports.app = https.onRequest(app)