'use strict';

// url类
const Url = require('url');
module.exports = options => {
    /**
     * 控制器以及动作的获取
     *
     * @param {function} next - 中间件继续执行的方法
     * @return {void}
     */
    return function* urlParse(next) {
        this.actionName = '';
        // 获取当前控制器和动作名称
        const urlInfo = Url.parse(this.request.originalUrl, true);
        const url = urlInfo.pathname.substr(1);
        const actionInfo = url.split('/');

        this.controllerName = typeof actionInfo === 'object' && actionInfo[0] !== undefined ? actionInfo[0] : '';
        this.actionName = typeof actionInfo === 'object' && actionInfo[1] !== undefined ? actionInfo[1] : '';

        this.urlInfo = urlInfo;
        // 防止为空
        this.request.headers.referer = this.request.headers.referer === undefined ? '/dashboard' : this.request.headers.referer;
        yield next;
    };
};
