Skip to content

Commit

Permalink
feat: setup web app with express
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jun 14, 2019
1 parent 1572d2a commit d128430
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/bin/web.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node

'use strict'

// log to files
const logger = require('console-files')
// handle app authentication to Store API
// https://github.com/ecomclub/ecomplus-app-sdk
const { ecomAuth, ecomServerIps } = require('ecomplus-app-sdk')

// web server with Express
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.PORT || 3000

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use((req, res, next) => {
if (req.baseUrl.startsWith('/ecom/') && process.env.NODE_ENV === 'production') {
// check if request is comming from E-Com Plus servers
if (ecomServerIps.indexOf(req.get('x-real-ip')) === -1) {
res.status(403).send('Who are you? Unauthorized IP address')
} else {
next()
}
} else {
// bypass
next()
}
})

ecomAuth.then(appSdk => {
// setup app routes
require('./../routes/')(appSdk)
})

app.listen(port)
logger.log(`--> Starting web app on port :${port}`)
1 change: 1 addition & 0 deletions app/routes/ecom/auth-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict'
1 change: 1 addition & 0 deletions app/routes/ecom/webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict'
19 changes: 19 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

// read app package.json
const pkg = require('./../package.json')
// setup Express router
const router = require('express').Router()

module.exports = appSdk => {
// base routes for E-Com Plus Store API
;[ 'auth-callback', 'webhook' ].forEach(route => {
let filename = `/ecom/${route}`
router.use(filename, require(`.${filename}`)(appSdk))
})

// show package info on domain root
router.get('/', (req, res) => res.send(pkg))

/* Add custom app routes here */
}

0 comments on commit d128430

Please sign in to comment.