
import * as fastify from 'fastify'
import { Singleton, Inject } from "typescript-ioc";
import { HandlerInterceptor } from "./HandlerInterceptor";
import { LoginInterceptor } from "./LoginInterceptor";
import { Context, logger } from "mslib";

@Singleton
class HandlerExecutior {

    private interceptors: Array<HandlerInterceptor> = []
    //登录拦截器，暂时没想到怎么动态加载拦截器
    @Inject
    private loginInterceptor: LoginInterceptor

    constructor() {
        this.loadInterceptors();
    }

    async preService(request, reply): Promise<boolean> {
        for (const interceptor of this.interceptors) {
            try {
                return interceptor.preService(request, reply)
            } catch (e) {
                logger.error("ERROR=====>", e)
            }
        }

    }

    private loadInterceptors() {

        this.interceptors.push(this.loginInterceptor)

    }

}

export { HandlerExecutior }