-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathroutes.js
127 lines (119 loc) · 3.49 KB
/
routes.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
const express = require('express')
const router = express.Router()
const cache = require('memory-cache')
const axios = require('axios')
const DBHandler = require('./DBHandler')
router.get('/api/elements', function (req, res) {
try {
const elements = cache.get('elements')
if (elements) {
res.status(200).send(elements)
// refresh caches
return DBHandler.getElements()
} else {
const tempElements = cache.get('temp_elements')
if (tempElements) {
res.status(200).send(tempElements)
// refresh caches
return DBHandler.getElements()
} else {
// refresh caches
return DBHandler.getElements().then(({ data: elements }) => {
res.status(200).send(elements)
})
}
}
} catch (err) {
console.log(err)
res.status(500).send({ err: 'something went wrong' })
}
})
router.get('/api/isotopes', function (req, res) {
try {
const isotopes = cache.get('isotopes')
if (isotopes) {
res.status(200).send(isotopes)
// refresh caches
return DBHandler.getIsotopes()
} else {
const tempIsotopes = cache.get('temp_isotopes')
if (tempIsotopes) {
res.status(200).send(tempIsotopes)
// refresh caches
return DBHandler.getIsotopes()
} else {
// refresh caches
return DBHandler.getIsotopes().then(({ data: isotopes }) => {
res.status(200).send(isotopes)
})
}
}
} catch (err) {
console.log(err)
res.status(500).send({ err: 'something went wrong' })
}
})
router.get('/api/compounds/:formula', async function (req, res) {
const key = `compounds-${req.params.formula}`
try {
let compounds = cache.get(key)
if (!compounds) {
compounds = await DBHandler.findAllByFormula(req.params.formula)
cache.put(key, compounds, 60000)
}
res.status(200).send(compounds)
} catch (err) {
res.status(500).send({ err })
}
})
router.get('/api/compounds', function (req, res) {
try {
const compounds = cache.get('compounds')
if (compounds) {
res.status(200).send(compounds)
// refresh caches
return DBHandler.findAllCompounds()
} else {
const tempCompounds = cache.get('temp_compounds')
if (tempCompounds) {
res.status(200).send(tempCompounds)
// refresh caches
return DBHandler.findAllCompounds()
} else {
// refresh caches
return DBHandler.findAllCompounds().then(({ data: compounds }) => {
res.status(200).send(compounds)
})
}
}
} catch (err) {
console.log(err)
res.status(500).send({ err: 'something went wrong' })
}
})
router.get('/api/contents/:id', async function (req, res) {
try {
const result = await axios.get(`https://evrimagaci.org/api/v2/content/${req.params.id}`)
res.status(200).send(result.data)
} catch (err) {
console.log(err)
res.status(500).send({ err: 'something went wrong' })
}
})
router.get('/api/credits', async function (req, res) {
try {
const result = await axios.get(`https://evrimagaci.org/api/v2/periodum/credits`)
res.status(200).send(result.data)
} catch (err) {
res.status(500).send({ err: 'something went wrong' })
}
})
router.get('/api/about', async function (req, res) {
try {
const result = await axios.get(`https://evrimagaci.org/api/v2/content/11003`)
res.status(200).send(result.data.content)
} catch (err) {
res.status(500).send({ err: 'something went wrong' })
}
})
module.exports = router