-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
269 lines (252 loc) · 9.3 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const config = require('./config.json')
const jsonfile = require('jsonfile')
const fs = require('fs')
const path = require('path')
var cookieParser = require('cookie-parser')
const Bible = require('./lib/bible.js')
const port = process.env.PORT || config.port
const translations = jsonfile.readFileSync('data/translations.json')
const structure = jsonfile.readFileSync('data/structure.json')
let bibles = {}
let bibleFiles = fs.readdirSync('data/bibles')
for (let index = 0; index < bibleFiles.length; index++) {
const f = bibleFiles[index]
bibles[f.replace('.json', '')] = jsonfile.readFileSync(path.join('data/bibles/', f))
}
let bible = new Bible(structure, bibles, translations)
const yearly = jsonfile.readFileSync('data/calendar/yearly.json')
const monthly = jsonfile.readFileSync('data/calendar/monthly.json')
const weekly = jsonfile.readFileSync('data/calendar/weekly.json')
const daily = jsonfile.readFileSync('data/calendar/daily.json')
app.use(cookieParser())
app.use(function (req, res, next) { // Enable Cors
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.use(function (req, res, next) { // readout translation and set to req.translation
req.translation = req.query.translation || req.headers.translation || req.cookies.translation || 'elb'
next()
})
app.get('/', function (req, res) {
res.json({})
// TODO: Get Basic Help Information. Send an index.html
})
app.get('/translations', function (req, res) {
res.json(translations)
})
app.get('/translations/:language', function (req, res) {
let result = {}
let key
for (key in translations) {
if (translations.hasOwnProperty(key) && translations[key].language === req.params.language) {
result[key] = translations[key]
}
}
res.json(result)
})
app.get('/bible', function (req, res) {
res.json(structure)
})
app.get('/bible/:book', function (req, res) {
let book = bible.getBook(req.params.book)
if (book) {
res.json(book)
} else {
res.status(404).send('Book ' + req.params.book + ' not found')
}
})
app.get('/bible/:book/:chapter', function (req, res) {
let book = bible.getBook(req.params.book)
if (book) {
if (book.chapters[req.params.chapter]) {
res.json(parseInt(book.chapters[req.params.chapter]))
} else {
res.status(404).send('Book ' + req.params.book + ' with Chapter ' + req.params.chapter + ' not found')
}
} else {
res.status(404).send('Book ' + req.params.book + ' not found')
}
})
app.get('/verse/:id', function (req, res) {
if (req.params.id.includes('-')) { // is id: book-chapter-verse
res.json(bible.getVerseFromID(req.params.id, req.translation)) // FIXME: if undefined, return 404
} else {
// TODO: parse CODE: <BookRef><Chapter>,<verse>[-<verse>][;Repeat]
}
})
app.get('/ids/:book/:chapter/:verse', function (req, res) {
res.json(bible.getIDs(req.params.book, req.params.chapter, req.params.verse)) // FIXME: if undefined, return 404
})
app.get('/ids/:book/:chapter', function (req, res) {
res.json(bible.getIDs(req.params.book, req.params.chapter)) // FIXME: if undefined, return 404
})
app.get('/ids/:book', function (req, res) {
res.json(bible.getIDs(req.params.book)) // FIXME: if undefined, return 404
})
app.get('/book/:book/:chapter/:verse', function (req, res) {
let output = []
let ids = bible.getIDs(req.params.book, req.params.chapter, req.params.verse)
for (let index = 0; index < ids.length; index++) {
const id = ids[index]
output.push(bible.getVerseFromID(id, req.translation))
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/book/:book/:chapter', function (req, res) {
let output = []
let ids = bible.getIDs(req.params.book, req.params.chapter)
for (let index = 0; index < ids.length; index++) {
const id = ids[index]
output.push(bible.getVerseFromID(id, req.translation))
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/book/:book', function (req, res) {
let output = []
let ids = bible.getIDs(req.params.book)
for (let index = 0; index < ids.length; index++) {
const id = ids[index]
output.push(bible.getVerseFromID(id, req.translation))
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/yearly/:year', function (req, res) {
res.json(bible.getVerseFromID(yearly[req.params.year], req.translation)) // FIXME: if undefined, return 404
})
app.get('/yearly/', function (req, res) {
let output = {}
for (var y in yearly) {
if (yearly.hasOwnProperty(y)) {
output[y] = bible.getVerseFromID(yearly[y], req.translation)
}
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/monthly/:year/:month', function (req, res) {
res.json(bible.getVerseFromID(monthly[req.params.year][req.params.month], req.translation)) // FIXME: if undefined, return 404
})
app.get('/monthly/:year', function (req, res) {
let output = {}
for (var m in monthly[req.params.year]) {
if (monthly[req.params.year].hasOwnProperty(m)) {
output[m] = bible.getVerseFromID(monthly[req.params.year][m], req.translation)
}
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/monthly', function (req, res) {
let output = {}
for (var y in monthly) {
if (monthly.hasOwnProperty(y)) {
for (var m in monthly[y]) {
if (monthly[y].hasOwnProperty(m)) {
output[y][m] = bible.getVerseFromID(monthly[y][m], req.translation)
}
}
}
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/weekly/:year/:week', function (req, res) {
res.json(bible.getVerseFromID(weekly[req.params.year][req.params.week], req.translation)) // FIXME: if undefined, return 404
})
app.get('/weekly/:year', function (req, res) {
let output = {}
for (var w in weekly[req.params.year]) {
if (weekly[req.params.year].hasOwnProperty(w)) {
output[w] = bible.getVerseFromID(weekly[req.params.year][w], req.translation)
}
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/weekly', function (req, res) {
let output = {}
for (var y in weekly) {
if (weekly.hasOwnProperty(y)) {
for (var w in weekly[y]) {
if (weekly[y].hasOwnProperty(w)) {
output[y][w] = bible.getVerseFromID(weekly[y][w], req.translation)
}
}
}
}
res.json(output) // FIXME: if undefined, return 404
})
app.get('/daily/:year/:month/:day', function (req, res) {
let output = bible.getVerseFromID(daily[req.params.year][req.params.month][req.params.day]['losung'], req.translation)
if (output) {
res.json(output) // FIXME: also lehrtext
} else {
res.status(404).json({error: 'No Daily Verses for Month ' + req.params.month + ' in year ' + req.params.year + ' found'})
}
})
app.get('/daily/:year/:month', function (req, res) {
let output = {}
for (var d in daily[req.params.year][req.params.month]) {
if (daily[req.params.year][req.params.month].hasOwnProperty(d)) {
output[d] = bible.getVerseFromID(daily[req.params.year][req.params.month][d]['losung'], req.translation) // FIXME: also lehrtext
}
}
if (output) {
res.json(output)
} else {
res.status(404).json({error: 'No Daily Verses for Month ' + req.params.month + ' in year ' + req.params.year + ' found'})
}
})
app.get('/daily/today', function (req, res) {
// TODO: return daily for now!
})
app.get('/daily/:dateOrYear', function (req, res) {
if (req.params.dateOrYear.includes('-')) { // is Date
let ds = req.params.dateOrYear.split('-')
let year = ds[0]
let month = ds[1]
let day = ds[2]
res.json(bible.getVerseFromID(daily[year][month][day]['losung'], req.translation)) // FIXME: if undefined, return 404
// FIXME: also lehrtext
} else { // is Year
let output = {}
for (var m in daily[req.params.dateOrYear]) {
if (daily[req.params.dateOrYear].hasOwnProperty(m)) {
for (var d in daily[req.params.dateOrYear][m]) {
if (daily[req.params.dateOrYear][m].hasOwnProperty(d)) {
output[m][d] = bible.getVerseFromID(daily[req.params.dateOrYear][m][d]['losung'], req.translation) // FIXME: also lehrtext
}
}
}
}
if (output) {
res.json(output)
} else {
res.status(404).json({error: 'No Daily Verses for year ' + req.params.dateOrYear + ' found'})
}
}
})
app.get('/daily', function (req, res) {
let output = {}
for (var y in daily) {
if (daily.hasOwnProperty(y)) {
for (var m in daily[y]) {
if (daily[y].hasOwnProperty(m)) {
for (var d in daily[y][m]) {
if (daily[y][m].hasOwnProperty(d)) {
output[y][m][d] = bible.getVerseFromID(daily[y][m][d]['losung'], req.translation) // FIXME: also lehrtext
}
}
}
}
}
}
if (output) {
res.json(output)
} else {
res.status(404).json({error: 'No Daily Verses found'})
}
})
server.listen(port)
console.log('bible-api running on Port ' + port)