This commit is contained in:
robin
2022-04-07 19:33:31 +08:00
parent a593253a63
commit c2f8473fb5
17 changed files with 271 additions and 133 deletions
+53
View File
@@ -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,
}
+129
View File
@@ -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,
}
}
}
+18
View File
@@ -0,0 +1,18 @@
//入口文件
require('@babel/register')({
presets: ['@babel/env'],
plugins: [
'@babel/plugin-transform-runtime',
[
'babel-plugin-webpack-alias',
{
"config": "./webpack.config.js"
}
]
]
})
require('@babel/polyfill')
const bamboo = require('./bamboo/index')
const app = new bamboo();
// app.listen(3000);