1 | "use strict";
|
2 | const fs = require("fs");
|
3 | const Koa = require("koa");
|
4 | const bodyParser = require("koa-bodyparser");
|
5 | const serverStaic = require("koa-static");
|
6 | const path = require("path");
|
7 | const config_1 = require("./config");
|
8 | const config_2 = require("./config");
|
9 | const emitter = require("./event.js");
|
10 | const Exception = require("./Exception.js");
|
11 | const initApp = require("./InitApp.js");
|
12 | const Loader_js_1 = require("./Loader.js");
|
13 | const Logger = require("./Logger.js");
|
14 | const catchError = require("./middleware/bun_catch_error.js");
|
15 | const router = require("./middleware/bun_router.js");
|
16 | const views = require("./middleware/bun_view.js");
|
17 | const Plugin = require("./Plugin.js");
|
18 | const Routes = require("./Routes.js");
|
19 | const utils_1 = require("./utils");
|
20 | class Bun extends Koa {
|
21 | constructor(name, options) {
|
22 | super();
|
23 | this.name = name;
|
24 | this.context.app = this;
|
25 | const rootPath = options.ROOT_PATH || path.dirname(require.main.filename);
|
26 | this.globalPath = {};
|
27 | Object.assign(this.globalPath, config_1.globalPath(rootPath));
|
28 | this.Logger = Logger(this.globalPath.LOG_PATH);
|
29 | this.Loader = Loader_js_1.loader;
|
30 | this.Routes = Routes;
|
31 | this.events = emitter;
|
32 | this.plugins = {};
|
33 | this.lib = {};
|
34 | this.globalModule = {};
|
35 | const Module = require("module");
|
36 | Module._extensions[".less"] = (module, fn) => {
|
37 | return "";
|
38 | };
|
39 | Module._extensions[".css"] = (module, fn) => {
|
40 | return "";
|
41 | };
|
42 | Object.freeze(bun);
|
43 | }
|
44 | setException() {
|
45 | global.Exception = Exception;
|
46 | this.emitter("setException");
|
47 | }
|
48 | setGlobalModule() {
|
49 | const files = fs.readdirSync(this.globalPath.ROOT_PATH + "/app");
|
50 | files.forEach((filename) => {
|
51 | const stat = fs.lstatSync(this.globalPath.ROOT_PATH + "/app" + "/" + filename);
|
52 | if (stat.isDirectory()) {
|
53 | let loaderList = [...config_2.mvcLoaderList];
|
54 | try {
|
55 | const appLoaderConf = require(this.globalPath.CONF_PATH + '/' + filename + '/globalLoader');
|
56 | loaderList = [...config_2.mvcLoaderList, ...appLoaderConf];
|
57 | }
|
58 | catch (e) {
|
59 | bun.Logger.bunwarn('App ' + filename + " globalLoader not found ");
|
60 | }
|
61 | Loader_js_1.getGlobalModule(filename, loaderList);
|
62 | }
|
63 | });
|
64 | const Module = require("module");
|
65 | function stripBOM(content) {
|
66 | if (content.charCodeAt(0) === 0xFEFF) {
|
67 | content = content.slice(1);
|
68 | }
|
69 | return content;
|
70 | }
|
71 | Module._extensions['.js'] = function (module, filename) {
|
72 | let content = fs.readFileSync(filename, 'utf8');
|
73 | let regExp = new RegExp(bun.globalPath.ROOT_PATH + '/app/(.*?)/.*');
|
74 | if (filename.indexOf('/node_modules') === -1
|
75 | && filename.match(regExp)) {
|
76 | let appName = RegExp.$1;
|
77 | if (!bun.globalModule[appName]) {
|
78 | module._compile(stripBOM(content), filename);
|
79 | return;
|
80 | }
|
81 | let currentKey = Loader_js_1.getFuncName(filename.replace('.js', ''), '/app/' + appName);
|
82 | let str = '';
|
83 | for (const [key, value] of Object.entries(bun.globalModule[appName])) {
|
84 | if (key === currentKey || content.indexOf(key) === -1) {
|
85 | continue;
|
86 | }
|
87 | str += 'const ' + key + ' = require("' + value + '");\n';
|
88 | }
|
89 | content = str + '\n' + content;
|
90 | }
|
91 | module._compile(stripBOM(content), filename);
|
92 | };
|
93 | this.emitter("setGlobalModule");
|
94 | }
|
95 | initAllApps() {
|
96 | let routes = {};
|
97 | this.app = {};
|
98 | const files = fs.readdirSync(this.globalPath.ROOT_PATH + "/app");
|
99 | files.forEach((filename) => {
|
100 | const stat = fs.lstatSync(this.globalPath.ROOT_PATH + "/app" + "/" + filename);
|
101 | if (stat.isDirectory()) {
|
102 | routes = new this.Routes(filename);
|
103 | this.app[filename] = initApp(filename);
|
104 | }
|
105 | });
|
106 | this.emitter("initApp", this.app);
|
107 | }
|
108 | setLib() {
|
109 | this.Loader({
|
110 | keypath: "lib",
|
111 | path: "/lib",
|
112 | context: this.lib
|
113 | });
|
114 | this.emitter("setLib", this.lib);
|
115 | }
|
116 | setPlugins() {
|
117 | Plugin();
|
118 | this.emitter("setPlugins", this.plugins);
|
119 | }
|
120 | setRouter() {
|
121 | const routes = new this.Routes();
|
122 | for (const [key, value] of Object.entries(this.app)) {
|
123 | routes.mergeAppRoutes(value.router.routesHandle);
|
124 | }
|
125 | this.use(router(routes.routesHandle));
|
126 | this.emitter("setRouter");
|
127 | }
|
128 | setReqLog() {
|
129 | this.use(this.Logger.log4js.koaLogger(this.Logger.reqLog(), {
|
130 | format: "[:remote-addr :method :url :status :response-timems][:referrer HTTP/:http-version :user-agent]",
|
131 | level: "auto",
|
132 | }));
|
133 | this.emitter("setReqLog");
|
134 | }
|
135 | setServerStaic() {
|
136 | this.use(serverStaic(this.globalPath.STATIC_PATH));
|
137 | this.emitter("setServerStaic");
|
138 | }
|
139 | setBodyParser() {
|
140 | this.use(bodyParser());
|
141 | this.emitter("setBodyParser");
|
142 | }
|
143 | setViews() {
|
144 | this.use(views(this.globalPath.TPL_PATH, {
|
145 | ext: config_1.viewExt,
|
146 | }));
|
147 | this.emitter("setViews");
|
148 | }
|
149 | setErrHandle() {
|
150 | this.use(catchError);
|
151 | }
|
152 | run(port = config_1.defaultPort) {
|
153 | this.listen(port);
|
154 | utils_1.deepFreeze(bun, "context");
|
155 | console.log("start on" + port);
|
156 | }
|
157 | emitter(eventName, freeze) {
|
158 | if (freeze) {
|
159 | utils_1.deepFreeze(freeze);
|
160 | }
|
161 | emitter.emit(eventName, this);
|
162 | }
|
163 | }
|
164 | module.exports = Bun;
|
165 |
|
\ | No newline at end of file |