1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 |
|
9 | var path = require("path");
|
10 | var util = require("util");
|
11 |
|
12 | var Ero = require("ero");
|
13 |
|
14 | var Model = require("./loader/model.js");
|
15 | var Connection = require("./loader/connection");
|
16 | var Controller = require("./loader/controller");
|
17 | var Express = require("./_express/application");
|
18 | var Logger = require("./logger");
|
19 | var Plugin = require("./loader/plugin");
|
20 | var Service = require("./loader/service");
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var Akyuu = function(projectRoot) {
|
28 | Express.call(this);
|
29 |
|
30 | this.projectRoot = projectRoot || path.resolve(__dirname, "../../../src");
|
31 | this.requirePaths = require("./require");
|
32 | this.requirePaths.addRequirePath(this.projectRoot);
|
33 |
|
34 | this.config = require("./config");
|
35 | this.logger = new Logger(this);
|
36 |
|
37 | this.ero = new Ero({
|
38 | template: require("./error/template"),
|
39 | definitions: require("./error/predefinition")
|
40 | });
|
41 | this.Errors = this.ero.Errors;
|
42 |
|
43 | this.Errors.define = this.ero.defineError.bind(this.ero);
|
44 |
|
45 | this.PLUGIN_POS = Plugin.POS;
|
46 |
|
47 | this.plugin = new Plugin(this);
|
48 | this.connection = new Connection(this);
|
49 | this.model = new Model(this);
|
50 | this.controller = new Controller(this);
|
51 | this.service = new Service(this);
|
52 | };
|
53 |
|
54 | util.inherits(Akyuu, Express);
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | Akyuu.prototype.init = function(callback) {
|
61 | Express.prototype.init.call(this);
|
62 |
|
63 | this.logger.load();
|
64 | this._logger = this.logger.get("akyuu");
|
65 |
|
66 |
|
67 | var errors = this.config.errors || {};
|
68 | for(var key in errors) {
|
69 | if(!errors.hasOwnProperty(key)) continue;
|
70 | this.Errors.define(errors[key], key);
|
71 | }
|
72 |
|
73 | this.plugin.load();
|
74 |
|
75 | this.plugin.plug(this.PLUGIN_POS.BEFORE_SERVICE);
|
76 | this.service.load("");
|
77 | this.plugin.plug(this.PLUGIN_POS.AFTER_SERVICE);
|
78 |
|
79 | this.plugin.plug(this.PLUGIN_POS.BEFORE_CONNECTION);
|
80 | this.connection.load();
|
81 | this.plugin.plug(this.PLUGIN_POS.AFTER_CONNECTION);
|
82 |
|
83 | this.plugin.plug(this.PLUGIN_POS.BEFORE_MODEL);
|
84 | this.model.load("");
|
85 | this.plugin.plug(this.PLUGIN_POS.AFTER_MODEL);
|
86 |
|
87 | this.plugin.plug(this.PLUGIN_POS.BEFORE_CONTROLLER);
|
88 | this.controller.load("");
|
89 | this.plugin.plug(this.PLUGIN_POS.AFTER_CONTROLLER);
|
90 |
|
91 | this._logger.info("Akyuu.js initialized.");
|
92 |
|
93 | process.nextTick(callback);
|
94 | };
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | Akyuu.prototype.start = function() {
|
100 | var server = this.config.server || {};
|
101 |
|
102 | if(server.socketPath) {
|
103 | this.listen(server.socketPath);
|
104 | } else {
|
105 | this.listen(server.port || 3000);
|
106 | }
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | Akyuu.prototype.setTemplateRoot = function(root) {
|
114 | this.set("views", root);
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Akyuu.prototype.setTemplateEngine = function(engine) {
|
122 | this.set("view engine", engine);
|
123 | };
|
124 |
|
125 | module.exports = Akyuu;
|