-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
155 lines (119 loc) · 3.72 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
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
const path = require('path');
const express = require('express');
const _ = require('lodash')
const { getClosestPaints, getBrandsCount, searchPaints, getPaintById, getAllPaints, updatePaint, createPaint, deletePaint } = require('./services/paints')
const { isValidHexColor } = require('./services/paints/utils')
const authConfig = require('./authConfig')
const brandsOrder = [
'Citadel',
'Vallejo Game Color',
'Vallejo Model Color',
'P3 Formula'
]
const isAuthenticated = authheader => {
// -----------------------------------------------------------------------
// authentication middleware
const auth = authConfig
// parse login and password from headers
const b64auth = (authheader || '').split(' ')[1] || ''
const [login, password] = new Buffer(b64auth, 'base64').toString().split(':')
// Verify login and password are set and correct
if (!login || !password || login !== auth.login || password !== auth.password) {
return false
}
return true
}
const validatePaint = (paint, withId) => {
console.log(paint);
if (!paint.name || !paint.brand || !paint.type) return false
if (!isValidHexColor(paint.hex)) return false
if (withId && !paint._id) return false
return true
}
module.exports = app => {
// API calls
app.get('/api/closestPaints/:hex', async (req, res) => {
let paints = await getClosestPaints(req.params.hex)
paints = _.sortBy(paints, ({ paint }, i) => {
return brandsOrder.indexOf(paint.brand)
})
res.send(paints)
});
app.get('/api/brands/count', async (req, res) => {
const brandsCount = await getBrandsCount()
res.send({ count: brandsCount })
})
app.get('/api/paints/search', async (req, res) => {
const paints = await searchPaints(req.query.query)
res.send(paints)
})
app.get('/api/paints', async (req, res) => {
const paints = await getAllPaints()
res.send(paints)
})
app.get('/api/paints/:id', async (req, res) => {
const paint = await getPaintById(req.params.id)
res.send(paint)
})
app.get('/api/admin/isauthenticated', async (req, res) => {
const authenticated = await isAuthenticated(req.headers.authorization)
if (!authenticated) {
res.status(401)
res.send(false)
return
}
res.send(true)
})
app.post('/api/admin/paints', async (req, res) => {
const authenticated = await isAuthenticated(req.headers.authorization)
if (!authenticated) {
res.status(401)
res.send(false)
return
}
const paint = req.body
if (!validatePaint(paint, true)) {
res.status(500)
res.end()
return
}
const createdPaint = await createPaint(paint)
res.send(createdPaint)
})
app.post('/api/admin/paints/:id', async (req, res) => {
const authenticated = await isAuthenticated(req.headers.authorization)
if (!authenticated) {
res.status(401)
res.send(false)
return
}
const id = req.params.id
const paint = req.body
if (!validatePaint(paint)) {
res.status(500)
res.end()
return
}
const updatedPaint = await updatePaint(id, paint)
res.send(updatedPaint)
})
app.delete('/api/admin/paints/:id', async (req, res) => {
const authenticated = await isAuthenticated(req.headers.authorization)
if (!authenticated) {
res.status(401)
res.send(false)
return
}
const id = req.params.id
await deletePaint(id)
res.send(true)
})
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
}