UNPKG

16.7 kBJavaScriptView Raw
1'use strict';
2
3var _typeof2 = require('babel-runtime/helpers/typeof');
4
5var _typeof3 = _interopRequireDefault(_typeof2);
6
7var _regenerator = require('babel-runtime/regenerator');
8
9var _regenerator2 = _interopRequireDefault(_regenerator);
10
11var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
12
13var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
14
15var _extends2 = require('babel-runtime/helpers/extends');
16
17var _extends3 = _interopRequireDefault(_extends2);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _init = function () {
28 var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8() {
29 var _configs, keys, apis, mapper, server;
30
31 return _regenerator2.default.wrap(function _callee8$(_context8) {
32 while (1) {
33 switch (_context8.prev = _context8.next) {
34 case 0:
35 _context8.prev = 0;
36
37 AKSHOOKS.append({
38 entry: this,
39 context: this
40 });
41 _configs = this.configs, keys = _configs.keys, apis = _configs.apis, mapper = _configs.mapper;
42
43 app.keys = this.configs.keys;
44
45 if (apis.list) {
46 _context8.next = 6;
47 break;
48 }
49
50 throw new Error('api 列表必须封装为list的子属性');
51
52 case 6:
53 if (!(!mapper.js || !mapper.css)) {
54 _context8.next = 8;
55 break;
56 }
57
58 throw new Error('请将静态资源列表分别配置作为mapper.js和mapper.css的子元素');
59
60 case 8:
61 _context8.next = 10;
62 return core.call(this, app, this.configs);
63
64 case 10:
65 server = _context8.sent;
66 return _context8.abrupt('return', server);
67
68 case 14:
69 _context8.prev = 14;
70 _context8.t0 = _context8['catch'](0);
71
72 console.log(_context8.t0.stack);
73
74 case 17:
75 case 'end':
76 return _context8.stop();
77 }
78 }
79 }, _callee8, this, [[0, 14]]);
80 }));
81
82 return function _init() {
83 return _ref8.apply(this, arguments);
84 };
85}();
86
87function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
88
89var fs = require('fs');
90var Koa = require('koa');
91var glob = require('glob');
92var md5 = require('blueimp-md5');
93var render = require('koa-art-template');
94var _statics = require('koa-static-cache');
95var _bodyparser = require('koa-bodyparser');
96require('aotoo');
97require('aotoo-web-widgets');
98var fkpcore = require('./fkpcore');
99var fetch = require('./fkpcore/modules/fetch');
100var cache = require('./fkpcore/modules/cache');
101var core = fkpcore.core;
102var fkp = fkpcore.fkp;
103var aks = fkp;
104
105ReactDom = require('react-dom/server');
106var AKSHOOKS = SAX('AOTOO-KOA-SERVER');
107global.ReactDomServer = ReactDom;
108global.AotooServerHooks = AKSHOOKS;
109Aotoo.render = function () {
110 AKSHOOKS.emit('AotooClassDestory'); // 演示2.5秒
111 return ReactDomServer.renderToString.apply(null, arguments);
112};
113Aotoo.html = function () {
114 AKSHOOKS.emit('AotooClassDestory');
115 return ReactDomServer.renderToStaticMarkup.apply(null, arguments);
116};
117
118var app = new Koa();
119var DEFAULTCONFIGS = {
120 mapper: {
121 js: {},
122 css: {},
123 public: {
124 js: '/js',
125 css: '/css'
126 }
127 },
128
129 apis: {
130 list: {}
131 },
132
133 fetchOptions: {
134 headers: {},
135 timeout: 100000
136 },
137
138 cacheOptions: {
139 max: 300,
140 length: function length(n, key) {
141 return n * 2 + key.length;
142 },
143 dispose: function dispose(key, value) {},
144 maxAge: 2 * 60 * 60 * 1000
145 },
146
147 bodyOptions: null,
148
149 routerOptions: {
150 allMethods: ['get', 'post', 'put', 'del'],
151 parameters: {
152 get: ['/', '/:cat', '/:cat/:title', '/:cat/:title/:id'],
153 post: ['/', '/:cat', '/:cat/:title', '/:cat/:title/:id']
154 },
155 prefixes: {}
156 }
157};
158
159var aotooServer = function () {
160 function aotooServer() {
161 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
162 (0, _classCallCheck3.default)(this, aotooServer);
163
164 this.middlewares = [];
165 this._public = {
166 js: '/js',
167 css: '/css'
168 };
169
170 opts = _.merge({}, DEFAULTCONFIGS, opts);
171
172 this.configs = {
173 keys: opts.keys || ['aotoo koa'], // cookie session关键字
174 index: opts.index || 'index', // 默认首页
175
176 apis: opts.apis, // api接口集合
177 mapper: opts.mapper, // 静态资源映射文件
178
179 fetchOptions: opts.fetchOptions,
180 cacheOptions: opts.cacheOptions,
181 bodyOptions: opts.bodyOptions,
182 routerOptions: opts.routerOptions,
183
184 root: opts.root, // 渲染默认目录
185 pages: opts.pages || opts.pagesFolder || opts.controls, // control层文件夹,必须
186 pluginsFolder: opts.pluginsFolder // 插件文件夹
187 };
188
189 this.state = {
190 views: false,
191 bodyparser: false,
192 status: false
193 };
194
195 if (this.configs.bodyOptions) {
196 this.state.bodyparser = true;
197 app.use(_bodyparser(this.configs.bodyOptions));
198 }
199
200 // 传入apis
201 global.Fetch = this.fetch = fetch((0, _extends3.default)({ apis: this.configs.apis }, this.fetchOptions));
202 global.Cache = this.cache = cache(this.configs.cacheOptions);
203 global.Md5 = md5;
204
205 if (this.configs.mapper) {
206 var _public = void 0;
207 var mapper = this.configs.mapper;
208 if (mapper.public) {
209 _public = mapper.public;
210 // delete mapper.public
211 }
212 if (_public) {
213 this._public = _public;
214 Aotoo.inject.public = _public;
215 }
216 Aotoo.inject.mapper = mapper;
217 }
218
219 this.on = function (name, cb) {
220 if (name === 'error') app.on(name, cb);
221 AKSHOOKS.on(name, cb);
222 };
223 this.one = AKSHOOKS.one.bind(AKSHOOKS);
224 this.off = AKSHOOKS.off.bind(AKSHOOKS);
225 this.emit = AKSHOOKS.emit.bind(AKSHOOKS);
226 this.hasOn = AKSHOOKS.hasOn.bind(AKSHOOKS);
227 this.append = AKSHOOKS.append.bind(AKSHOOKS);
228 this.set = AKSHOOKS.set.bind(AKSHOOKS);
229 this.get = AKSHOOKS.get.bind(AKSHOOKS);
230 }
231
232 (0, _createClass3.default)(aotooServer, [{
233 key: 'setFetchOptions',
234 value: function setFetchOptions(opts) {
235 DEFAULTCONFIGS.fetchOptions.apis = this.fetch.apis;
236 var _opts = _.merge({}, DEFAULTCONFIGS.fetchOptions, opts);
237 this.configs.fetchOptions = _opts;
238 global.Fetch = this.fetch = fetch(_opts);
239 }
240 }, {
241 key: 'setCacheOptions',
242 value: function setCacheOptions(opts) {
243 var _opts = _.merge({}, DEFAULTCONFIGS.cacheOptions, opts);
244 this.configs.cacheOptions = _opts;
245 global.Cache = this.cache = cache(_opts);
246 }
247 }, {
248 key: 'setRouterOptions',
249 value: function setRouterOptions(opts) {
250 var _opts = _.merge({}, DEFAULTCONFIGS.routerOptions, opts);
251 this.configs.routerOptions = _opts;
252 }
253 }, {
254 key: 'setRouterPrefixes',
255 value: function setRouterPrefixes(opts) {
256 if (!this.state.status) {
257 var _opts = _.merge({}, this.configs.routerOptions.prefixes, opts);
258 this.configs.routerOptions.prefixes = _opts;
259 } else {
260 console.log('===========');
261 console.log('初始化状态才能设置前缀路由');
262 }
263 }
264 }, {
265 key: 'public',
266 value: function _public(opts) {
267 this._public = opts;
268 Aotoo.inject.public = opts;
269 }
270
271 // 注册KOA2的中间间,与KOA2语法保持一致
272
273 }, {
274 key: 'use',
275 value: function () {
276 var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(midw) {
277 return _regenerator2.default.wrap(function _callee$(_context) {
278 while (1) {
279 switch (_context.prev = _context.next) {
280 case 0:
281 app.use(midw);
282
283 case 1:
284 case 'end':
285 return _context.stop();
286 }
287 }
288 }, _callee, this);
289 }));
290
291 function use(_x2) {
292 return _ref.apply(this, arguments);
293 }
294
295 return use;
296 }()
297
298 // 注册一个Aotoo插件方法
299
300 }, {
301 key: 'plugins',
302 value: function plugins(name, fn) {
303 aks.plugins(name, fn);
304 }
305
306 // 注册一个Aotoo助手方法
307
308 }, {
309 key: 'utile',
310 value: function utile(name, fn) {
311 aks.utileHand(name, fn);
312 }
313 }, {
314 key: 'callback',
315 value: function callback() {
316 return app.callback(arguments);
317 }
318
319 // 指定站点静态路径,如 /images, /uploader, /user
320
321 }, {
322 key: 'statics',
323 value: function () {
324 var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(dist, opts, files) {
325 var dft;
326 return _regenerator2.default.wrap(function _callee2$(_context2) {
327 while (1) {
328 switch (_context2.prev = _context2.next) {
329 case 0:
330 dft = {
331 dynamic: false,
332 buffer: false,
333 gzip: false
334 };
335
336 dft = _.merge(dft, opts);
337
338 app.use(_statics(dist, dft, files));
339
340 case 3:
341 case 'end':
342 return _context2.stop();
343 }
344 }
345 }, _callee2, this);
346 }));
347
348 function statics(_x3, _x4, _x5) {
349 return _ref2.apply(this, arguments);
350 }
351
352 return statics;
353 }()
354
355 // 注册api接口集,用于做接口层的数据访问
356
357 }, {
358 key: 'apis',
359 value: function () {
360 var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
361 var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
362 return _regenerator2.default.wrap(function _callee3$(_context3) {
363 while (1) {
364 switch (_context3.prev = _context3.next) {
365 case 0:
366 if ((typeof obj === 'undefined' ? 'undefined' : (0, _typeof3.default)(obj)) == 'object') {
367 this.configs.apis = obj;
368 }
369
370 case 1:
371 case 'end':
372 return _context3.stop();
373 }
374 }
375 }, _callee3, this);
376 }));
377
378 function apis() {
379 return _ref3.apply(this, arguments);
380 }
381
382 return apis;
383 }()
384
385 // 注册POST中间件,可以通过 ctx.bodys来访问post数据
386
387 }, {
388 key: 'bodyparser',
389 value: function () {
390 var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
391 var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
392 return _regenerator2.default.wrap(function _callee4$(_context4) {
393 while (1) {
394 switch (_context4.prev = _context4.next) {
395 case 0:
396 if (!this.state.bodyparser && (typeof obj === 'undefined' ? 'undefined' : (0, _typeof3.default)(obj)) == 'object') {
397 this.state.bodyparser = true;
398 app.use(_bodyparser(obj));
399 }
400
401 case 1:
402 case 'end':
403 return _context4.stop();
404 }
405 }
406 }, _callee4, this);
407 }));
408
409 function bodyparser() {
410 return _ref4.apply(this, arguments);
411 }
412
413 return bodyparser;
414 }()
415
416 // 注册渲染方法
417
418 }, {
419 key: 'views',
420 value: function () {
421 var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(dist, opts) {
422 var dft, _views, distState;
423
424 return _regenerator2.default.wrap(function _callee5$(_context5) {
425 while (1) {
426 switch (_context5.prev = _context5.next) {
427 case 0:
428 // import views from 'koa-views' // 放到顶部
429 // let dft = {
430 // map: {
431 // html: (opts&&opts.html||'ejs')
432 // }
433 // }
434 // if (opts&&opts.extension) {
435 // dft.extension = opts.extension
436 // }
437 // if (opts&&opts.options) {
438 // dft.options = opts.options
439 // }
440 // this.state.views = true
441 // app.use( views(dist, dft) )
442
443 dft = {
444 root: dist,
445 extname: '.html',
446 debug: process.env.NODE_ENV !== 'production'
447 };
448
449
450 if (typeof opts == 'function') {
451 opts = {
452 render: opts
453 };
454 }
455
456 dft = _.merge({}, dft, opts);
457
458 if (dist) {
459 _views = [];
460
461 this.state.viewsRoot = dist;
462 if (fs.existsSync(dist)) {
463 distState = fs.statSync(dist);
464
465 if (distState.isDirectory()) {
466 // glob.sync(dist + '/**/*.html').forEach(function (item) {
467 glob.sync(dist + '/**/*.html').forEach(function (item) {
468 _views.push(item);
469 });
470 }
471 }
472 this.state.views = _views;
473 }
474 if (dft.render && typeof dft.render == 'function') {
475 app.use(dft.render);
476 } else {
477 render(app, dft);
478 }
479
480 case 5:
481 case 'end':
482 return _context5.stop();
483 }
484 }
485 }, _callee5, this);
486 }));
487
488 function views(_x8, _x9) {
489 return _ref5.apply(this, arguments);
490 }
491
492 return views;
493 }()
494
495 // 初始化
496
497 }, {
498 key: 'init',
499 value: function () {
500 var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() {
501 return _regenerator2.default.wrap(function _callee6$(_context6) {
502 while (1) {
503 switch (_context6.prev = _context6.next) {
504 case 0:
505 _context6.prev = 0;
506
507 if (this.configs.pages) {
508 _context6.next = 3;
509 break;
510 }
511
512 throw new Error('控制器目录没有指定');
513
514 case 3:
515 if (this.state.views) {
516 _context6.next = 9;
517 break;
518 }
519
520 if (this.configs.root) {
521 _context6.next = 8;
522 break;
523 }
524
525 throw new Error('koa的模板解析引擎没有配置且需设置app.state.views=true; app.state.viewsRoot=HTMLDIST');
526
527 case 8:
528 this.views(this.configs.root);
529
530 case 9:
531 if (!this.state.bodyparser) {
532 app.use(_bodyparser());
533 }
534 _context6.next = 12;
535 return _init.call(this);
536
537 case 12:
538 return _context6.abrupt('return', _context6.sent);
539
540 case 15:
541 _context6.prev = 15;
542 _context6.t0 = _context6['catch'](0);
543
544 console.error(_context6.t0);
545
546 case 18:
547 case 'end':
548 return _context6.stop();
549 }
550 }
551 }, _callee6, this, [[0, 15]]);
552 }));
553
554 function init() {
555 return _ref6.apply(this, arguments);
556 }
557
558 return init;
559 }()
560 }, {
561 key: 'listen',
562 value: function () {
563 var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(port, dom, cb) {
564 var server;
565 return _regenerator2.default.wrap(function _callee7$(_context7) {
566 while (1) {
567 switch (_context7.prev = _context7.next) {
568 case 0:
569 _context7.next = 2;
570 return this.init();
571
572 case 2:
573 server = _context7.sent;
574
575 this.state.status = 'running';
576 server.listen(port, dom, cb);
577
578 case 5:
579 case 'end':
580 return _context7.stop();
581 }
582 }
583 }, _callee7, this);
584 }));
585
586 function listen(_x10, _x11, _x12) {
587 return _ref7.apply(this, arguments);
588 }
589
590 return listen;
591 }()
592 }]);
593 return aotooServer;
594}();
595
596module.exports = function (opts) {
597 return new aotooServer(opts);
598};
599//# sourceMappingURL=maps/server.js.map