fix: unregistry controller error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-07-06 17:41:50 +08:00
parent 9b28cd4de0
commit b1a4c65adb

View File

@ -25,7 +25,7 @@ export class Server {
private StreamUtils = org.springframework.util.StreamUtils private StreamUtils = org.springframework.util.StreamUtils
private ResponseEntity = org.springframework.http.ResponseEntity private ResponseEntity = org.springframework.http.ResponseEntity
private pluginControllers: Map<string, any> private controllerActions: Map<string, interfaces.ActionMetadata[]>
private interceptors: Map<string, InterceptorAdapter> private interceptors: Map<string, InterceptorAdapter>
private methodMappings: Map<string, Map<string, RequestHandler>> private methodMappings: Map<string, Map<string, RequestHandler>>
@ -34,6 +34,7 @@ export class Server {
@postConstruct() @postConstruct()
initialization() { initialization() {
this.beanFactory = base.getInstance().getAutowireCapableBeanFactory() this.beanFactory = base.getInstance().getAutowireCapableBeanFactory()
this.controllerActions = new Map()
this.interceptors = new Map() this.interceptors = new Map()
this.methodMappings = new Map() this.methodMappings = new Map()
this.start() this.start()
@ -77,8 +78,10 @@ export class Server {
if (!controllerMetadata) { throw new Error(`Controller ${target.name} must have @Controller decorator!`) } if (!controllerMetadata) { throw new Error(`Controller ${target.name} must have @Controller decorator!`) }
target = this.bindController(target) target = this.bindController(target)
let actions = getControllerActions(target) let actions = getControllerActions(target)
this.controllerActions.set(controllerMetadata.name, [])
for (const action of actions) { for (const action of actions) {
let actionMetadata = getActionMetadata(target, action) let actionMetadata = getActionMetadata(target, action)
this.controllerActions.get(controllerMetadata.name).push(actionMetadata)
let path = `${controllerMetadata.path || ''}${actionMetadata.path || ''}` let path = `${controllerMetadata.path || ''}${actionMetadata.path || ''}`
if (!path) throw new Error(`Controller ${controllerMetadata.name} Action ${actionMetadata.name} path is empty!`) if (!path) throw new Error(`Controller ${controllerMetadata.name} Action ${actionMetadata.name} path is empty!`)
if (!this.methodMappings.has(path)) { this.methodMappings.set(path, new Map()) } if (!this.methodMappings.has(path)) { this.methodMappings.set(path, new Map()) }
@ -124,19 +127,15 @@ export class Server {
if (!target) { throw new Error('Controller can\'t be undefiend!') } if (!target) { throw new Error('Controller can\'t be undefiend!') }
let controllerMetadata = getControllerMetadata(target) let controllerMetadata = getControllerMetadata(target)
if (!controllerMetadata) { throw new Error(`Controller ${target.name} must have @Controller decorator!`) } if (!controllerMetadata) { throw new Error(`Controller ${target.name} must have @Controller decorator!`) }
try { if (!this.controllerActions.has(controllerMetadata.name)) { return console.warn(`Controller ${controllerMetadata.name} not registry!`) }
target = this.container.getNamed(METADATA_KEY.Controller, target.name) let actions = this.controllerActions.get(controllerMetadata.name)
} catch (error) { for (const actionMetadata of actions) {
throw new Error(`Controller ${target.name} not registry! err: ${error}`)
}
let actions = getControllerActions(target)
for (const action of actions) {
let actionMetadata = getActionMetadata(target, action)
let path = `${controllerMetadata.path || ''}${actionMetadata.path || ''}` let path = `${controllerMetadata.path || ''}${actionMetadata.path || ''}`
if (!this.methodMappings.has(path)) { continue } if (!this.methodMappings.has(path)) { continue }
this.methodMappings.get(path).delete(actionMetadata.method) this.methodMappings.get(path).delete(actionMetadata.method)
console.debug(`Controller ${controllerMetadata.name} Unregistry ${path} Action.`) console.debug(`Controller ${controllerMetadata.name} Unregistry ${path} Action.`)
} }
this.controllerActions.delete(controllerMetadata.name)
} }
registryMapping(path: string, handler: RequestHandler) { registryMapping(path: string, handler: RequestHandler) {