1 | var koa = require('koa');
|
2 | var router = require('koa-router')
|
3 | var merge = require('merge-descriptors');
|
4 | var session = require('koa-session');
|
5 | var koa_body = require('koa-body');
|
6 |
|
7 |
|
8 |
|
9 | module.exports = Server;
|
10 | Server.prototype.__proto__ = new koa()
|
11 | function Server (options,app){
|
12 | this.port = options.port;
|
13 | this.app = app
|
14 | this.serverStartupMiddleware()
|
15 |
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Server.prototype.loader = function(app){
|
22 | var Application = require('./application');
|
23 | if(!(app instanceof Application)){
|
24 | console.error('请传入alajs() 产生的对象');
|
25 | throw new Error();
|
26 | }
|
27 | app.server = this;
|
28 |
|
29 | this.setMiddleware()
|
30 |
|
31 | app.load();
|
32 |
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | Server.prototype.setMiddleware = function(){
|
38 |
|
39 | this.use(router(this))
|
40 |
|
41 | }
|
42 |
|
43 | Server.prototype.serverStartupMiddleware = function(){
|
44 | this.keys = [this.app.name];
|
45 |
|
46 |
|
47 | this.use(koa_body());
|
48 | this.use(function *(next) {
|
49 | this.post = this.request.body;
|
50 | yield next;
|
51 | });
|
52 |
|
53 | this.use(session());
|
54 | this.extendFromContext();
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 | Server.prototype.extendFromContext = function(){
|
60 | var app = this.app;
|
61 | var contextEditer = require('./context')
|
62 | this.use(function *(next){
|
63 | contextEditer(this,app);
|
64 | yield next
|
65 | })
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | Server.prototype.bootstrap = function(){
|
71 | this.listen(this.port);
|
72 | }
|
73 |
|
74 |
|
75 |
|