-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
53 lines (45 loc) · 1.36 KB
/
main.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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const sqlite = require('sqlite3').verbose()
//
const { ecomAuth, setup } = require('ecomplus-app-sdk')
let appConfig = {}
if (process.argv[2] === '--config') {
appConfig = fs.readFileSync(process.argv[3], 'utf8')
appConfig = JSON.parse(appConfig)
} else {
console.log('\nflag --config not found \ntry: ecomplus-app-server --config /path/to/app-config.json')
process.exit(1)
}
setup(appConfig.ecom_auth_db)
ecomAuth.then(appSdk => {
// configure setup for stores
// list of procedures to save
appSdk.configureSetup((appConfig.procedures || null), (err, { storeId }) => {
if (!err) {
console.log('Setup store #' + storeId)
} else if (!err.appAuthRemoved) {
console.error(err.response.data)
}
})
})
ecomAuth.catch(err => {
console.error(err)
setTimeout(() => {
// destroy Node process while Store API auth cannot be handled
process.exit(1)
}, 1000)
})
const port = appConfig.port || 5000
// config db
const db = new sqlite.Database(appConfig.ecom_auth_db)
// middleware
const middleware = require('./lib/token')(appConfig)
app.use(bodyParser.json())
app.use(require('./lib/routes')({ ecomAuth, db, appConfig, middleware }))
app.listen(port)
console.log('Rodando em', port)