|
| 1 | +require("regenerator-runtime"); |
| 2 | +const restify = require("restify"); |
| 3 | +const server = restify.createServer(); |
| 4 | +// const models = require("./src/models"); |
| 5 | +// const { setupRoutes } = require("./src/routes"); |
| 6 | +const { loadConfiguration, getLogger } = require("./src/common"); |
| 7 | +const { setupRoutes } = require("./src/routes"); |
| 8 | +const corsMiddleware = require("restify-cors-middleware"); |
| 9 | +const log = getLogger(); |
| 10 | + |
| 11 | +(async () => { |
| 12 | + let configuration; |
| 13 | + try { |
| 14 | + configuration = await loadConfiguration(); |
| 15 | + } catch (error) { |
| 16 | + console.error("configuration.json not found - stopping now"); |
| 17 | + process.exit(); |
| 18 | + } |
| 19 | + // await models.sequelize.sync(); |
| 20 | + |
| 21 | + const cors = corsMiddleware({ |
| 22 | + preflightMaxAge: 5, //Optional |
| 23 | + origins: ["*"], |
| 24 | + allowHeaders: [ |
| 25 | + "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization", |
| 26 | + ], |
| 27 | + exposeHeaders: [ |
| 28 | + "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization", |
| 29 | + ], |
| 30 | + }); |
| 31 | + server.pre(cors.preflight); |
| 32 | + server.use(cors.actual); |
| 33 | + if (process.env.NODE_ENV === "development") { |
| 34 | + server.use((req, res, next) => { |
| 35 | + log.debug(`${req.route.method}: ${req.route.path}`); |
| 36 | + return next(); |
| 37 | + }); |
| 38 | + } |
| 39 | + server.use(restify.plugins.dateParser()); |
| 40 | + server.use(restify.plugins.queryParser()); |
| 41 | + server.use(restify.plugins.jsonp()); |
| 42 | + server.use(restify.plugins.gzipResponse()); |
| 43 | + server.use( |
| 44 | + restify.plugins.bodyParser({ |
| 45 | + maxBodySize: 0, |
| 46 | + mapParams: true, |
| 47 | + mapFiles: false, |
| 48 | + overrideParams: false, |
| 49 | + multiples: true, |
| 50 | + hash: "sha1", |
| 51 | + rejectUnknown: true, |
| 52 | + requestBodyOnGet: false, |
| 53 | + reviver: undefined, |
| 54 | + maxFieldsSize: 2 * 1024 * 1024, |
| 55 | + }) |
| 56 | + ); |
| 57 | + setupRoutes({ server }); |
| 58 | + // const configuration = { |
| 59 | + // api: { |
| 60 | + // port: "8080", |
| 61 | + // }, |
| 62 | + // }; |
| 63 | + |
| 64 | + const app = server.listen(configuration.api.port, function () { |
| 65 | + console.log("ready on %s", server.url); |
| 66 | + }); |
| 67 | +})(); |
0 commit comments