1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.stopServer = exports.startServer = exports.DEFAULT_TESTING_PORT = exports.DEFAULT_PORT = void 0;
|
9 |
|
10 | var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));
|
11 |
|
12 | var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));
|
13 |
|
14 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));
|
15 |
|
16 | var _safe = _interopRequireDefault(require("colors/safe"));
|
17 |
|
18 | var _exitHook = _interopRequireDefault(require("exit-hook"));
|
19 |
|
20 | var _env = require("@sharyn/env");
|
21 |
|
22 | var _checkSetup = require("@sharyn/check-setup");
|
23 |
|
24 |
|
25 |
|
26 | var Koa = (0, _checkSetup.hasPackage)('koa', true) && require("".concat(_checkSetup.appRoot, "/node_modules/koa"));
|
27 |
|
28 |
|
29 | var Router = (0, _checkSetup.hasPackage)('koa-router', true) && require("".concat(_checkSetup.appRoot, "/node_modules/koa-router"));
|
30 |
|
31 | var router = new Router();
|
32 |
|
33 | var PREFIX = _safe.default.cyan('[sharyn/koa]');
|
34 |
|
35 | var DEFAULT_PORT = 8020;
|
36 | exports.DEFAULT_PORT = DEFAULT_PORT;
|
37 | var DEFAULT_TESTING_PORT = 8021;
|
38 | exports.DEFAULT_TESTING_PORT = DEFAULT_TESTING_PORT;
|
39 | var port = _env.IS_TEST_ENV ? _env.TESTING_PORT || DEFAULT_TESTING_PORT : _env.PORT || DEFAULT_PORT;
|
40 | var hasDocker = (0, _checkSetup.hasFile)('docker-compose.yml');
|
41 | var server;
|
42 |
|
43 | var stopServer_ = function stopServer_(options) {
|
44 | if (server) {
|
45 | if (!((options === null || options === void 0 ? void 0 : options.silent) || _env.IS_TEST_ENV)) {
|
46 |
|
47 | console.log();
|
48 |
|
49 |
|
50 | console.log("".concat(PREFIX, " Server stopped"));
|
51 | }
|
52 |
|
53 | var sharynDb = (0, _checkSetup.requireSharyn)('db');
|
54 | sharynDb && sharynDb.knex.destroy();
|
55 | var sharynRedis = hasDocker && (0, _checkSetup.requireSharyn)('redis');
|
56 | sharynRedis && sharynRedis.quit();
|
57 | return server.close();
|
58 | }
|
59 |
|
60 | throw Error('Tried to stop the server but no server was running');
|
61 | };
|
62 |
|
63 | var startServer_ = function startServer_(manualRouting, options) {
|
64 | var routing = manualRouting || (0, _checkSetup.requireCascadeFromSource)('_server/routing.js', 'server/routing.js');
|
65 |
|
66 | if (!routing) {
|
67 | throw Error('You must pass a routing function to startServer, or have a _server/routing.js file exporting the routing function');
|
68 | }
|
69 |
|
70 | if (!((options === null || options === void 0 ? void 0 : options.silent) || _env.IS_TEST_ENV)) {
|
71 |
|
72 | console.log("\n".concat(PREFIX, " \uD83C\uDF39 Server running on PORT: ").concat(port, " | NODE_ENV: ").concat(_env.NODE_ENV, " | ENV_TYPE: ").concat(_env.ENV_TYPE));
|
73 | }
|
74 |
|
75 | (0, _exitHook.default)(function () {
|
76 | return stopServer_();
|
77 | });
|
78 | var app = new Koa();
|
79 |
|
80 | if ((0, _checkSetup.hasPackage)('koa-session')) {
|
81 | app.keys = [_env.SESSION_SECRET_KEY];
|
82 | }
|
83 |
|
84 | app.use(
|
85 |
|
86 | function () {
|
87 | var _ref = (0, _asyncToGenerator2.default)(
|
88 |
|
89 | _regenerator.default.mark(function _callee(ctx, next) {
|
90 | return _regenerator.default.wrap(function _callee$(_context) {
|
91 | while (1) {
|
92 | switch (_context.prev = _context.next) {
|
93 | case 0:
|
94 | _context.prev = 0;
|
95 | _context.next = 3;
|
96 | return next();
|
97 |
|
98 | case 3:
|
99 | _context.next = 10;
|
100 | break;
|
101 |
|
102 | case 5:
|
103 | _context.prev = 5;
|
104 | _context.t0 = _context["catch"](0);
|
105 | ctx.status = _context.t0.status || 500;
|
106 | ctx.redirect('/error');
|
107 | ctx.app.emit('error', _context.t0, ctx);
|
108 |
|
109 | case 10:
|
110 | case "end":
|
111 | return _context.stop();
|
112 | }
|
113 | }
|
114 | }, _callee, this, [[0, 5]]);
|
115 | }));
|
116 |
|
117 | return function (_x, _x2) {
|
118 | return _ref.apply(this, arguments);
|
119 | };
|
120 | }());
|
121 |
|
122 | if ((0, _checkSetup.hasPackage)('koa-sslify')) {
|
123 |
|
124 | var enforceHttps = require('koa-sslify');
|
125 |
|
126 | var hasHeroku = (0, _checkSetup.hasFile)('Procfile');
|
127 | _env.IS_LOCAL_ENV_TYPE || app.use(enforceHttps({
|
128 | trustProtoHeader: hasHeroku
|
129 | }));
|
130 | }
|
131 |
|
132 | if ((0, _checkSetup.hasPackage)('koa-session')) {
|
133 |
|
134 | var session = require('koa-session');
|
135 |
|
136 | var sessionOptions = {
|
137 | maxAge: 1000 * 60 * 60 * 24 * 14,
|
138 |
|
139 | rolling: true
|
140 | };
|
141 | var sharynRedis = hasDocker && (0, _checkSetup.requireSharyn)('redis');
|
142 |
|
143 | if (sharynRedis) {
|
144 | sessionOptions.store = {
|
145 | get: function () {
|
146 | var _get = (0, _asyncToGenerator2.default)(
|
147 |
|
148 | _regenerator.default.mark(function _callee2(key) {
|
149 | return _regenerator.default.wrap(function _callee2$(_context2) {
|
150 | while (1) {
|
151 | switch (_context2.prev = _context2.next) {
|
152 | case 0:
|
153 | _context2.t0 = JSON;
|
154 | _context2.next = 3;
|
155 | return sharynRedis.getAsync("session:".concat(key));
|
156 |
|
157 | case 3:
|
158 | _context2.t1 = _context2.sent;
|
159 | return _context2.abrupt("return", _context2.t0.parse.call(_context2.t0, _context2.t1));
|
160 |
|
161 | case 5:
|
162 | case "end":
|
163 | return _context2.stop();
|
164 | }
|
165 | }
|
166 | }, _callee2, this);
|
167 | }));
|
168 |
|
169 | return function get(_x3) {
|
170 | return _get.apply(this, arguments);
|
171 | };
|
172 | }(),
|
173 | set: function set(key, sess) {
|
174 | return sharynRedis.setAsync("session:".concat(key), (0, _stringify.default)(sess));
|
175 | },
|
176 | destroy: function destroy(key) {
|
177 | return sharynRedis.delAsync(key);
|
178 | }
|
179 | };
|
180 | }
|
181 |
|
182 | app.use(session(sessionOptions, app));
|
183 | }
|
184 |
|
185 | if ((0, _checkSetup.hasPackage)('koa-body')) {
|
186 |
|
187 | app.use(require('koa-body')({
|
188 | multipart: true
|
189 | }));
|
190 | }
|
191 |
|
192 | if ((0, _checkSetup.hasPackage)('koa-compress')) {
|
193 |
|
194 | app.use(require('koa-compress')());
|
195 | }
|
196 |
|
197 | if ((0, _checkSetup.hasPackage)('koa-mount') && (0, _checkSetup.hasPackage)('koa-static')) {
|
198 |
|
199 | var mount = require('koa-mount');
|
200 |
|
201 |
|
202 | var serveStatic = require('koa-static');
|
203 |
|
204 | app.use(mount('/static', serveStatic('dist'))).use(mount('/static', serveStatic('public'))).use(mount('/', serveStatic('public/root')));
|
205 | }
|
206 |
|
207 | if ((0, _checkSetup.hasPackage)('koa-favicon')) {
|
208 | app.use(
|
209 | require('koa-favicon')("./".concat((0, _checkSetup.pathCascade)('public/img/favicon/favicon.ico', 'public/img/favicon.ico', 'public/favicon.ico'))));
|
210 | }
|
211 |
|
212 | if ((0, _checkSetup.hasPackage)('apollo-server-koa')) {
|
213 | var _ref2;
|
214 |
|
215 |
|
216 | var _require = require('apollo-server-koa'),
|
217 | ApolloServer = _require.ApolloServer;
|
218 |
|
219 | var apolloServer = new ApolloServer((_ref2 = options === null || options === void 0 ? void 0 : options.apolloConfig) !== null && _ref2 !== void 0 ? _ref2 : {});
|
220 | apolloServer.applyMiddleware({
|
221 | app: app
|
222 | });
|
223 | }
|
224 |
|
225 | routing(router);
|
226 | app.use(router.routes()).use(router.allowedMethods());
|
227 | app.on('error', function (err) {
|
228 | return console.error(err);
|
229 | });
|
230 | server = app.listen((options === null || options === void 0 ? void 0 : options.port) || port);
|
231 | };
|
232 |
|
233 | var startServer = startServer_;
|
234 | exports.startServer = startServer;
|
235 | var stopServer = stopServer_;
|
236 | exports.stopServer = stopServer; |
\ | No newline at end of file |