This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
generated from ecomplus/express-app-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
68 lines (57 loc) · 1.97 KB
/
web.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
#!/usr/bin/env node
'use strict'
// log to files
const logger = require('console-files')
// handle app authentication to Store API
// https://github.com/ecomplus/application-sdk
const { ecomAuth, ecomServerIps } = require('@ecomplus/application-sdk')
// web server with Express
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const router = express.Router()
const port = process.env.PORT || 3000
app.use(bodyParser.urlencoded({ extended: false, limit: '1mb' }))
app.use(bodyParser.json({ limit: '2mb' }))
app.use((req, res, next) => {
if (req.url.startsWith('/ecom/')) {
// get E-Com Plus Store ID from request header
req.storeId = parseInt(req.get('x-store-id'), 10)
if (req.url.startsWith('/ecom/modules/')) {
// request from Mods API
// https://github.com/ecomclub/modules-api
const { body } = req
if (typeof body !== 'object' || body === null || !body.params || !body.application) {
return res.status(406).send('Request not comming from Mods API? Invalid body')
}
}
// on production check if request is comming from E-Com Plus servers
if (process.env.NODE_ENV === 'production' && ecomServerIps.indexOf(req.get('x-real-ip')) === -1) {
return res.status(403).send('Who are you? Unauthorized IP address')
}
}
// pass to the endpoint handler
// next Express middleware
next()
})
ecomAuth.then(appSdk => {
// setup app routes
const routes = './../routes'
router.get('/', require(`${routes}/`)())
;[
// routes for E-Com Plus APIs
'/ecom/auth-callback',
'/ecom/modules/apply-discount'
].forEach(route => router.post(route, require(`${routes}${route}`)(appSdk)))
// add router and start web server
app.use(router)
app.listen(port)
logger.log(`--> Starting web app on port :${port}`)
})
ecomAuth.catch(err => {
logger.error(err)
setTimeout(() => {
// destroy Node process while Store API auth cannot be handled
process.exit(1)
}, 1100)
})