#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.options import define, options

define("conf", default='report.cnf', help='tornado settings file', type=str)

import tornado.ioloop
import tornado.web
import tornado.httpserver
from common.log.loger import *
import os
#from tornado.options import define, options
from controllers.AnalysisReportHandlers import *
from nacos_py import *
import configparser

def tornado_start():
    print('===')
    nacos_main(nacosConf)
    static_path = os.path.join(os.path.dirname(__file__), "static")
    application=tornado.web.Application(
        [(r"/", IndexHandler),
         (r"/report_down/(.*)", tornado.web.StaticFileHandler, {'path':'report_down'}),#dict(basepath="/stata_down/")
         (r"/reportfile", DownWordProgressMessageReturnReportHandler),
         (r"/reportfile2", DownWordProgressMessageReturnReportHandler2),
         (r"/reportcancel", DownWordProgressMessageCancelReportHandler),
         (r"/reportname", DownWordProgressMessageReportNameHandler),
         (r"/reportdown", DownWordProgressMessageDownReportHandler),
         (r"/largeScreen", DownJsonProgressMessageLargeScreenHandler),
         # (r"/reportdown", DownWordReturnReportHandler),
         ],static_path, autoreload=False, debug=False )

    http_server=tornado.httpserver.HTTPServer(application)
    # log=logger.()
    tornado.options.parse_command_line()
    http_server.bind(options.port)
    http_server.start(10)
    # application.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

class config(object):
    """
    # Config().get_content("user_information")
    """
    def __init__(self):
        if os.path.abspath(os.curdir).find('zkey_report') > -1:
            file_path = os.path.join(os.path.abspath(os.curdir), "conf", BaseConf.BaseCnf)
        else:
            file_path = os.path.join(os.path.abspath(os.curdir), "zkey_report", "conf",BaseConf.BaseCnf)

        self.cf = configparser.ConfigParser()
        self.cf.read(file_path,encoding='utf-8')
    def get_sections(self):
        return self.cf.sections()

    def get_options(self, section):
        return self.cf.options(section)

    def get_content(self, section):
        result = {}
        for option in self.get_options(section):
            value = self.cf.get(section, option)
            result[option] = int(value) if value.isdigit() else value
        return result


serverConf = config().get_content("server")
nacosConf = config().get_content("nacos")

define("port", default=serverConf['serverport'], help="run on the given port", type=int)
testFlag = True

if __name__=="__main__":
    # 当前服务监听的端口
    tornado_start()
