up
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
doc : "",
|
||||
path : "/",
|
||||
method : ["get", "post"],
|
||||
middleware: [],
|
||||
params : {},
|
||||
return : {},
|
||||
controller: async (ctx, app) => {
|
||||
|
||||
ctx.body = 'index,xe:' + app.utils.VERSION
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
const logger = require('koa-logger')
|
||||
module.exports = logger()
|
||||
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
/*响应时间*/
|
||||
module.exports = async (ctx, next, app) => {
|
||||
// console.log(ctx, next, options);
|
||||
// const timetaken = `${ctx.request.method}${ctx.request.url} 响应时间`
|
||||
// console.time(timetaken)
|
||||
// app.logger.debug("123")
|
||||
throw new app.err.HttpException
|
||||
await next()
|
||||
// console.timeEnd(timetaken)
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
//bamboo入口
|
||||
import Koa from 'koa';
|
||||
const app = new Koa();
|
||||
app.listen(3000);
|
||||
// module.exports = class bamboo {
|
||||
//
|
||||
// }
|
||||
@@ -1 +1,4 @@
|
||||
/*应用配置*/
|
||||
module.exports = {
|
||||
port: 3000,
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
'use strict';
|
||||
module.exports = {
|
||||
keys: '',
|
||||
listen: {
|
||||
port: 32006,
|
||||
host: '0.0.0.0',
|
||||
},
|
||||
// 配置需要的中间件,数组顺序即为中间件的加载顺序
|
||||
middleware: ['jiujianJwt'],
|
||||
mysql: {
|
||||
host: '192.168.1.25',
|
||||
port: 3306,
|
||||
username: 'shichu',
|
||||
password: 'zXbAhBA26anGGEhY',
|
||||
},
|
||||
mongo: {},
|
||||
redis: {
|
||||
path: '',
|
||||
port: 32005,
|
||||
hostname: '0.0.0.0',
|
||||
},
|
||||
sequelize: {},
|
||||
/** 关闭csrf,否则api post将无法访问 */
|
||||
csrf: false,
|
||||
jwt: {
|
||||
secret: "_shichu",//自定义 token 的加密条件字符串
|
||||
expiresIn: "30d",//表示有效期 不带单位默认为秒 如带单位如: "2 days", "10h", "7d"
|
||||
},
|
||||
/** cors 允许跨域 */
|
||||
cors: {
|
||||
origin: '*',
|
||||
credentials: true,
|
||||
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
|
||||
},
|
||||
/** multipart 文件上传 */
|
||||
multipart: {
|
||||
whitelist: [// 文件白名单
|
||||
// images
|
||||
'.jpg', '.jpeg', // image/jpeg
|
||||
'.png', // image/png, image/x-png
|
||||
// '.gif', // image/gif
|
||||
// '.bmp', // image/bmp
|
||||
// '.wbmp', // image/vnd.wap.wbmp
|
||||
// '.webp',
|
||||
// '.tif',
|
||||
// '.psd',
|
||||
// // text
|
||||
// '.svg',
|
||||
// '.js', '.jsx',
|
||||
// '.json',
|
||||
// '.css', '.less',
|
||||
// '.html', '.htm',
|
||||
// '.xml',
|
||||
// // tar
|
||||
// '.zip',
|
||||
// '.gz', '.tgz', '.gzip',
|
||||
// // video
|
||||
// '.mp3',
|
||||
// '.mp4',
|
||||
// '.avi',
|
||||
'.p12',
|
||||
],
|
||||
},
|
||||
axios: {
|
||||
// can set more config in headers,like token,references and so on
|
||||
headers: {
|
||||
common: {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
// 添加认证【例如】,也可以在请求拦截器中修改具体的request config
|
||||
// 'Authorization':'19980115_520' // 不要问我19980115是什么,当然是女朋友生日呀!!!
|
||||
},
|
||||
// 可以设置请求头等属性
|
||||
},
|
||||
// 定义请求拦截器处理方法【可选】
|
||||
// requestInterceptorsHandler: config => {
|
||||
// console.log('定义请求拦截器处理方法',config);
|
||||
// 请求之前的配置信息
|
||||
// 当该字段【函数】不存在是,默认如下:
|
||||
// app.coreLogger.debug(`[egg-axios-plus] send request, baseURL: ${JSON.stringify(baseURL)}, url: ${url}, method: ${method}, data: ${JSON.stringify(data)}, headers: ${JSON.stringify(headers)}`);
|
||||
// return config;
|
||||
// },
|
||||
// requestInterceptorsErrorHandler: error => {
|
||||
// // 请求之后发生的错误信息
|
||||
// // 当该字段【函数】不存在是,默认如下:
|
||||
// app.coreLogger.error(`[egg-axios-plus] send request error, ${error.message}`);
|
||||
// return Promise.reject(error);
|
||||
// },
|
||||
// // 定义axios响应拦截器处理方法【可选】
|
||||
// responseInterceptorsHandler: response => {
|
||||
// // response 响应结果
|
||||
// // 当该字段【函数】不存在是,默认如下:
|
||||
// app.coreLogger.debug(`[egg-axios-plus] receive response, data: ${JSON.stringify(response.data)}, status: ${response.status}, headers: ${JSON.stringify(response.headers)}`);
|
||||
// if (response.config && (response.method.toUpperCase() === 'HEAD' || response.method.toUpperCase() === 'options')) {
|
||||
// return response;
|
||||
// }
|
||||
// return response.data;
|
||||
// },
|
||||
responseInterceptorsErrorHandler: error => {
|
||||
// 接口响应失败的错误结果
|
||||
// 当该字段【函数】不存在是,默认如下:
|
||||
// app.coreLogger.error(`[egg-axios-plus] receive response error, ${error.message}`);
|
||||
console.log(`[egg-axios-plus] receive response error, ${error.message}`);
|
||||
return Promise.reject(error);
|
||||
},
|
||||
timeout: 12000, // 默认请求超时
|
||||
app: true, // 在app.js上启动加载
|
||||
agent: false, // 在agent.js上启动加载
|
||||
}
|
||||
};
|
||||
@@ -1 +1,35 @@
|
||||
/*日志配置*/
|
||||
'use strict';
|
||||
module.exports = {
|
||||
replaceConsole: true,
|
||||
pm2 : true,
|
||||
appenders : {
|
||||
stdout: {//控制台输出
|
||||
type: 'console'
|
||||
},
|
||||
req : { //请求转发日志
|
||||
type : 'dateFile', //指定日志文件按时间打印
|
||||
filename : 'logs/req/req', //指定输出文件路径
|
||||
pattern : 'yyyy-MM-dd.log',
|
||||
alwaysIncludePattern: true
|
||||
},
|
||||
err : { //错误日志
|
||||
type : 'dateFile',
|
||||
filename : 'logs/err/err',
|
||||
pattern : 'yyyy-MM-dd.log',
|
||||
alwaysIncludePattern: true
|
||||
},
|
||||
oth : { //其他日志
|
||||
type : 'dateFile',
|
||||
filename : 'logs/oth/oth',
|
||||
pattern : 'yyyy-MM-dd.log',
|
||||
alwaysIncludePattern: true
|
||||
}
|
||||
|
||||
},
|
||||
categories : {
|
||||
//appenders:采用的appender,取appenders项,level:设置级别
|
||||
default: {appenders: ['stdout', 'req'], level: 'debug'},
|
||||
err : {appenders: ['stdout', 'err'], level: 'error'},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
'use strict';
|
||||
import path from 'path';
|
||||
import bodyParser from 'koa-bodyparser';
|
||||
import staticFile from 'koa-static';
|
||||
export default {
|
||||
bodyParser: bodyParser(),
|
||||
staticFile: staticFile(path.resolve('pbulic')),
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
time: {
|
||||
params: '123'
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
/*redis配置*/
|
||||
module.exports = {
|
||||
port: 3000,
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
class HttpException extends Error {
|
||||
// message为异常信息,errorCode为错误码(开发人员内部约定),code为HTTP状态码
|
||||
constructor(message = '服务器异常', errorCode = 10000, code = 400) {
|
||||
super()
|
||||
this.errorCode = errorCode || 10000
|
||||
this.code = code || 400
|
||||
this.message = message || '服务器异常'
|
||||
}
|
||||
}
|
||||
|
||||
class ParameterException extends HttpException {
|
||||
constructor(message, errorCode) {
|
||||
super()
|
||||
this.errorCode = errorCode || 10000
|
||||
this.code = 400
|
||||
this.message = message || '参数错误'
|
||||
}
|
||||
}
|
||||
|
||||
class NotFound extends HttpException {
|
||||
constructor(message, errorCode) {
|
||||
super()
|
||||
this.errorCode = errorCode || 10001
|
||||
this.code = 404
|
||||
this.message = message || '资源未找到'
|
||||
}
|
||||
}
|
||||
|
||||
class AuthFailed extends HttpException {
|
||||
constructor(message, errorCode) {
|
||||
super()
|
||||
this.errorCode = errorCode || 10002
|
||||
this.message = message || '授权失败'
|
||||
this.code = 401
|
||||
}
|
||||
}
|
||||
|
||||
class Forbidden extends HttpException {
|
||||
constructor(message, errorCode) {
|
||||
super()
|
||||
this.errorCode = errorCode || 10003
|
||||
this.message = message || '禁止访问'
|
||||
this.code = 403
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
HttpException,
|
||||
ParameterException,
|
||||
NotFound,
|
||||
AuthFailed,
|
||||
Forbidden,
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
'use strict'
|
||||
//bamboo入口
|
||||
import Koa from 'koa';
|
||||
//https://x-extends.gitee.io/xe-utils/#/
|
||||
import xe from 'xe-utils'
|
||||
|
||||
const log4js = require("log4js");
|
||||
const path = require('path')
|
||||
const error = require('./errorException')
|
||||
const requireDirectory = require("require-directory");
|
||||
const Router = require("koa-router");
|
||||
|
||||
|
||||
module.exports = class Bamboo extends Koa {
|
||||
constructor(agrs, options) {
|
||||
super(options)
|
||||
super.on('error', (err, ctx) => this.serverError(err, ctx));
|
||||
this.config = {}
|
||||
this.utils = this.registeredContextUtils(xe)
|
||||
this.logger = null
|
||||
this.registeredConfig()
|
||||
this.setLogger()
|
||||
this.registeredMiddleware()
|
||||
this.registeredRouter()
|
||||
this.listen(8884)
|
||||
}
|
||||
|
||||
listen(args) {
|
||||
const isNumber = this.isNumber(args) || !args
|
||||
const host = isNumber ? "127.0.0.1" : args.host
|
||||
const path = isNumber ? "/" : args.host
|
||||
const port = isNumber ? args : args.host
|
||||
console.log(`bamboo:`)
|
||||
console.log(`http://${host}:${port}${path}`)
|
||||
return super.listen(args)
|
||||
}
|
||||
|
||||
//注册config
|
||||
registeredConfig() {
|
||||
console.log('注册 config');
|
||||
const hash = requireDirectory(module, this.path('config'));
|
||||
this.config = hash
|
||||
}
|
||||
|
||||
//注册 router
|
||||
registeredRouter() {
|
||||
console.log('注册 router');
|
||||
const router = new Router();
|
||||
const hash = requireDirectory(module, this.path('app/controller'), {
|
||||
visit: (c) => {
|
||||
for (let methodElement of c.method) {
|
||||
router[methodElement](c.path, async (ctx, next) => {
|
||||
ctx['logger'] = this.logger
|
||||
await c.controller(ctx, this.application)
|
||||
next();
|
||||
});
|
||||
}
|
||||
return c
|
||||
}
|
||||
});
|
||||
super.use(router.routes())
|
||||
super.use(router.allowedMethods())
|
||||
}
|
||||
|
||||
//注册 middleware
|
||||
registeredMiddleware() {
|
||||
console.log('注册 middleware');
|
||||
const hash = requireDirectory(module, this.path('app/middleware'), {
|
||||
visit: (obj, joined, filename) => {
|
||||
console.log(obj, joined, filename);
|
||||
const parse = path.parse(filename);
|
||||
const config = this.config.middleware[parse.name]
|
||||
// super.use(obj(config || {}))
|
||||
super.use(async (ctx, next) => {
|
||||
ctx['logger'] = this.logger
|
||||
return await obj(ctx, next, this.application)
|
||||
})
|
||||
return obj
|
||||
}
|
||||
});
|
||||
console.log(hash);
|
||||
}
|
||||
|
||||
//注册 utils
|
||||
registeredContextUtils(args) {
|
||||
console.log('注册 utils');
|
||||
console.log('xe version', this.xe.VERSION);
|
||||
return args
|
||||
}
|
||||
|
||||
//logger
|
||||
setLogger(args) {
|
||||
log4js.configure(this.config['log']);
|
||||
this.logger = log4js.getLogger();
|
||||
}
|
||||
|
||||
path(args) {
|
||||
return this.isPath + '/' + args
|
||||
}
|
||||
|
||||
isNumber(args) {
|
||||
return typeof args === 'number'
|
||||
}
|
||||
|
||||
isString(args) {
|
||||
return typeof args === 'string'
|
||||
}
|
||||
|
||||
isObject(args) {
|
||||
return typeof args === 'object'
|
||||
}
|
||||
|
||||
|
||||
get isPath() {
|
||||
return path.resolve('./')
|
||||
}
|
||||
|
||||
get xe() {
|
||||
return xe
|
||||
}
|
||||
|
||||
get application() {
|
||||
return {
|
||||
utils : this.utils,
|
||||
logger: this.logger,
|
||||
err : error,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,4 +13,6 @@ require('@babel/register')({
|
||||
]
|
||||
})
|
||||
require('@babel/polyfill')
|
||||
require('./bamboo/index')
|
||||
const bamboo = require('./bamboo/index')
|
||||
const app = new bamboo();
|
||||
// app.listen(3000);
|
||||
+1
-1
@@ -5,5 +5,5 @@
|
||||
".idea",
|
||||
"node_modules/**/node_modules"
|
||||
],
|
||||
"exec": "node bin/main.js"
|
||||
"exec": "node lib/main.js"
|
||||
}
|
||||
|
||||
+7
-3
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "Koa-learn",
|
||||
"name": "bamboo",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon",
|
||||
"start": "node bin/main.js",
|
||||
"start": "node lib/main.js",
|
||||
"server": "cross-env NODE_ENV=development nodemon bin/main.js"
|
||||
},
|
||||
"keywords": [],
|
||||
@@ -44,12 +44,16 @@
|
||||
"clean-webpack-plugin": "^4.0.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"koa-bodyparser": "^4.3.0",
|
||||
"koa-logger": "^2.0.1",
|
||||
"koa-redis": "^4.0.1",
|
||||
"log4js": "^6.4.4",
|
||||
"pm2": "^5.1.2",
|
||||
"require-all": "^3.0.0",
|
||||
"require-directory": "^2.1.1",
|
||||
"shelljs": "^0.8.5",
|
||||
"webpack": "^4.41.5",
|
||||
"webpack-cli": "3.3.10",
|
||||
"webpack-node-externals": "^3.0.0"
|
||||
"webpack-node-externals": "^3.0.0",
|
||||
"xe-utils": "^3.5.4"
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: 'bamboo_manage', // 项目名
|
||||
script: './bin/main.js', // 执行文件
|
||||
script: './lib/main.js', // 执行文件
|
||||
cwd: './', // 根目录
|
||||
args: '', // 传递给脚本的参数
|
||||
interpreter: '', // 指定的脚本解释器
|
||||
|
||||
+3
-3
@@ -5,13 +5,13 @@ const webpackconfig = {
|
||||
target: 'node',
|
||||
mode: 'development',
|
||||
entry: {
|
||||
server: path.resolve('bin/main.js')
|
||||
server: path.resolve('lib/main.js')
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.js', '.jsx', '.ts', '.tsx'],
|
||||
alias: {
|
||||
'app': path.resolve('app'),
|
||||
'bin': path.resolve('bin'),
|
||||
'lib': path.resolve('lib'),
|
||||
'config': path.resolve('config'),
|
||||
'middleware': path.resolve('middleware'),
|
||||
'public': path.resolve('public'),
|
||||
@@ -48,4 +48,4 @@ const webpackconfig = {
|
||||
path: true
|
||||
}
|
||||
}
|
||||
module.exports = webpackconfig
|
||||
module.exports = webpackconfig
|
||||
|
||||
Reference in New Issue
Block a user