kk-koa-framework
Advanced tools
Comparing version 2.1.6 to 2.1.8
@@ -10,2 +10,8 @@ { | ||
"request": "launch", | ||
"name": "测试cli", | ||
"program": "${workspaceFolder}/test/test-cli/index.js" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "启动程序", | ||
@@ -12,0 +18,0 @@ "cwd": "${workspaceFolder}/test", |
107
bin/init.js
#!/usr/bin/env node | ||
const fs = require("fs") | ||
const path = require('path') | ||
const ROOTPATH = path.resolve(__dirname,"../../../") | ||
const ROOTPATH = process.cwd() | ||
const kkRootPath = path.resolve(__dirname,"../default/") | ||
const sum = 11 | ||
const startTime=new Date().getTime() | ||
var count = 0 | ||
const isComplete = ()=>{ | ||
count++; | ||
if(count==sum){ | ||
console.log("项目初始化完成...") | ||
let spendTime = new Date().getTime() | ||
console.log(`耗时 ${spendTime-startTime} ms`) | ||
} | ||
} | ||
const copyFile = async (readFile,writeFile)=>{ | ||
@@ -24,42 +17,50 @@ let readStream = fs.createReadStream(path.join(kkRootPath,readFile)) | ||
// 创建目录 | ||
fs.mkdir(ROOTPATH+"/assets",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
}) | ||
fs.mkdir(ROOTPATH+"/config",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
fs.mkdir(ROOTPATH+"/config/env",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
copyFile("base.js","/config/env/base.js") | ||
}) | ||
copyFile("routes.js","/config/routes.js") | ||
}) | ||
fs.mkdir(ROOTPATH+"/src",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
fs.mkdir(ROOTPATH+"/src/controller",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
}) | ||
copyFile("index.js","/src/controller/index.js") | ||
}) | ||
fs.mkdir(ROOTPATH+"/views",err=>{ | ||
if(err) { | ||
console.error(err); | ||
} | ||
isComplete() | ||
copyFile("index.pug","/views/index.pug") | ||
}) | ||
copyFile("app.js","/app.js") | ||
let assetsPath = path.join(ROOTPATH,"assets") | ||
if(!fs.existsSync(assetsPath)){ | ||
fs.mkdirSync(assetsPath) | ||
} | ||
let configPath = path.join(ROOTPATH,"config") | ||
if(!fs.existsSync(configPath)){ | ||
fs.mkdirSync(configPath) | ||
} | ||
let envPath = path.join(configPath,"env") | ||
if(!fs.existsSync(envPath)){ | ||
fs.mkdirSync(envPath) | ||
} | ||
fs.copyFileSync(path.join(kkRootPath,"base.js"),path.join(envPath,"base.js")) | ||
fs.copyFileSync(path.join(kkRootPath,"routes.js"),path.join(configPath,"routes.js")) | ||
let srcPath = path.join(ROOTPATH,"src") | ||
if(!fs.existsSync(srcPath)){ | ||
fs.mkdirSync(srcPath) | ||
} | ||
let controllerPath = path.join(srcPath,"controller") | ||
if(!fs.existsSync(controllerPath)){ | ||
fs.mkdirSync(controllerPath) | ||
} | ||
fs.copyFileSync(path.join(kkRootPath,"index.js"),path.join(controllerPath,"index.js")) | ||
let viewPath = path.join(ROOTPATH,"views") | ||
if(!fs.existsSync(viewPath)){ | ||
fs.mkdirSync(viewPath) | ||
} | ||
fs.copyFileSync(path.join(kkRootPath,"index.pug"),path.join(viewPath,"index.pug")) | ||
fs.copyFileSync(path.join(kkRootPath,"app.js"),path.join(ROOTPATH,"app.js")) | ||
console.log("init ready,please enjoy it!!!") |
39
index.js
@@ -35,2 +35,8 @@ const Koa = require('koa') | ||
/** | ||
* 添加静态资源文件夹 | ||
* @param {String} p 需要映射的文件夹路径 | ||
* @param {serve.Options} option 针对静态资源的配置选项 | ||
* @returns {Framework} | ||
*/ | ||
addStatic(p,option={maxage:1000 * 60 * 60 * 24}){ | ||
@@ -47,2 +53,7 @@ if(process.env.NODE_ENV === 'development'){ | ||
/** | ||
* 设置视图目录,默认为程序运行目录下的views目录 | ||
* @param {String} p | ||
* @returns {Framework} | ||
*/ | ||
setViewPath(p){ | ||
@@ -53,2 +64,7 @@ this.app.$$.VIEW_DIR = p | ||
/** | ||
* 设置源代码目录,默认为程序运行目录下的src目录 | ||
* @param {String} p | ||
* @returns {Framework} | ||
*/ | ||
setSourcePath(p){ | ||
@@ -59,2 +75,7 @@ this.app.$$.SOURCE_DIR = p | ||
/** | ||
* 开启webpack watch | ||
* @param {webpack.Configuration} cfg | ||
* @returns {Framework} | ||
*/ | ||
enableWebpack(cfg){ | ||
@@ -65,2 +86,7 @@ this.webpack = cfg | ||
/** | ||
* 添加middleware | ||
* @param {compose.Middleware<Context>} middleware | ||
* @returns {Framework} | ||
*/ | ||
use(middleware){ | ||
@@ -71,2 +97,7 @@ this.app.use(middleware) | ||
/** | ||
* 配置应用程序 | ||
* @param {*} callback | ||
* @returns {Framework} | ||
*/ | ||
setup(callback=null){ | ||
@@ -103,2 +134,6 @@ let app = this.app | ||
/** | ||
* @private | ||
*/ | ||
compileWebpack(){ | ||
@@ -140,2 +175,6 @@ if(this.webpack){ | ||
/** | ||
* 启动监听 | ||
* @param {*} cb | ||
*/ | ||
startup(cb){ | ||
@@ -142,0 +181,0 @@ this.compileWebpack() |
@@ -6,3 +6,3 @@ { | ||
"name": "kk-koa-framework", | ||
"version": "2.1.6", | ||
"version": "2.1.8", | ||
"description": "kk koa mvc webframework", | ||
@@ -9,0 +9,0 @@ "main": "index.js", |
# kk-koa-framework | ||
## 2.1.7 版本改动 | ||
redis重试机制,改为使用retryStrategy | ||
## 2.1.5版本改动 | ||
@@ -4,0 +8,0 @@ |
@@ -8,4 +8,4 @@ const Redis = require('ioredis') | ||
class RedisClient{ | ||
constructor(config){ | ||
class RedisClient { | ||
constructor(config) { | ||
let { | ||
@@ -17,41 +17,41 @@ port, | ||
} = config || {} | ||
let redis = null | ||
let pingSi = null | ||
const connect = () => { | ||
redis = new Redis({ | ||
port, | ||
host, | ||
password, | ||
db, | ||
}) | ||
this.redis = redis | ||
redis.on('error', (e) => { | ||
console.error('redis connect error:', e) | ||
try { | ||
redis.end() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
redis = null | ||
connect() | ||
}) | ||
redis.ping() | ||
pingSi && clearInterval(pingSi) | ||
pingSi = setInterval(function () { | ||
redis.ping() | ||
}, 60 * 1000) | ||
// let redis = null | ||
// let pingSi = null | ||
} | ||
connect() | ||
// const connect = () => { | ||
let redis = new Redis({ | ||
port, | ||
host, | ||
password, | ||
db, | ||
retryStrategy: function (times) { | ||
console.warn('reids connection lost! retry:',times) | ||
var delay = Math.min(times * 50, 2000) | ||
return delay | ||
} | ||
}) | ||
this.redis = redis | ||
redis.on('error', (e) => { | ||
console.error('redis connect error:', e) | ||
}) | ||
// redis.ping() | ||
// pingSi && clearInterval(pingSi) | ||
// pingSi = setInterval(function () { | ||
// redis.ping() | ||
// }, 60 * 1000) | ||
// } | ||
// connect() | ||
} | ||
static create(config,key='redis'){ | ||
if(pools[key]){ | ||
static create(config, key = 'redis') { | ||
if (pools[key]) { | ||
return pools[key] | ||
@@ -61,6 +61,6 @@ } | ||
return pools[key] | ||
return pools[key] | ||
} | ||
get client(){ | ||
get client() { | ||
return this.redis | ||
@@ -72,3 +72,6 @@ } | ||
class RedisStore { | ||
constructor(redis,{ttl,prefix}={}) { | ||
constructor(redis, { | ||
ttl, | ||
prefix | ||
} = {}) { | ||
this.ttl = ttl | ||
@@ -97,3 +100,3 @@ this.redis = redis | ||
await this.redis.set(id, JSON.stringify(obj)) | ||
ttl = ttl || this.ttl | ||
ttl = ttl || this.ttl | ||
ttl && this.redis.expire(id, ttl) | ||
@@ -106,3 +109,3 @@ } catch (e) { | ||
async refresh(sid){ | ||
async refresh(sid) { | ||
let id = `${this.prefix}:${sid}` | ||
@@ -117,2 +120,5 @@ this.ttl && this.redis.expire(id, this.ttl) | ||
module.exports = {RedisClient,RedisStore} | ||
module.exports = { | ||
RedisClient, | ||
RedisStore | ||
} |
@@ -11,2 +11,3 @@ const Koa = require('../index') | ||
passport.use(new RegisterStrategy(()=>{},({username,password},done)=>{ | ||
@@ -22,6 +23,8 @@ console.log('enter register') | ||
}, (params) => { | ||
console.log("param:", params) | ||
console.log('param:', params) | ||
return params | ||
})) | ||
app.use() | ||
app.setup() | ||
@@ -28,0 +31,0 @@ |
114184
46
1169
377