mm_os
Version:
MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。
194 lines (181 loc) • 4.69 kB
JavaScript
const send = require('koa-send');
const Item = require('mm_machine').Drive;
const { Static } = require('mm_statics');
/**
* 静态文件驱动类
* @augments {Item}
* @class
*/
class Drive extends Item {
static config = {
// 应用名
'app': 'server',
// 插件名
'plugin': 'sys',
// 物理根目录
'root': '',
// 路由根路径
'path': '',
// 名称
'name': '',
// 首页
'index': 'index.html',
// 重定向
'redirect': true,
// 缓存保留10分钟
'max_age': 60 * 10,
// 是否保持不变
'immutable': true,
// 是否启用brotli压缩
'brotli': true,
// 是否启用gizp压缩
'gzip': true,
// 允许访问的拓展名
'extensions': null,
// 是否将ES6转换AMD
'convert_amd': true,
// 主程序文件 - 默认为空
'main': ''
};
/**
* 构造函数
* @param {object} config 配置参数
* @param {object} parent 父对象
* @class
*/
constructor(config, parent) {
super({ ...Drive.config, ...config }, parent);
// 更新并重载
this.mode = 3;
}
}
/**
* 发送文件函数
*/
Drive.prototype.send = send;
/**
* 更新配置对象前
*/
Drive.prototype._preset = function () {
var cg = this.config;
if (cg.maxAge) {
cg.max_age = cg.maxAge * 1000;
delete cg.maxAge;
}
if (!cg.app) {
cg.app = this.getAppName(this.config_file);
}
if (!cg.plugin) {
cg.plugin = this.getPluginName(this.config_file);
}
if (!cg.path) {
let app = cg.app || this.getAppName(this.config_file);
let plugin = cg.plugin || this.getPluginName(this.config_file);
if (plugin) {
cg.path = `/${app}/${plugin}/`;
}
else {
cg.path = `/${app}/`;
}
}
else if (!cg.path.endsWith('/')) {
cg.path += '/';
}
if (!cg.root) {
cg.root = this._getRoot(this.config_file);
}
this.static = new Static(cg);
};
/**
* 获取应用名
* @param {string} file 文件路径
* @returns {string} 应用名
*/
Drive.prototype.getAppName = function (file) {
if (!file || typeof file !== 'string') {
return '';
}
return file.between('app/', '/') || '';
};
/**
* 获取插件名
* @param {string} file 文件路径
* @returns {string} 插件名
*/
Drive.prototype.getPluginName = function (file) {
if (!file || typeof file !== 'string') {
return '';
}
return file.between('plugin/', '/') || '';
};
/**
* 获取物理根目录
* @param {string} file 文件路径
* @returns {string} 物理根目录
*/
Drive.prototype._getRoot = function (file) {
if (!file || typeof file !== 'string') {
return '';
}
let f = file.replace(/\\/g, '/');
let dir = f.dirname().replace(/\\/g, '/').toLowerCase();
let ph = $.run_path.replace(/\\/g, '/').toLowerCase();
return dir.replace(ph, '.');
};
/**
* 执行前
* @param {object} ctx http请求上下文
* @param {object} path 文件路径
*/
Drive.prototype.before = async function (ctx, path) { };
/**
* 执行
* @param {object} ctx http请求上下文
* @param {object} path 文件路径
* @returns {boolean} 成功发送返回true,失败返回false
*/
Drive.prototype.main = async function (ctx, path) {
return send(ctx, path, this.config);
};
/**
* 执行后
* @param {object} ctx http请求上下文
* @param {object} path 文件路径
*/
Drive.prototype.after = async function (ctx, path) { };
/**
* 执行静态文件
* @param {object} ctx Http请求上下文
* @param {object} next 跳过当前函数
* @returns {string} 执行成功返回文件路径
*/
Drive.prototype.run = async function (ctx, next) {
return await this.static.main(ctx, next);
};
/**
* 获取名称
* @param {string} file 文件路径
* @returns {string} 名称
*/
Drive.prototype._getName = function (file) {
return file.dirname().dirname().basename();
};
/**
* 获取模型
* @param {string} type 模型类型
* @returns {object} 返回获取到的模型
*/
Drive.prototype.getModel = function (type) {
let model = { ...this.config };
let dir = this.getDir();
let l = $.slash;
let app_name = dir.between('app' + l, l);
let plugin_name = dir.between('plugin' + l, l);
let name = dir.basename();
model.app = app_name;
model.plugin = plugin_name;
model.name = model.name || plugin_name ? plugin_name + '_' + name : app_name + '_' + name;
model.path = model.path || plugin_name ? `/${app_name}/${plugin_name}/` : `/${app_name}/`;
return model;
};
module.exports = Drive;