Commit 01545d19301e3762256643a264bfefb16fd93310
1 parent
057b3f4f52
Exists in
master
Generate json products
Showing 3 changed files with 79 additions and 0 deletions Inline Diff
lib/db-evo.js
File was created | 1 | const mysql = require('mysql'); | |
2 | |||
3 | const config = require('komodo-sdk/config'); | ||
4 | |||
5 | exports.ERROR_POOL_NOT_READY = new Error('DB-EVO: pool is not ready'); | ||
6 | |||
7 | exports.pool = config.mysql ? mysql.createPool({ | ||
8 | host: config.mysql.evo.host || 'localhost', | ||
9 | database: config.mysql.evo.database || 'komodo', | ||
10 | user: config.mysql.evo.user || 'komodo', | ||
11 | password: config.mysql.evo.password, | ||
12 | }) : null; | ||
13 | |||
14 | exports.query = (query, values, cb) => { | ||
15 | // pool.query.apply(null, arguments); | ||
16 | if (!this.pool || !this.pool.query) { | ||
17 | if (typeof cb === 'function') { | ||
18 | cb(this.ERROR_POOL_NOT_READY); | ||
19 | } | ||
20 | |||
21 | return; | ||
22 | } | ||
23 | |||
24 | this.pool.query(query, values, cb); | ||
25 | }; | ||
26 | |||
27 | exports.format = (sql, values, cb) => new Promise((resolve, reject) => { | ||
28 | if (!this.pool) { | ||
29 | reject(this.ERROR_POOL_NOT_READY); | ||
30 | if (typeof cb === 'function') cb(this.ERROR_POOL_NOT_READY); | ||
31 | return; | ||
32 | } | ||
33 | |||
34 | this.pool.getConnection((err, connection) => { | ||
35 | if (err) { | ||
36 | reject(err); | ||
37 | if (typeof cb === 'function') cb(err); | ||
38 | return; | ||
39 | } | ||
40 | |||
41 | const formatted = connection.format(sql, values); | ||
42 | connection.release(); | ||
43 | |||
44 | resolve(formatted); | ||
45 | if (typeof cb === 'function') cb(null, formatted); | ||
46 | }); | ||
47 | }); | ||
48 |
lib/webservice/index.js
1 | const CLUSTER_MODE = false; | 1 | const CLUSTER_MODE = false; |
2 | const cluster = require('cluster'); | 2 | const cluster = require('cluster'); |
3 | const numCPUs = require('os').cpus().length; | 3 | const numCPUs = require('os').cpus().length; |
4 | const express = require('express'); | 4 | const express = require('express'); |
5 | 5 | ||
6 | const config = require('komodo-sdk/config'); | 6 | const config = require('komodo-sdk/config'); |
7 | const logger = require('komodo-sdk/logger'); | 7 | const logger = require('komodo-sdk/logger'); |
8 | 8 | ||
9 | const app = express(); | 9 | const app = express(); |
10 | const listenPort = config.webservice.port || config.webservice.listen_port; | 10 | const listenPort = config.webservice.port || config.webservice.listen_port; |
11 | 11 | ||
12 | const routerPriceplan = require('./router/priceplan'); | 12 | const routerPriceplan = require('./router/priceplan'); |
13 | const routerProduct = require('./router/product'); | ||
13 | 14 | ||
14 | app.use('/apikey/:apikey/priceplan', routerPriceplan); | 15 | app.use('/apikey/:apikey/priceplan', routerPriceplan); |
16 | app.use('/apikey/:apikey/products', routerProduct); | ||
15 | 17 | ||
16 | if (CLUSTER_MODE && cluster.isMaster) { | 18 | if (CLUSTER_MODE && cluster.isMaster) { |
17 | logger.info(`Master ${process.pid} is running`); | 19 | logger.info(`Master ${process.pid} is running`); |
18 | 20 | ||
19 | // Fork workers. | 21 | // Fork workers. |
20 | for (let i = 0; i < numCPUs; i += 1) { | 22 | for (let i = 0; i < numCPUs; i += 1) { |
21 | cluster.fork(); | 23 | cluster.fork(); |
22 | } | 24 | } |
23 | 25 | ||
24 | cluster.on('exit', (worker) => { | 26 | cluster.on('exit', (worker) => { |
25 | logger.info(`worker ${worker.process.pid} died`); | 27 | logger.info(`worker ${worker.process.pid} died`); |
26 | }); | 28 | }); |
27 | } else { | 29 | } else { |
28 | app.listen(listenPort, () => { | 30 | app.listen(listenPort, () => { |
29 | logger.info(`WEBSERVICE ${process.pid} listen on ${listenPort}`); | 31 | logger.info(`WEBSERVICE ${process.pid} listen on ${listenPort}`); |
30 | }); | 32 | }); |
31 | } | 33 | } |
32 | 34 |
lib/webservice/router/product.js
File was created | 1 | const express = require('express'); | |
2 | const logger = require('komodo-sdk/logger'); | ||
3 | const dbEvo = require('../../db-evo'); | ||
4 | |||
5 | const router = express.Router(); | ||
6 | module.exports = router; | ||
7 | |||
8 | function pageJson(req, res) { | ||
9 | const query = ` | ||
10 | SELECT | ||
11 | UPPER(service.keyword) AS name | ||
12 | FROM product | ||
13 | LEFT JOIN service ON service.svc_id = product.svc_id | ||
14 | WHERE | ||
15 | service.svc_cat IN (1, 6) | ||
16 | ORDER BY SOUNDEX(service.keyword), LENGTH(service.keyword), service.keyword; | ||
17 | `.trim(); | ||
18 | |||
19 | dbEvo.query(query, [], (err, result) => { | ||
20 | if (err) { | ||
21 | logger.warn(`ROUTER-PRODUCTS: DB error. ${err.toString()}`); | ||
22 | } | ||
23 | |||
24 | const products = (result || []).map((item) => item.name); | ||
25 | |||
26 | res.json(products); | ||
27 | }); | ||
28 | } | ||
29 | |||
30 | router.get('/json', pageJson); | ||
31 |