Comparing version 0.1.1 to 0.1.2
var fs = require("fs"); | ||
var path = require("path"); | ||
var util = require("util"); | ||
var crypto = require("crypto"); | ||
//when for global variables | ||
global.when = require("when"); | ||
/** | ||
@@ -129,2 +133,15 @@ * 动态创建一个类 | ||
/** | ||
* 修改目录或者文件权限 | ||
* @param {[type]} p [description] | ||
* @param {[type]} mode [description] | ||
* @return {[type]} [description] | ||
*/ | ||
global.chmod = function(p, mode){ | ||
mode = mode || 0777; | ||
if (fs.existsSync(p)) { | ||
return true; | ||
}; | ||
return fs.chmodSync(p, mode); | ||
} | ||
/** | ||
* 判断一个目录是否可写 | ||
@@ -148,2 +165,9 @@ * @param {[type]} p [description] | ||
} | ||
global.file_state = function(file){ | ||
if (!fs.existsSync(file)) { | ||
return false; | ||
}; | ||
return fs.statSync(file); | ||
} | ||
/** | ||
@@ -162,2 +186,6 @@ * 获取文件内容 | ||
global.file_put_contents = function(file, data){ | ||
return fs.writeFileSync(file, data); | ||
} | ||
global.ucfirst = function(name){ | ||
@@ -172,3 +200,3 @@ name = name || ""; | ||
*/ | ||
global.throw_error = function(obj){ | ||
global.throw_error = function(obj, http){ | ||
if (is_string(obj)) { | ||
@@ -179,5 +207,22 @@ obj = {msg: obj}; | ||
type: "error", | ||
msg: "" | ||
msg: "", | ||
http: http | ||
}, obj); | ||
if (http) { | ||
throw obj; | ||
return; | ||
}; | ||
throw new Error(JSON.stringify(obj)); | ||
} | ||
/** | ||
* 获取字符串的md5 | ||
* @param {[type]} str [description] | ||
* @return {[type]} [description] | ||
*/ | ||
global.md5 = function(str){ | ||
var instance = crypto.createHash('md5'); | ||
instance.update(str); | ||
return instance.digest('hex'); | ||
} |
@@ -5,2 +5,6 @@ /** | ||
*/ | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var _alias = {}; | ||
@@ -10,15 +14,23 @@ var _autoload_callbacks = []; | ||
if (_alias[name]) { | ||
return require(_alias[name]); | ||
var obj = require(_alias[name]); | ||
if (typeof obj == 'function') { | ||
//修正子类继承的方法获取到正确的文件名 | ||
obj.prototype.__filename = _alias[name]; | ||
}; | ||
return obj; | ||
}; | ||
var result = ''; | ||
_autoload_callbacks.some(function(callback){ | ||
var file = callback && callback(name); | ||
if (file) { | ||
result = file; | ||
result = callback && callback(name); | ||
if (result) { | ||
return true; | ||
}; | ||
return false; | ||
}) | ||
if (result) { | ||
return require(result); | ||
var obj = require(result); | ||
if (typeof obj == 'function') { | ||
//修正子类继承的方法获取到正确的文件名 | ||
obj.prototype.__filename = result; | ||
}; | ||
return obj; | ||
}; | ||
@@ -45,50 +57,27 @@ return require(name); | ||
} | ||
/** | ||
* 配置读取和写入 | ||
* 类继承 | ||
* @param {[type]} className [description] | ||
* @param {[type]} obj [description] | ||
* @return {[type]} [description] | ||
*/ | ||
var _config = {}; | ||
global.C = function(name, value){ | ||
if (name === undefined) { | ||
return _config; | ||
}; | ||
if (is_string(name)) { | ||
if (name.indexOf('.') <= 0) { | ||
name = name.toLowerCase(); | ||
if (value === undefined) { | ||
return _config[name]; | ||
}; | ||
_config[name] = value; | ||
return; | ||
}; | ||
name = name.split("."); | ||
name[0] = name[0].toLowerCase(); | ||
if (value === undefined) { | ||
value = _config[name[0]] || {}; | ||
return value[name[1]]; | ||
}; | ||
if (!_config[name[0]]) { | ||
_config[name[0]] = {}; | ||
}; | ||
_config[name[0]][name[1]] = value; | ||
}else{ | ||
_config = extend(_config, name); | ||
global.inherits = function(className, obj){ | ||
var cls = Class({}); | ||
cls.inherits(think_require(className)); | ||
cls.extend(obj); | ||
return cls; | ||
}; | ||
["Cache", "Behavior", "Action", "Session", "Model", "Db"].forEach(function(item){ | ||
global[item] = function(obj){ | ||
return inherits(item, obj); | ||
} | ||
} | ||
}); | ||
/** | ||
* 创建一个行为类 | ||
*/ | ||
global.Behavior = function(obj){ | ||
var cls = Class({}); | ||
cls.inherits(think_require("Behavior")); | ||
cls.extend(obj); | ||
return cls; | ||
} | ||
/** | ||
* 调用一个指定的行为 | ||
* @param {[type]} name [description] | ||
*/ | ||
global.B = function(name, data){ | ||
global.B = function(name, http, data){ | ||
var cls = name + "Behavior"; | ||
@@ -98,3 +87,3 @@ if (APP_DEBUG) { | ||
}; | ||
var result = think_require(cls)().run(data); | ||
var result = think_require(cls)(http).run(data); | ||
if (APP_DEBUG) { | ||
@@ -110,3 +99,3 @@ G('behavior_end'); | ||
*/ | ||
global.tag = function(name, data){ | ||
global.tag = function(name, http, data){ | ||
var sys_tags = C('sys_tags.' + name); | ||
@@ -132,3 +121,3 @@ var tags = C('tags.' + name); | ||
tags.forEach(function(b){ | ||
var result = B(b, __behavior_data); | ||
var result = B(b, http, __behavior_data); | ||
if (result !== undefined) { | ||
@@ -142,29 +131,129 @@ __behavior_data = result; | ||
/** | ||
* 记录时间和内存使用情况 | ||
* 配置读取和写入 | ||
*/ | ||
global.G = function(){ | ||
var _config = {}; | ||
global.C = function(name, value){ | ||
if (name === undefined) { | ||
return _config; | ||
}; | ||
if (is_string(name)) { | ||
if (name.indexOf('.') <= 0) { | ||
name = name.toLowerCase(); | ||
if (value === undefined) { | ||
return _config[name]; | ||
}; | ||
_config[name] = value; | ||
return; | ||
}; | ||
name = name.split("."); | ||
name[0] = name[0].toLowerCase(); | ||
if (value === undefined) { | ||
value = _config[name[0]] || {}; | ||
return value[name[1]]; | ||
}; | ||
if (!_config[name[0]]) { | ||
_config[name[0]] = {}; | ||
}; | ||
_config[name[0]][name[1]] = value; | ||
}else{ | ||
_config = extend(_config, name); | ||
} | ||
} | ||
/** | ||
* 创建一个Action,继承Action基类 | ||
* @param {[type]} obj [description] | ||
* 记录时间和内存使用情况 | ||
*/ | ||
global.Action = function(obj){ | ||
var cls = Class({}); | ||
cls.inherits(think_require("Action")); | ||
cls.extend(obj); | ||
return cls; | ||
global.G = function(){ | ||
} | ||
/** | ||
* 实例化Action类 | ||
* 实例化Action类,可以调用一个具体的Action | ||
* A('Group/Action') | ||
* @param {[type]} name [description] | ||
*/ | ||
global.A = function(name){ | ||
try{ | ||
return think_require(name + "Action")(); | ||
}catch(e){ | ||
return false; | ||
global.A = function(name, http, data){ | ||
if (name.indexOf('/') === -1) { | ||
name = http.req.group + "/" + name; | ||
}; | ||
name = name.split("/"); | ||
var gm = name.shift() + "/" + name.shift(); | ||
var action = name.shift(); | ||
var instance = think_require(gm + "Action")(http); | ||
if (action) { | ||
action += C('action_suffix'); | ||
return instance[action].apply(instance, data || []); | ||
}; | ||
return instance; | ||
} | ||
/** | ||
* 快速文件读取和写入 | ||
*/ | ||
var _cache = {}; | ||
global.F = function(name, value, rootPath){ | ||
if (rootPath === undefined) { | ||
rootPath = DATA_PATH; | ||
}; | ||
var filename = rootPath + "/" + name + ".json"; | ||
if (value !== undefined) { | ||
if (value === null) { | ||
if (is_file(filename)) { | ||
fs.unlink(filename, function(){}); | ||
}; | ||
return; | ||
}; | ||
var dir = path.dirname(filename); | ||
mkdir(dir, "0777"); | ||
_cache[name] = value; | ||
fs.writeFile(filename, JSON.stringify(value), function(){}); | ||
return; | ||
}; | ||
if (_cache[name] !== undefined) { | ||
return _cache[name]; | ||
}; | ||
if (is_file(filename)) { | ||
var data = JSON.parse(file_get_contents(filename)); | ||
_cache[name] = data; | ||
return data; | ||
}; | ||
return false; | ||
} | ||
global.M = function(){ | ||
} | ||
global.D = function(){ | ||
} | ||
/** | ||
* 缓存 | ||
*/ | ||
var caches = {}; | ||
global.S = function(name, value, options){ | ||
if (options === undefined) { | ||
options = {}; | ||
}; | ||
var type = options.type || C('data_cache_type'); | ||
if (type) { | ||
type = ucfirst(type.toLowerCase()) + "Cache"; | ||
}else{ | ||
type = "Cache"; | ||
} | ||
var instance = caches[type]; | ||
if (!instance) { | ||
instance = caches[type] = think_require(type)(options); | ||
}; | ||
if (value === undefined) { | ||
return instance.get(name); | ||
}; | ||
if (value === null) { | ||
return instance.rm(name); | ||
}; | ||
var expire = typeof options == 'number' ? options : undefined; | ||
return instance.set(name, value, expire); | ||
} | ||
global.N = function(){ | ||
} |
@@ -7,10 +7,4 @@ /** | ||
//URL模式 | ||
// global.URL_COMMON = 0; | ||
// global.URL_PATHINFO = 1; | ||
// global.URL_REWRITE = 2; | ||
// global.URL_COMPAT = 3; | ||
//系统路径设置 | ||
global.THINK_LIB_PATH = THINK_PATH + "/Lib"; | ||
global.EXTEND_PATH = THINK_PATH + "/Extend"; | ||
//应用路径设置 | ||
@@ -58,10 +52,15 @@ var config = { | ||
function build_app_dir(){ | ||
if (!is_dir(APP_PATH)) { | ||
mkdir(APP_PATH); | ||
}; | ||
if (is_writable(APP_PATH)) { | ||
var dirs = [ | ||
LIB_PATH, RUNTIME_PATH, CONF_PATH, COMMON_PATH, LANG_PATH, CACHE_PATH, | ||
TMPL_PATH, LOG_PATH, TEMP_PATH, DATA_PATH, | ||
LIB_PATH, RUNTIME_PATH, CONF_PATH, | ||
COMMON_PATH, CACHE_PATH, | ||
LOG_PATH, TEMP_PATH, DATA_PATH, | ||
LIB_PATH + '/Model', | ||
LIB_PATH + '/Action', | ||
LIB_PATH + '/Action/Home', | ||
LIB_PATH + '/Behavior', | ||
LIB_PATH + '/Widget' | ||
LIB_PATH + '/Driver', | ||
TMPL_PATH + "/Home" | ||
]; | ||
@@ -84,3 +83,3 @@ dirs.forEach(function(dir){ | ||
function build_first_action(){ | ||
var file = LIB_PATH + "/Action/IndexAction.class.js"; | ||
var file = LIB_PATH + "/Action/Home/IndexAction.class.js"; | ||
if (!is_file(file)) { | ||
@@ -87,0 +86,0 @@ var content = file_get_contents(THINK_PATH + "/Tpl/IndexAction.class.js"); |
@@ -15,6 +15,6 @@ /** | ||
"Model": THINK_LIB_PATH + "/Core/Model.class.js", | ||
"Session": THINK_LIB_PATH + "/Core/Session.class.js", | ||
"Think": THINK_LIB_PATH + "/Core/Think.class.js", | ||
"View": THINK_LIB_PATH + "/Core/View.class.js", | ||
"Widget": THINK_LIB_PATH + "/Core/Widget.class.js", | ||
"Tool": THINK_LIB_PATH + "/Core/Tool.class.js" | ||
"Valid": THINK_LIB_PATH + "/Core/Valid.class.js", | ||
"View": THINK_LIB_PATH + "/Core/View.class.js" | ||
} |
@@ -8,2 +8,3 @@ /** | ||
encoding: 'utf8', //编码 | ||
pathname_prefix: "", //不解析的pathname前缀 | ||
action_name_black_list: ["super", "init", /^\_[^\_]+/], //不能通过URL访问的action名单 | ||
@@ -20,3 +21,3 @@ app_tags_on: true, //是否支持标签功能 | ||
default_action: 'index', //默认Action | ||
url_module_map: {}, //模块别名,隐藏真实模块名 | ||
url_module_map: {}, //Module别名,隐藏真实模块名 | ||
url_action_map: {}, //Action别名 | ||
@@ -28,14 +29,58 @@ app_group_mode: 0, //0为普通分组,1为独立分组 | ||
action_suffix: "Action", //action后缀 | ||
cookie_expires: 0, //cookie默认保持时间数,默认随浏览器关闭失效 | ||
deny_ip: [], //阻止的ip | ||
deny_remote_access_by_ip: true, //禁止通过外网的IP直接访问 | ||
cookie_domain: "", // Cookie有效域名 | ||
cookie_path: "", // Cookie路径 | ||
cookie_expires: 0, //cookie默认保持时间数,默认随浏览器关闭失效 | ||
cookie_prefix: "", // Cookie前缀 避免冲突 | ||
session_auto_start: true, | ||
session_id: "thinkjs", | ||
session_type: "File", //session存储类型 | ||
session_options: {}, | ||
session_cookie_sign: "", //session对应的cookie使用签名,如果使用这里填密钥 | ||
db_type: "mysql", // 数据库类型 | ||
db_host: "localhost",// 服务器地址 | ||
db_name: "",// 数据库名 | ||
db_user: "root",// 用户名 | ||
db_pwd: "",// 密码 | ||
db_port: "",// 端口 | ||
db_prefix: "think_",// 数据库表前缀 | ||
db_fieldtype_check: false,// 是否进行字段类型检查 | ||
db_fields_cache: true, // 启用字段缓存 | ||
db_charset: "utf8", // 数据库编码默认采用utf8 | ||
db_deploy_type: 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) | ||
db_rw_separate: false, // 数据库读写是否分离 主从式有效 | ||
db_master_num: 1, // 读写分离后 主服务器数量 | ||
db_slave_no: "", // 指定从服务器序号 | ||
db_sql_build_cache: false, // 数据库查询的SQL创建缓存 | ||
db_sql_build_queue: 'file', // SQL缓存队列的缓存方式 支持 file | ||
db_sql_build_length: 20, // SQL缓存的队列长度 | ||
db_sql_log: false, // SQL执行日志记录 | ||
db_field_version: "", // | ||
tpl_content_type: "text/html", //模版输出类型 | ||
tpl_file_suffix: ".html", //模版文件名后缀 | ||
tpl_file_depr: "_", //module和action之间的分隔符 | ||
tpl_engine_type: "ejs", //模版引擎名称 | ||
tpl_engine_config: { //模版引擎需要的配置 | ||
open: "{%", | ||
end: "%}", | ||
} | ||
}, | ||
log_record: false, //是否记录日志 | ||
data_cache_type: "File", //数据缓存类型,支持:File|Db|Memcache | ||
data_cache_time: 0, //数据缓存有效期,0表示永久缓存,单位: 秒 | ||
data_cache_prefix: "", //缓存前缀 | ||
data_cache_check: false, //数据缓存是否校验缓存 | ||
data_cache_path: TEMP_PATH, //缓存路径设置 (仅对File方式缓存有效)\ | ||
data_path_level: 1, // 子目录缓存级别,默认不使用子目录 | ||
data_file_suffix: ".json", //缓存文件后缀 | ||
memcache_host: "127.0.0.1", // | ||
memcache_port: 11211, //memecache端口 | ||
}; |
@@ -17,3 +17,4 @@ /** | ||
view_filter: [], | ||
view_end: [] | ||
view_end: [], | ||
before_res_end: [] | ||
} |
@@ -8,3 +8,4 @@ /** | ||
var Dispatcher = think_require("Dispatcher"); | ||
var behavior = module.exports = Behavior(function(){ | ||
module.exports = Behavior(function(){ | ||
return { | ||
@@ -16,3 +17,3 @@ options: { | ||
run: function(){ | ||
var pathname = __http.req.pathname; | ||
var pathname = this.http.req.pathname; | ||
if (!pathname) { | ||
@@ -66,3 +67,3 @@ return false; | ||
for(var i = 0,length = Math.ceil(pathname.length)/2; i < length; i++){ | ||
__http.req.query[pathname[i * 2]] = pathname[i * 2 + 1] || ""; | ||
this.http.req.query[pathname[i * 2]] = pathname[i * 2 + 1] || ""; | ||
} | ||
@@ -103,9 +104,9 @@ }; | ||
if (urlInfo.query) { | ||
__http.req.query = extend(__http.req.query, urlInfo.query); | ||
this.http.req.query = extend(this.http.req.query, urlInfo.query); | ||
}; | ||
var pathname = urlInfo.pathname || ""; | ||
pathname = pathname.split("/"); | ||
__http.req.action = Dispatcher.getAction(pathname.pop() || ""); | ||
__http.req.module = Dispatcher.getModule(pathname.pop() || ""); | ||
__http.req.group = Dispatcher.getGroup(pathname.pop() || ""); | ||
this.http.req.action = Dispatcher.getAction(pathname.pop() || ""); | ||
this.http.req.module = Dispatcher.getModule(pathname.pop() || ""); | ||
this.http.req.group = Dispatcher.getGroup(pathname.pop() || ""); | ||
}, | ||
@@ -123,3 +124,3 @@ getRoute: function(route){ | ||
}); | ||
if (nmethod.indexOf(__http.req.method) != -1) { | ||
if (nmethod.indexOf(this.http.req.method) != -1) { | ||
sRoute = route[method]; | ||
@@ -151,3 +152,3 @@ break; | ||
for(var i = 0,length = Math.ceil(pathname.length)/2; i < length; i++){ | ||
__http.req.query[pathname[i * 2]] = pathname[i * 2 + 1] || ""; | ||
this.http.req.query[pathname[i * 2]] = pathname[i * 2 + 1] || ""; | ||
} | ||
@@ -154,0 +155,0 @@ }; |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
var behavior = module.exports = Behavior(function(){ | ||
module.exports = Behavior(function(){ | ||
return { | ||
@@ -15,3 +15,3 @@ options: { | ||
}; | ||
var clientIps = (__http.req.ip+"").split("."); | ||
var clientIps = (this.http.req.ip+"").split("."); | ||
var flag = this.options.deny_ip.some(function(item){ | ||
@@ -28,4 +28,4 @@ return (item + "").split(".").every(function(num, i){ | ||
msg: "deny ip", | ||
code: 403 | ||
}) | ||
code: 403, | ||
}, this.http); | ||
}; | ||
@@ -32,0 +32,0 @@ return true; |
@@ -5,3 +5,4 @@ /** | ||
*/ | ||
var behavior = module.exports = Behavior(function(){ | ||
module.exports = Behavior(function(){ | ||
return { | ||
@@ -17,6 +18,6 @@ run: function(templateFile){ | ||
templateFile = [ | ||
TMPL_PATH, "/", __http.req.group, "/", | ||
__http.req.module.toLowerCase(), | ||
TMPL_PATH, "/", this.http.req.group, "/", | ||
this.http.req.module.toLowerCase(), | ||
C('tpl_file_depr'), | ||
__http.req.action.toLowerCase(), | ||
this.http.req.action.toLowerCase(), | ||
C('tpl_file_suffix') | ||
@@ -27,4 +28,4 @@ ].join(""); | ||
var action = path.pop(); | ||
var module = path.pop() || __http.req.module.toLowerCase(); | ||
var group = ucfirst(path.pop()) || __http.req.group; | ||
var module = path.pop() || this.http.req.module.toLowerCase(); | ||
var group = ucfirst(path.pop()) || this.http.req.group; | ||
templateFile = [ | ||
@@ -31,0 +32,0 @@ TMPL_PATH, "/", group, "/", |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
var behavior = module.exports = Behavior(function(){ | ||
module.exports = Behavior(function(){ | ||
return { | ||
@@ -8,0 +8,0 @@ run: function(data){ |
@@ -5,31 +5,71 @@ /** | ||
*/ | ||
var action = module.exports = Class(function(){ | ||
var fs = require("fs"); | ||
module.exports = Class(function(){ | ||
return { | ||
init: function(){ | ||
init: function(http){ | ||
this.http = http; | ||
this.view = null; | ||
tag('action_start'); | ||
}, | ||
/** | ||
* 获取客户端的ip | ||
* @return {[type]} [description] | ||
*/ | ||
ip: function(){ | ||
return this.http.req.ip; | ||
}, | ||
/** | ||
* 实例化View类 | ||
* @return {[type]} [description] | ||
*/ | ||
initView: function(){ | ||
if (!this.view) { | ||
this.view = think_require("View")(); | ||
this.view = think_require("View")(this.http); | ||
}; | ||
return this.view; | ||
}, | ||
/** | ||
* 是否是GET请求 | ||
* @return {Boolean} [description] | ||
*/ | ||
isGet: function(){ | ||
return __http.req.method == 'get'; | ||
return this.http.req.method == 'get'; | ||
}, | ||
/** | ||
* 是否是POST请求 | ||
* @return {Boolean} [description] | ||
*/ | ||
isPost: function(){ | ||
return __http.req.method == 'post'; | ||
return this.http.req.method == 'post'; | ||
}, | ||
/** | ||
* 是否是特定METHOD请求 | ||
* @param {[type]} method [description] | ||
* @return {Boolean} [description] | ||
*/ | ||
isMethod: function(method){ | ||
return __http.req.method == method; | ||
return this.http.req.method == method; | ||
}, | ||
/** | ||
* 是否是AJAX请求 | ||
* @return {Boolean} [description] | ||
*/ | ||
isAjax: function(){ | ||
return this.header("X-Requested-With").toLowerCase() == "xmlhttprequest"; | ||
}, | ||
/** | ||
* 获取QUERY参数 | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
get: function(name){ | ||
return __http.req.query[name] || ""; | ||
return this.http.req.query[name] || ""; | ||
}, | ||
/** | ||
* 获取POST参数 | ||
* @param {[type]} name [description] | ||
* @return {[type]} [description] | ||
*/ | ||
post: function(name){ | ||
return __http.req.post[name] || ""; | ||
return this.http.req.post[name] || ""; | ||
}, | ||
@@ -40,21 +80,56 @@ param: function(name){ | ||
file: function(name){ | ||
return __http.req.file[name] || ""; | ||
return this.http.req.file[name] || ""; | ||
}, | ||
/** | ||
* header操作 | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
* @return {[type]} [description] | ||
*/ | ||
header: function(name, value){ | ||
if (value !== undefined) { | ||
__http.res.setHeader(name, value); | ||
this.http.res.setHeader(name, value); | ||
return this; | ||
}; | ||
if (name === undefined) { | ||
return __http.req.headers; | ||
return this.http.req.headers; | ||
}; | ||
return __http.req.getHeader(name); | ||
return this.http.req.getHeader(name); | ||
}, | ||
/** | ||
* cookie操作 | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
* @param {[type]} options [description] | ||
* @return {[type]} [description] | ||
*/ | ||
cookie: function(name, value, options){ | ||
if (value !== undefined) { | ||
__http.res.setCookie(name, value, options); | ||
this.http.res.setCookie(name, value, options); | ||
return this; | ||
}; | ||
return __http.req.cookie[name]; | ||
if (name === undefined) { | ||
return this.http.req.cookie; | ||
}; | ||
return this.http.req.cookie[name]; | ||
}, | ||
/** | ||
* session | ||
* 如果是get操作,则返回一个promise | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
* @return {[type]} [description] | ||
*/ | ||
session: function(name, value){ | ||
think_require('Session').start(this.http); | ||
var instance = this.http.req.session; | ||
if (name === undefined) { | ||
return instance.rm(); | ||
}; | ||
if (value !== undefined) { | ||
instance.set(name, value); | ||
return this; | ||
}; | ||
return instance.get(name); | ||
}, | ||
redirect: function(url, code){ | ||
@@ -64,6 +139,12 @@ throw_error({ | ||
msg: url, | ||
code: code | ||
code: code, | ||
http: this.http | ||
}); | ||
return this; | ||
}, | ||
/** | ||
* 赋值变量到模版 | ||
* @param {[type]} name [description] | ||
* @param {[type]} value [description] | ||
* @return {[type]} [description] | ||
*/ | ||
assign: function(name, value){ | ||
@@ -73,14 +154,54 @@ this.initView().assign(name, value); | ||
}, | ||
fetch: function(templateFile, content, prefix){ | ||
return this.initView().fetch(templateFile, content, prefix); | ||
/** | ||
* 获取解析后的模版内容 | ||
* @param {[type]} templateFile [description] | ||
* @param {[type]} content [description] | ||
* @return {[type]} [description] | ||
*/ | ||
fetch: function(templateFile, content){ | ||
return this.initView().fetch(templateFile, content); | ||
}, | ||
display: function(templateFile, charset, contentType, content, prefix){ | ||
return this.initView().display(templateFile, charset, contentType, content, prefix); | ||
/** | ||
* 输出模版内容 | ||
* @param {[type]} templateFile [description] | ||
* @param {[type]} charset [description] | ||
* @param {[type]} contentType [description] | ||
* @param {[type]} content [description] | ||
* @return {[type]} [description] | ||
*/ | ||
display: function(templateFile, charset, contentType, content){ | ||
return this.initView().display(templateFile, charset, contentType, content); | ||
}, | ||
echo: function(obj){ | ||
error: function(msg){ | ||
throw_error(msg, this.http); | ||
}, | ||
/** | ||
* [action description] | ||
* @param {[type]} action [description] | ||
* @return {[type]} [description] | ||
*/ | ||
action: function(action){ | ||
A(action, this.http); | ||
}, | ||
/** | ||
* 输出内容 | ||
* 自动JSON.stringify | ||
* 自定将数字等转化为字符串 | ||
* @param {[type]} obj [description] | ||
* @return {[type]} [description] | ||
*/ | ||
echo: function(obj, encoding){ | ||
if (is_array(obj) || is_object(obj)) { | ||
obj = JSON.stringify(obj); | ||
}; | ||
__response.write(obj, C('encoding')); | ||
if (!is_string(obj) && !(obj instanceof Buffer)) { | ||
obj += ""; | ||
}; | ||
this.http._res.write(obj, encoding || C('encoding')); | ||
}, | ||
/** | ||
* 结束输出,输出完成时一定要调用这个方法 | ||
* @param {[type]} obj [description] | ||
* @return {[type]} [description] | ||
*/ | ||
end: function(obj){ | ||
@@ -90,5 +211,40 @@ if (obj) { | ||
}; | ||
__response.end(); | ||
this.http.res.end(); | ||
}, | ||
/** | ||
* 下载文件 | ||
* @return {[type]} [description] | ||
*/ | ||
download: function(file, contentType){ | ||
if (!contentType) { | ||
var mime = require('mime'); | ||
contentType = mime.lookup(file); | ||
}; | ||
var res = this.http.res; | ||
var fileStream = fs.createReadStream(file); | ||
res.setHeader("Content-Type", contentType); | ||
fileStream.pipe(this.http._res); | ||
fileStream.on("end", function(){ | ||
res.end(); | ||
}) | ||
}, | ||
/** | ||
* 校验一个值是否合法 | ||
* @param {[type]} data [description] | ||
* @param {[type]} validType [description] | ||
* @return {[type]} [description] | ||
*/ | ||
valid: function(data, validType){ | ||
//单个值检测,只返回是否正常 | ||
if (validType !== undefined) { | ||
data = { | ||
value: data, | ||
valid: validType | ||
}; | ||
var result = think_require("Valid").check(data); | ||
return result.length == 0; | ||
}; | ||
return think_require("Valid").check(data); | ||
} | ||
} | ||
}); |
@@ -5,123 +5,135 @@ /** | ||
*/ | ||
var http = require("http"); | ||
var App = { | ||
init: function(){ | ||
this.loadExtFile(); | ||
this.loadExtConfig(); | ||
think_require('Dispatcher').run(); | ||
tag ('url_dispatch'); | ||
}, | ||
exec: function(){ | ||
var module = ''; | ||
var group = ''; | ||
if (!(/^[A-Za-z](\w)*$/.test(__http.req.module))) { | ||
module = ''; | ||
}else{ | ||
group = __http.req.group && C('app_group_mode') == 0 ? __http.req.group + "/" : ""; | ||
module = A(group + __http.req.module); | ||
} | ||
if (!module) { | ||
module = A(group + "Empty"); | ||
var cluster = require("cluster"); | ||
var App = module.exports = Class(function(){ | ||
return { | ||
http: null, | ||
init: function(http){ | ||
this.http = http; | ||
this.loadExtFile(); | ||
this.loadExtConfig(); | ||
think_require('Dispatcher')(http).run(); | ||
tag ('url_dispatch', http); | ||
}, | ||
exec: function(){ | ||
var module = ''; | ||
var group = ''; | ||
var self = this; | ||
if (!(/^[A-Za-z](\w)*$/.test(this.http.req.module))) { | ||
module = ''; | ||
}else{ | ||
group = this.http.req.group && C('app_group_mode') == 0 ? this.http.req.group + "/" : ""; | ||
try{ | ||
module = A(group + this.http.req.module, this.http); | ||
}catch(e){} | ||
} | ||
if (!module) { | ||
throw_error(__http.req.module + " module not found"); | ||
module = A(group + "Empty", this.http); | ||
if (!module) { | ||
throw_error(this.http.req.module + " module not found", this.http); | ||
}; | ||
}; | ||
}; | ||
var action = __http.req.action; | ||
if (C('action_suffix')) { | ||
action += C('action_suffix'); | ||
}; | ||
//action黑名单 | ||
if (C('action_name_black_list').length) { | ||
var flag = C('action_name_black_list').some(function(item){ | ||
if (is_string(item) && item == action) { | ||
return true; | ||
}else if(item && item.test && item.test(action)){ | ||
return true; | ||
} | ||
return false; | ||
}) | ||
if (flag) { | ||
throw_error("action black"); | ||
var action = this.http.req.action; | ||
if (C('action_suffix')) { | ||
action += C('action_suffix'); | ||
}; | ||
}; | ||
if (!/^[A-Za-z](\w)*$/.test(action)) { | ||
throw_error('action name error'); | ||
}; | ||
if (typeof module[action] == 'function') { | ||
if (C('url_params_bind')) { | ||
var toString = module[action].toString(); | ||
toString = toString.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg, ''); | ||
var match = toString.match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1].split(/,/).filter(function(item){ | ||
return item; | ||
}); | ||
if (match.length) { | ||
var data = []; | ||
match.forEach(function(item){ | ||
var value = __http.req.post[item] || __http.req.query[item] || ""; | ||
data.push(value); | ||
//action黑名单 | ||
if (C('action_name_black_list').length) { | ||
var flag = C('action_name_black_list').some(function(item){ | ||
if (is_string(item) && item == action) { | ||
return true; | ||
}else if(item && item.test && item.test(action)){ | ||
return true; | ||
} | ||
return false; | ||
}) | ||
if (flag) { | ||
throw_error("action black", this.http); | ||
}; | ||
}; | ||
if (!/^[A-Za-z](\w)*$/.test(action)) { | ||
throw_error('action name error', this.http); | ||
}; | ||
if (typeof module[action] == 'function') { | ||
if (C('url_params_bind')) { | ||
var toString = module[action].toString(); | ||
toString = toString.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg, ''); | ||
var match = toString.match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1].split(/,/).filter(function(item){ | ||
return item; | ||
}); | ||
module[action].apply(module, data); | ||
if (match.length) { | ||
var data = []; | ||
match.forEach(function(item){ | ||
var value = self.http.req.post[item] || self.http.req.query[item] || ""; | ||
data.push(value); | ||
}); | ||
module[action].apply(module, data); | ||
}else{ | ||
module[action](); | ||
} | ||
}else{ | ||
module[action](); | ||
} | ||
return true; | ||
}else{ | ||
module[action](); | ||
if (C('call_method') && typeof module[C('call_method')] == 'function') { | ||
return module[C('call_method')](action); | ||
}; | ||
} | ||
return true; | ||
}else{ | ||
if (C('call_method') && typeof module[C('call_method')] == 'function') { | ||
return module[C('call_method')](action); | ||
throw_error("action: "+action+" not found", this.http); | ||
}, | ||
//加载自定义外部文件 | ||
loadExtFile: function(){ | ||
var files = C('load_ext_file'); | ||
if (files) { | ||
if (is_string(files)) { | ||
files = files.split(','); | ||
}; | ||
files.forEach(function(file){ | ||
file = COMMONT_PATH + "/" + file + ".js"; | ||
if (is_file(file)) { | ||
require(file); | ||
}; | ||
}) | ||
}; | ||
} | ||
throw_error("action: "+action+" not found"); | ||
}, | ||
//加载自定义外部文件 | ||
loadExtFile: function(){ | ||
var files = C('load_ext_file'); | ||
if (files) { | ||
if (is_string(files)) { | ||
files = files.split(','); | ||
}; | ||
files.forEach(function(file){ | ||
file = COMMONT_PATH + "/" + file + ".js"; | ||
if (is_file(file)) { | ||
require(file); | ||
}, | ||
//加载额外的配置 | ||
loadExtConfig: function(){ | ||
var files = C('load_ext_config'); | ||
if (files) { | ||
if (is_string(files)) { | ||
files = files.split(","); | ||
}; | ||
}) | ||
}; | ||
}, | ||
//加载额外的配置 | ||
loadExtConfig: function(){ | ||
var files = C('load_ext_config'); | ||
if (files) { | ||
if (is_string(files)) { | ||
files = files.split(","); | ||
files.forEach(function(file){ | ||
file = CONF_PATH + "/" + file + ".js"; | ||
if (is_file(file)) { | ||
C(require(file)); | ||
}; | ||
}) | ||
}; | ||
files.forEach(function(file){ | ||
file = CONF_PATH + "/" + file + ".js"; | ||
if (is_file(file)) { | ||
C(require(file)); | ||
}; | ||
}) | ||
}; | ||
}, | ||
run: function(){ | ||
var self = this; | ||
http.createServer(function (request, response) { | ||
global.__request = request; | ||
global.__response = response; | ||
think_require("Http").run(function(){ | ||
tag ('app_init'); | ||
self.init(); | ||
tag('app_begin'); | ||
G('initTime'); | ||
self.exec(); | ||
tag('app_end'); | ||
if (C('log_record')) { | ||
think_require('Log').save(); | ||
}; | ||
}); | ||
}).listen(C('port')); | ||
} | ||
} | ||
} | ||
module.exports = App; | ||
}); | ||
//start server | ||
module.exports.run = function(){ | ||
var server = require("http").createServer(function (req, res) { | ||
req.beginTime = Date.now(); | ||
think_require("Http")(req, res).run(function(http){ | ||
//初始化Session | ||
if (C('session_auto_start')) { | ||
think_require('Session').start(http); | ||
}; | ||
tag ('app_init', http); | ||
var instance = App(http); | ||
tag('app_begin', http); | ||
G('initTime'); | ||
instance.exec(); | ||
tag('app_end', http); | ||
}); | ||
}); | ||
var params = [C('port')]; | ||
//禁止外网直接通过IP访问 | ||
if (C('deny_remote_access_by_ip')) { | ||
params.push("127.0.0.1"); | ||
}; | ||
server.listen.apply(server, params); | ||
}; |
@@ -5,6 +5,8 @@ /** | ||
*/ | ||
var Behavior = Class(function(){ | ||
module.exports = Class(function(){ | ||
return { | ||
options: {}, //行为选项 | ||
init: function(){ | ||
http: null, | ||
init: function(http){ | ||
this.http = http; | ||
for(var name in this.options){ | ||
@@ -14,3 +16,3 @@ if (C(name) !== undefined) { | ||
}else{ | ||
C(name) = this.options[name]; | ||
C(name, this.options[name]); | ||
} | ||
@@ -23,3 +25,2 @@ } | ||
} | ||
}); | ||
module.exports = Behavior; | ||
}); |
@@ -5,12 +5,46 @@ /** | ||
*/ | ||
var Cache = Class(function(){ | ||
module.exports = Class(function(){ | ||
var caches = {}; | ||
return { | ||
init: function(){ | ||
options: {}, | ||
init: function(options){ | ||
this.options = extend({ | ||
"temp": C('data_cache_path'), | ||
"prefix": C('data_cache_prefix'), | ||
"expire": C('data_cache_expire'), | ||
"length": 0 | ||
}, options); | ||
}, | ||
run: function(){ | ||
get: function(name){ | ||
var value = caches[name]; | ||
if (!value) { | ||
return false; | ||
}; | ||
var expire = value.expire * 1000; | ||
var data = value.data; | ||
var time = value.time; | ||
if (Date.now() > (time + expire)) { | ||
delete caches[name]; | ||
return false; | ||
}; | ||
return data; | ||
}, | ||
set: function(name, value, expire){ | ||
if (expire === undefined) { | ||
expire = this.options.expire; | ||
}; | ||
caches[name] = { | ||
expire: expire, | ||
time: Date.now(), | ||
data: value | ||
}; | ||
return this; | ||
}, | ||
rm: function(name){ | ||
delete caches[name]; | ||
}, | ||
clear: function(){ | ||
caches = {}; | ||
} | ||
} | ||
}); | ||
module.exports = Cache; | ||
}); |
@@ -8,80 +8,83 @@ /** | ||
var _group = ''; | ||
var _module = ''; | ||
var _action = ''; | ||
var Dispatcher = { | ||
run: function(){ | ||
tag("path_info"); | ||
var pathname = __http.req.pathname; | ||
var extname = path.extname(pathname); | ||
if (extname == C('url_html_suffix')) { | ||
pathname = path.dirname(pathname) + "/" + path.basename(pathname, C('url_html_suffix')); | ||
}; | ||
pathname = pathname.split("/").filter(function(item){ | ||
item = item.trim(); | ||
if (item) { | ||
return item; | ||
}; | ||
}).join("/"); | ||
__http.req.pathname = pathname; | ||
if (pathname == 'favicon.ico') { | ||
throw_error("favicon.ico"); | ||
return false; | ||
}; | ||
if (!this.routerCheck()) { | ||
var paths = pathname.split("/"); | ||
var groupList = C('app_group_list') || []; | ||
if (groupList.length) { | ||
if (groupList.indexOf(paths[0])) { | ||
_group = paths.shift(); | ||
var Dispatcher = module.exports = Class(function(){ | ||
return { | ||
http: null, | ||
init: function(http){ | ||
this.http = http; | ||
}, | ||
run: function(){ | ||
var _group = ""; | ||
var _module = ""; | ||
var _action = ""; | ||
tag("path_info", this.http); | ||
var pathname = this.http.req.pathname; | ||
var extname = path.extname(pathname); | ||
if (extname == C('url_html_suffix')) { | ||
pathname = path.dirname(pathname) + "/" + path.basename(pathname, C('url_html_suffix')); | ||
}; | ||
pathname = pathname.split("/").filter(function(item){ | ||
item = item.trim(); | ||
if (item) { | ||
return item; | ||
}; | ||
}).join("/"); | ||
//去除pathname前缀 | ||
if (C('pathname_prefix') && pathname.indexOf(C('pathname_prefix')) === 0) { | ||
pathname = pathname.substr(C('pathname_prefix').length); | ||
}; | ||
var deny = C('app_group_deny') || []; | ||
if (_group && deny.length) { | ||
if (deny.indexOf(_group)) { | ||
throw new Error(""); | ||
this.http.req.pathname = pathname; | ||
if (pathname == 'favicon.ico') { | ||
throw_error("favicon.ico"); | ||
return false; | ||
}; | ||
if (!this.routerCheck()) { | ||
var paths = pathname.split("/"); | ||
var groupList = C('app_group_list') || []; | ||
if (groupList.length) { | ||
if (groupList.indexOf(paths[0])) { | ||
_group = paths.shift(); | ||
}; | ||
}; | ||
var deny = C('app_group_deny') || []; | ||
if (_group && deny.length) { | ||
if (deny.indexOf(_group)) { | ||
throw new Error(""); | ||
}; | ||
}; | ||
_module = paths.shift(); | ||
_action = paths.shift(); | ||
if (paths.length) { | ||
for(var i = 0,length = Math.ceil(paths.length)/2; i < length; i++){ | ||
this.http.req.query[paths[i*2]] = paths[i*2+1] || ""; | ||
} | ||
}; | ||
this.http.req.group = Dispatcher.getGroup(_group); | ||
this.http.req.module = Dispatcher.getModule(_module); | ||
this.http.req.action = Dispatcher.getAction(_action); | ||
}; | ||
_module = paths.shift(); | ||
_action = paths.shift(); | ||
if (paths.length) { | ||
for(var i = 0,length = Math.ceil(paths.length)/2; i < length; i++){ | ||
__http.req.query[paths[i*2]] = paths[i*2+1] || ""; | ||
} | ||
}; | ||
__http.req.group = this.getGroup(); | ||
__http.req.module = this.getModule(); | ||
__http.req.action = this.getAction(); | ||
}; | ||
return true; | ||
}, | ||
routerCheck: function(){ | ||
tag('route_check'); | ||
return global.__behavior_value; | ||
}, | ||
getModule: function(module){ | ||
module = typeof module == 'undefined' ? _module : module; | ||
module = module || C('default_module'); | ||
if (C('url_module_map')[module]) { | ||
__http.req.module_alias = module; | ||
return C('url_module_map')[module]; | ||
}; | ||
return ucfirst(module); | ||
}, | ||
getAction: function(action){ | ||
action = typeof action == 'undefined' ? _action : action; | ||
action = action || C('default_action'); | ||
if (C('url_action_map')[action]) { | ||
__http.req.action_alias = action; | ||
return C('url_action_map')[action]; | ||
}; | ||
return action; | ||
}, | ||
getGroup: function(group){ | ||
group = typeof group == 'undefined' ? _group : group; | ||
group = group || C('default_group'); | ||
return ucfirst(group); | ||
return true; | ||
}, | ||
routerCheck: function(){ | ||
return tag('route_check', this.http); | ||
} | ||
} | ||
} | ||
module.exports = Dispatcher; | ||
}); | ||
Dispatcher.getModule = function(module){ | ||
module = module || C('default_module'); | ||
if (C('url_module_map')[module]) { | ||
return C('url_module_map')[module]; | ||
}; | ||
return ucfirst(module); | ||
}; | ||
Dispatcher.getAction = function(action){ | ||
action = action || C('default_action'); | ||
if (C('url_action_map')[action]) { | ||
return C('url_action_map')[action]; | ||
}; | ||
return action; | ||
}; | ||
Dispatcher.getGroup = function(group){ | ||
group = group || C('default_group'); | ||
return ucfirst(group); | ||
} |
@@ -10,102 +10,117 @@ /** | ||
var Http = { | ||
init: function(){ | ||
if (C('use_php_vars')) { | ||
global.$_GET = {}; | ||
global.$_POST = {}; | ||
global.$_REQUEST = {}; | ||
global.$_FILES = {}; | ||
global.$_COOKIE = {}; | ||
global.$_HTTP = {}; | ||
}; | ||
global.__http = {}; | ||
}, | ||
run: function(callback){ | ||
this.init(); | ||
this._request(); | ||
this._response(); | ||
var method = __http.req.method; | ||
var methods = ["post", "put", "patch"]; | ||
if (methods.indexOf(method)> -1) { | ||
if (C('post_data_asyn')) { | ||
callback && callback(); | ||
this.getData(); | ||
module.exports = Class(function(){ | ||
return { | ||
req: null, | ||
res: null, | ||
http: null, | ||
init: function(req, res){ | ||
this.req = req; | ||
this.res = res; | ||
this.http = {}; | ||
}, | ||
run: function(callback){ | ||
this._request(); | ||
this._response(); | ||
var method = this.req.method; | ||
var methods = ["post", "put", "patch"]; | ||
if (methods.indexOf(method)> -1) { | ||
this.getData(callback); | ||
}else{ | ||
this.getData(callback); | ||
callback && callback(this.http); | ||
} | ||
}else{ | ||
callback && callback(); | ||
} | ||
}, | ||
getData: function(callback){ | ||
var multiparty = require("multiparty"); | ||
var form = new multiparty.Form(); | ||
form.on("file", function(name, value){ | ||
__http.req.file[name] = value; | ||
}); | ||
form.on("field", function(name, value){ | ||
__http.req.post[name] = value; | ||
}); | ||
form.on("close", function(){ | ||
callback && callback(); | ||
}); | ||
form.parse(__request); | ||
}, | ||
_request: function(){ | ||
var req = { | ||
version: __request.httpVersion, | ||
statusCode: __request.statusCode, | ||
status: http.STATUS_CODES[__http.statusCode] || "", | ||
method: __request.method.toLowerCase(), | ||
headers: __request.headers, | ||
getHeader: function(name){ | ||
if (name == 'referer') { | ||
name = 'referrer'; | ||
}; | ||
return this.headers[name] || ""; | ||
}, | ||
post: {}, | ||
file: {}, | ||
ip: __request.headers["x-real-ip"] || __request.headers["x-forwarded-for"] | ||
}; | ||
}, | ||
/** | ||
* 获取POST过来的数据,包含上传的文件 | ||
* 依赖multiparty库 | ||
* @param {Function} callback [description] | ||
* @return {[type]} [description] | ||
*/ | ||
getData: function(callback){ | ||
var multiparty = require("multiparty"); | ||
var form = new multiparty.Form(); | ||
var self = this; | ||
form.on("file", function(name, value){ | ||
self.http.req.file[name] = value; | ||
}); | ||
form.on("field", function(name, value){ | ||
self.http.req.post[name] = value; | ||
}); | ||
form.on("close", function(){ | ||
callback && callback(self.http); | ||
}); | ||
form.parse(this.req); | ||
}, | ||
_request: function(){ | ||
var req = { | ||
version: this.req.httpVersion, | ||
statusCode: this.req.statusCode, | ||
status: http.STATUS_CODES[this.req.statusCode] || "", | ||
method: this.req.method.toLowerCase(), | ||
headers: this.req.headers, | ||
getHeader: function(name){ | ||
if (name == 'referer') { | ||
name = 'referrer'; | ||
}; | ||
return this.headers[name] || ""; | ||
}, | ||
post: {}, | ||
file: {}, | ||
ip: this.req.headers["x-real-ip"] || this.req.headers["x-forwarded-for"] | ||
}; | ||
//cookie | ||
req.cookie = cookie.parse(__request.headers.cookie || ""); | ||
//cookie | ||
req.cookie = cookie.parse(this.req.headers.cookie || ""); | ||
var urlInfo = url.parse(__request.url, true); | ||
extend(req, urlInfo); | ||
__http.req = req; | ||
}, | ||
_response: function(){ | ||
var res = { | ||
setHeader: function(name, value){ | ||
__response.setHeader(name, value); | ||
}, | ||
setCookie: function(name, value, options){ | ||
options = options || {}; | ||
if (typeof options == 'number') { | ||
options = { | ||
expires: options | ||
} | ||
}; | ||
var expires = options.expires; | ||
if (expires === undefined) { | ||
expires = C('cookie_expires'); | ||
}; | ||
delete options.expires; | ||
var defaultOptions = { | ||
path: "/", | ||
expires: new Date (Date.now() + expires) | ||
}; | ||
if (expires === 0) { | ||
delete defaultOptions.expires; | ||
}; | ||
options = extend(defaultOptions, options || {}); | ||
value = cookie.serialize(name, value, options); | ||
this.setHeader("Set-Cookie", value); | ||
var urlInfo = url.parse(this.req.url, true); | ||
extend(req, urlInfo); | ||
this.http.req = req; | ||
this.http.beginTime = this.req.beginTime; | ||
this.http._req = this.req; | ||
}, | ||
_response: function(){ | ||
var self = this; | ||
var res = { | ||
setHeader: function(name, value){ | ||
self.res.setHeader(name, value); | ||
}, | ||
setCookie: function(name, value, options){ | ||
options = options || {}; | ||
if (typeof options == 'number') { | ||
options = { | ||
expires: options | ||
} | ||
}; | ||
var expires = options.expires; | ||
if (expires === undefined) { | ||
expires = C('cookie_expires'); | ||
}; | ||
delete options.expires; | ||
//if value is null, remove cookie | ||
if (value === null) { | ||
expires = -1000; | ||
}; | ||
var defaultOptions = { | ||
path: "/", | ||
expires: new Date (Date.now() + expires) | ||
}; | ||
if (expires === 0) { | ||
delete defaultOptions.expires; | ||
}; | ||
options = extend(defaultOptions, options || {}); | ||
value = cookie.serialize(name, value, options); | ||
this.setHeader("Set-Cookie", value); | ||
}, | ||
end: function(){ | ||
tag("before_res_end", self.http); | ||
//end前保存session | ||
if (self.http.req.session) { | ||
self.http.req.session.flush(); | ||
}; | ||
self.http._res.end(); | ||
} | ||
} | ||
this.http.res = res; | ||
this.http._res = this.res; | ||
} | ||
__http.res = res; | ||
} | ||
}; | ||
module.exports = Http; | ||
}); |
@@ -1,2 +0,6 @@ | ||
var Think = { | ||
/** | ||
* [exports description] | ||
* @type {Object} | ||
*/ | ||
module.exports = { | ||
start: function(){ | ||
@@ -10,24 +14,21 @@ this.register_exception(); | ||
process.on('uncaughtException', function(err) { | ||
if (err instanceof Error) { | ||
var message = err.message; | ||
try{ | ||
var obj = JSON.parse(message); | ||
switch(obj.type){ | ||
case "deny": | ||
__response.statusCode = obj.code || 403; | ||
__response.write(obj.msg, C('encoding')); | ||
break; | ||
case "redirect": | ||
__response.statusCode = obj.code || 302; | ||
__response.setHeader("Location", obj.msg); | ||
break; | ||
case "error": | ||
__response.write(err.stack, C('encoding')); | ||
break; | ||
} | ||
}catch(e){ | ||
console.log(err.stack); | ||
if (typeof err && err.http) { | ||
var http = err.http; | ||
switch(err.type){ | ||
case "deny": | ||
http._res.statusCode = err.code || 403; | ||
http._res.write(err.msg, C('encoding')); | ||
break; | ||
case "redirect": | ||
http._res.statusCode = err.code || 302; | ||
http._res.setHeader("Location", err.msg); | ||
break; | ||
default: | ||
http._res.statusCode = err.code || 500; | ||
http._res.write(err.msg, C('encoding')); | ||
} | ||
err.http.res.end(); | ||
return; | ||
}; | ||
__response.end(); | ||
console.log(err); | ||
}); | ||
@@ -58,4 +59,8 @@ }, | ||
}; | ||
//debug模式 | ||
if (APP_DEBUG) { | ||
C(require(THINK_PATH + "/Conf/debug.js")); | ||
//加载debug模式下的配置 | ||
if (is_file(THINK_PATH + "/Conf/debug.js")) { | ||
C(require(THINK_PATH + "/Conf/debug.js")); | ||
}; | ||
var status = C('app_status'); | ||
@@ -65,2 +70,8 @@ if (status && is_file(CONF_PATH + "/" + status + '.js')) { | ||
}; | ||
//debug模式下实时加载,文件修改后刷新立即生效 | ||
setInterval(function(){ | ||
for(var file in require.cache){ | ||
delete require.cache[file]; | ||
} | ||
}, 100); | ||
}; | ||
@@ -75,24 +86,29 @@ }, | ||
THINK_LIB_PATH + "/Behavior/" + sysfile, | ||
EXTEND_PATH + "/Behavior/" + sysfile, | ||
LIB_PATH + "/Behavior/" + file | ||
], | ||
Model: [ | ||
LIB_PATH + "/Model/" + file, | ||
EXTEND_PATH + "/Model/" + sysfile | ||
LIB_PATH + "/Model/" + file | ||
], | ||
Action: [ | ||
LIB_PATH + "/Action/" + file, | ||
EXTEND_PATH + "/Action/" + sysfile | ||
LIB_PATH + "/Action/" + file | ||
], | ||
Cache: [ | ||
EXTEND_PATH + "/Driver/Cache/" + sysfile, | ||
LIB_PATH + "/Driver/Cache/" + file, | ||
THINK_LIB_PATH + "/Driver/Cache/" + sysfile | ||
], | ||
Db: [ | ||
EXTEND_PATH + "/Driver/Cache/" + sysfile, | ||
LIB_PATH + "/Driver/Cache/" + file, | ||
THINK_LIB_PATH + "/Driver/Cache/" + sysfile | ||
], | ||
Template: [ | ||
EXTEND_PATH + "/Driver/Template/" + sysfile, | ||
LIB_PATH + "/Driver/Template/" + file, | ||
THINK_LIB_PATH + "/Driver/Template/" + sysfile | ||
], | ||
Socket: [ | ||
LIB_PATH + "/Driver/Socket/" + file, | ||
THINK_LIB_PATH + "/Driver/Socket/" + sysfile | ||
], | ||
Session: [ | ||
LIB_PATH + "/Driver/Session/" + file, | ||
THINK_LIB_PATH + "/Driver/Session/" + sysfile | ||
] | ||
@@ -118,2 +134,1 @@ }; | ||
} | ||
module.exports = Think; |
@@ -1,6 +0,11 @@ | ||
var view = module.exports = Class(function(){ | ||
/** | ||
* view | ||
* @return {[type]} [description] | ||
*/ | ||
module.exports = Class(function(){ | ||
return { | ||
tVar: {}, | ||
init: function(){ | ||
init: function(http){ | ||
this.tVar = {}; | ||
this.http = http; | ||
}, | ||
@@ -20,8 +25,8 @@ assign: function(name, value){ | ||
}, | ||
display: function(templateFile, charset, contentType, content, prefix){ | ||
tag("view_begin"); | ||
content = this.fetch(templateFile, content, prefix); | ||
display: function(templateFile, charset, contentType, content){ | ||
tag("view_begin", this.http); | ||
content = this.fetch(templateFile, content); | ||
this.render(content, charset, contentType); | ||
tag("view_end"); | ||
__response.end(); | ||
tag("view_end", this.http); | ||
this.http.res.end(); | ||
}, | ||
@@ -35,9 +40,9 @@ render: function(content, charset, contentType){ | ||
}; | ||
__http.res.setHeader("Content-Type", contentType + "; charset="+charset); | ||
__http.res.setHeader("X-Powered-By", "thinkjs"); | ||
__response.write(content || "", C('encoding')); | ||
this.http.res.setHeader("Content-Type", contentType + "; charset="+charset); | ||
this.http.res.setHeader("X-Powered-By", "thinkjs"); | ||
this.http._res.write(content || "", C('encoding')); | ||
}, | ||
fetch: function(templateFile, content, prefix){ | ||
fetch: function(templateFile, content){ | ||
if (content === undefined) { | ||
templateFile = tag("view_template", templateFile); | ||
templateFile = tag("view_template", this.http, templateFile); | ||
if (!is_file(templateFile)) { | ||
@@ -47,9 +52,8 @@ return ""; | ||
}; | ||
content = tag("view_parse", { | ||
content = tag("view_parse", this.http, { | ||
"var": this.tVar, | ||
"file": templateFile, | ||
"content": content, | ||
"prefix": prefix | ||
"content": content | ||
}); | ||
content = tag("view_filter", content); | ||
content = tag("view_filter", this.http, content); | ||
return content; | ||
@@ -56,0 +60,0 @@ } |
@@ -7,6 +7,9 @@ /** | ||
var ejs = require("ejs"); | ||
var template = module.exports = { | ||
module.exports = { | ||
fetch: function(templateFile, tVar){ | ||
var content = file_get_contents(templateFile); | ||
var fn = ejs.compile(content, C('tpl_engine_config')); | ||
var html = fn(tVar); | ||
return html; | ||
} | ||
} |
/** | ||
* jade | ||
* jade模版引擎 | ||
* expressjs默认为该模版引擎 | ||
* @type {[type]} | ||
*/ | ||
var jade = require("jade"); | ||
var template = module.exports = { | ||
module.exports = { | ||
fetch: function(templateFile, tVar){ | ||
@@ -13,2 +15,2 @@ var content = file_get_contents(templateFile); | ||
} | ||
} | ||
}; |
@@ -6,3 +6,3 @@ /** | ||
var nsmarty = require("nsmarty"); | ||
var template = module.exports = { | ||
module.exports = { | ||
fetch: function(templateFile, tVar){ | ||
@@ -9,0 +9,0 @@ |
@@ -1,5 +0,4 @@ | ||
global._beginTime = new Date * 1; | ||
var path = require("path"); | ||
if (!global.APP_PATH) { | ||
throw new Error('define APP_PATH'); | ||
global.APP_PATH = path.dirname(__dirname) + "/App"; | ||
}else{ | ||
@@ -17,2 +16,2 @@ global.APP_PATH = path.normalize(APP_PATH); | ||
global.THINK_PATH = __dirname; | ||
require(THINK_PATH + "/Common/runtime"); | ||
require(THINK_PATH + "/Common/runtime.js"); |
@@ -1,2 +0,6 @@ | ||
var action = module.exports = Action(function(){ | ||
/** | ||
* action | ||
* @return | ||
*/ | ||
module.exports = Action(function(){ | ||
return { | ||
@@ -7,8 +11,6 @@ /*init: function(){ | ||
indexAction: function(){ | ||
this.assign({ //assign variables | ||
name: "welefen" | ||
}) | ||
this.display(); //render Home/index_index.html file | ||
this.end("hello, thinkjs!"); | ||
//this.display(); //render Home/index_index.html file | ||
} | ||
} | ||
}); |
{ | ||
"name": "thinkjs", | ||
"description": "thinkphp web framework for nodejs", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": { | ||
@@ -16,7 +16,7 @@ "name": "welefen", | ||
"dependencies": { | ||
"ejs": "0.8.4", | ||
"jade": "0.35.0", | ||
"cookie": "0.1.0", | ||
"mysql": "2.0.0-alpha9", | ||
"multiparty": "3.0.0" | ||
"multiparty": "3.0.0", | ||
"when": "2.5.1", | ||
"mime": "1.2.11" | ||
}, | ||
@@ -23,0 +23,0 @@ "keywords": [ |
@@ -1,9 +0,1 @@ | ||
var b = require("./b"); | ||
global.fn = 'welefen'; | ||
console.log(b.value()); | ||
b.change(10); | ||
console.log(b.value()); | ||
var c = require("/Users/welefen/Develop/git/thinkjs/test/b"); | ||
console.log(b.value()); | ||
c.change(20); | ||
console.log(b.value()) | ||
process.abort(); |
global.APP_PATH = __dirname + "/../App"; | ||
global.APP_DEBUG = true; | ||
require('../lib/think'); | ||
//console.log(process.argv); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
87196
51
2636
5
25
8
+ Addedmime@1.2.11
+ Addedwhen@2.5.1
+ Addedmime@1.2.11(transitive)
+ Addedwhen@2.5.1(transitive)
- Removedejs@0.8.4
- Removedmysql@2.0.0-alpha9
- Removedbignumber.js@1.0.1(transitive)
- Removedejs@0.8.4(transitive)
- Removedmysql@2.0.0-alpha9(transitive)
- Removedrequire-all@0.0.3(transitive)