UNPKG

3.1 kBJavaScriptView Raw
1/**
2 * XadillaX created at 2016-03-23 14:52:05 With ♥
3 *
4 * Copyright (c) 2016 akyuu.moe, all rights
5 * reserved.
6 */
7"use strict";
8
9var path = require("path");
10var util = require("util");
11
12var Ero = require("ero");
13
14var Model = require("./loader/model.js");
15var Connection = require("./loader/connection");
16var Controller = require("./loader/controller");
17var Express = require("./_express/application");
18var Logger = require("./logger");
19var Plugin = require("./loader/plugin");
20var Service = require("./loader/service");
21
22/**
23 * Akyuu
24 * @constructor
25 * @param {String} [projectRoot] the project root path
26 */
27var 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
54util.inherits(Akyuu, Express);
55
56/**
57 * init
58 * @param {Function} callback the callback function
59 */
60Akyuu.prototype.init = function(callback) {
61 Express.prototype.init.call(this);
62
63 this.logger.load();
64 this._logger = this.logger.get("akyuu");
65
66 // custom errors
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 * start the server
98 */
99Akyuu.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 * setTemplateRoot
111 * @param {String} root the view path
112 */
113Akyuu.prototype.setTemplateRoot = function(root) {
114 this.set("views", root);
115};
116
117/**
118 * setTemplateEngine
119 * @param {String|Function} engine the render engine
120 */
121Akyuu.prototype.setTemplateEngine = function(engine) {
122 this.set("view engine", engine);
123};
124
125module.exports = Akyuu;