-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
204 lines (172 loc) · 5.95 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Access .env variables
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const next = require('next')
const cache = require('./cache')
const Airtable = require('airtable')
Airtable.configure({ apiKey: process.env.AIRTABLE_API_KEY })
const dev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000
const app = next({ dev })
const handle = app.getRequestHandler()
// Markdown support for JSON feed
const MarkdownIt = require('markdown-it')
const md = new MarkdownIt()
const serialize = data => JSON.stringify({ data })
/* Main Airtable Query */
const getAirtablePosts = (baseId) => {
const base = new Airtable.base(baseId)
return new Promise((resolve, reject) => {
cache.get('airtablePosts', function(error, data) {
if (error) throw error
if (!!data) {
// Stored value, grab from cache
resolve(JSON.parse(data))
}
else {
// No stored value, retrieve from Airtable
const storeAirtablePosts = []
// Query
const apiQuery = {
pageSize: 50,
sort: [{field: 'Publish Date', direction: 'desc'}]
}
// Go get it!
base('Posts').select(apiQuery).eachPage((records, fetchNextPage) => {
// This function (`page`) will get called for each page of records.
// The properties here would correspond to your records
records.forEach(function(record) {
const post = {
title: record.get('Title'),
content: record.get('Content'),
publish_date: record.get('Publish Date'),
slug: record.get('Slug'),
id: record.id
}
storeAirtablePosts.push(post)
})
fetchNextPage()
}, function done(error) {
if (error) reject({ error })
// Store results in Redis, expires in 30 sec
cache.setex('airtablePosts', 30, JSON.stringify(storeAirtablePosts))
// Finish
resolve(storeAirtablePosts)
})
}
})
})
}
/* Get Individual Airtable Record */
const getAirtablePost = (recordId, baseId) => {
const base = new Airtable.base(baseId)
const cacheRef = '_cachedAirtableBook_'+recordId
return new Promise((resolve, reject) => {
cache.get(cacheRef, function(error, data) {
if (error) throw error
if (!!data) {
// Stored value, grab from cache
resolve(JSON.parse(data))
}
else {
base('Posts').find(recordId, function(err, record) {
if (err) {
console.error(err)
reject({ err })
}
const airtablePost = {
title: record.get('Title'),
content: record.get('Content'),
publish_date: record.get('Publish Date')
}
// Store results in Redis, expires in 30 sec
cache.setex(cacheRef, 30, JSON.stringify(airtablePost));
resolve(airtablePost)
})
}
})
})
}
app.prepare()
.then(() => {
const server = express()
// Internal API call to get Airtable data
server.get('/api/get/posts', (req, res) => {
Promise.resolve(getAirtablePosts(process.env.AIRTABLE_BASE_ID)).then(data => {
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(serialize(data))
}).catch((error) => {
console.log(error)
// Send empty JSON otherwise page load hangs indefinitely
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(serialize({}))
})
})
// Internal API call to get individual Airtable post
server.get('/api/post/:id', (req, res) => {
Promise.resolve(getAirtablePost(req.params.id, process.env.AIRTABLE_BASE_ID)).then(data => {
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(serialize(data))
}).catch((error) => {
console.log(error)
// Send empty JSON otherwise page load hangs indefinitely
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(serialize({}))
})
})
// JSON Feed
server.get('/feed/json', (req, res) => {
Promise.resolve(getAirtablePosts(process.env.AIRTABLE_BASE_ID)).then(data => {
const jsonFeed = {
"version": "https://jsonfeed.org/version/1",
"home_page_url": "https://yourdomain.com/",
"feed_url": "https://yourdomain.com/feed/json",
"title": "YOUR SITE TITLE",
"description": "YOUR SITE DESCRIPTION",
"items": [
]
}
// Go through each item in returned array and add it to our JSON Feed object
data.map((item) => {
jsonFeed.items.push({
"id": `https://yourdomain.com/post/${item.id}/${item.slug}`,
"url": `https://yourdomain.com/post/${item.id}/${item.slug}`,
"title": item.title,
"content_html": !!item.content ? md.render(item.content) : '',
"date_published": item.publish_date,
"author": {
"name": "YOUR NAME"
}
})
})
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(JSON.stringify(jsonFeed, null, 2))
}).catch((error) => {
console.log(error)
// Send empty JSON otherwise page load hangs indefinitely
res.writeHead(200, {'Content-Type': 'application/json'})
return res.end(serialize({}))
})
})
server.get('/post/:id/:slug', (req, res) => {
const actualPage = '/single'
const queryParams = {
id: req.params.id,
slug: req.params.slug
}
app.render(req, res, actualPage, queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})