def start_info(): """ 启动时, 打印系统信息 :return: """ width = 30 height = 15 logo_path = "{}/sys_imgs/osroom-logo-white.png".format(STATIC_PATH) if os.path.exists(logo_path): im = Image.open(logo_path) im = im.resize((width, height), Image.NEAREST) txt = "" for i in range(height): txt += " " * 15 for j in range(width): ch = get_char(*im.getpixel((j, i))) if ch == "*": ch = "\033[1;37m{}\033[0m".format(ch) else: ch = "\033[1;33m{}\033[0m".format(ch) txt += ch txt += '\n' start_info_print(txt) version = VERSION info = """ Welcome to use the osroom. osroom v{} osroom website: \033[1;34m https://osroom.com \033[0m Project code download: \033[1;34m https://github.com/osroom/osroom \033[0m License: BSD3 The operating system: {} """.format(version, platform.system()) start_info_print(info)
def init_core_module(app, **kwargs): """ 初始化核心模块 :param app: :return: """ csrf_enabled = kwargs.get("csrf_enabled") is_debug = kwargs.get("is_debug") # app config web_start_log.info("Initialize the core module") # 系统必要配置, 优先导入 app.config.from_object(ConfDictToClass(CONFIG["system"], key="value")) app.config.from_object(ConfDictToClass(CONFIG["key"], key="value")) # 数据库 redis.init_app() app.config.from_object(DatabaseConfig()) for name, mdb in mdbs.items(): mdb.init_app(app, config_prefix=name.upper()) # 缓存 app.config.from_object(ConfDictToClass(CONFIG["cache"], key="value")) app.config["CACHE_REDIS"] = redis app.config["CACHE_MONGODB_DBS"] = mdbs["sys"].dbs cache.init_app(app) # Clear CONFIG cache if not is_debug: version_info = mdbs["sys"].db.sys_config.find_one( {"new_version": { "$exists": True }}) ago_time = time.time() - 3600 * 24 ago_time_30m = time.time() - 1800 if version_info["sys_version_of_config"] >= SYS_CONFIG_VERSION \ and version_info["update_time"] > ago_time \ and version_info["update_time"] < ago_time_30m: # 系统正在使用的SYS_CONFIG_VERSION版本和当前机器CONFIG的一样,或更高 # And: 配置24小时内已有更新 # So: 这次不更新 msg = " * [sys configs cache] Not clean cache." \ " The system is using the same or higher configuration version.\n" \ " And it was executed within 24 hours." start_info_print("\033[33m{}\033[0m".format(msg)) web_start_log.warning(msg) else: with app.app_context(): msg = " * Clean configuration cache successfully" cache.delete(CONFIG_CACHE_KEY) cache.delete(PLUG_IN_CONFIG_CACHE_KEY) web_start_log.info(msg) start_info_print(msg) # 异常错误信息 app.config["PRESERVE_CONTEXT_ON_EXCEPTION"] = PRESERVE_CONTEXT_ON_EXCEPTION ################################################### # 在此之前, 任何程序不能调用utils.get_config.py下的方法 ################################################### from apps.core.utils.get_config import get_configs, get_config from apps.core.flask.request import OsrRequestProcess from apps.core.flask.errorhandler import ErrorHandler from apps.core.blueprint import api, admin_view, theme_view, static_html_view, \ static, open_api, admin_static_file from apps.core.flask.routing import RegexConverter from apps.core.flask.routing import push_url_to_db # 最大请求大小 app.config["MAX_CONTENT_LENGTH"] = get_config( "system", "MAX_CONTENT_LENGTH") * 1024 * 1024 # Session会话配置 session_config = get_configs("session") session_config["SESSION_PROTECTION"] = SESSION_PROTECTION session_config["SESSION_COOKIE_PATH"] = SESSION_COOKIE_PATH session_config["SESSION_COOKIE_HTTPONLY"] = SESSION_COOKIE_HTTPONLY session_config["SESSION_COOKIE_SECURE"] = SESSION_COOKIE_SECURE session_config["SESSION_USE_SIGNER"] = SESSION_USE_SIGNER session_config["SESSION_MONGODB_DB"] = mdbs["sys"].name app.config.from_object(ConfDictToClass(session_config)) app.config["SESSION_REDIS"] = redis app.config["SESSION_MONGODB"] = mdbs["sys"].connection sess.init_app(app) rest_session.init_app(app) # 邮件 app.config.from_object(ConfDictToClass(get_configs("email"))) mail.init_app(app) # Csrf token csrf_config = {} if csrf_enabled: csrf_config["CLIENT_TOKEN_AUTH_ENABLED"] = True start_info_print(" * Security authentication is turned on") else: csrf_config["CLIENT_TOKEN_AUTH_ENABLED"] = False start_info_print( "\033[31m WARNING: security verification is turned off\033[0m") # 这两个csrf参数这里关闭,request程序会根据CLIENT_TOKEN_AUTH_ENABLED判断处理 csrf_config["WTF_CSRF_CHECK_DEFAULT"] = False csrf_config["CSRF_ENABLED"] = False csrf_config["WTF_CSRF_METHODS"] = WTF_CSRF_METHODS app.config.from_object(ConfDictToClass(csrf_config)) csrf.init_app(app) # Babel app.config.from_object(ConfDictToClass(get_configs("babel"))) app.config["BABEL_TRANSLATION_DIRECTORIES"] = BABEL_TRANSLATION_DIRECTORIES babel.init_app(app) # 登录管理 login_manager.init_app(app) # login_manager.anonymous_user = AnonymousUser() login_manager.session_protection = SESSION_PROTECTION # oauth.init_app(app) # 让路由支持正则 app.url_map.converters['regex'] = RegexConverter # 注册蓝图 blueprint web_start_log.info("Register blueprint, Initialize the routing") app.register_blueprint(api) app.register_blueprint(open_api) app.register_blueprint(admin_view) app.register_blueprint(theme_view) app.register_blueprint(static_html_view) app.register_blueprint(static) app.register_blueprint(admin_static_file) if not is_debug: st = time.time() push_url_to_db(app) start_info_print( " * Routing updates saved in the database. It tasks time {} sec". format(int(time.time() - st))) celery.conf.update(app.config) # 请求处理 request_process = OsrRequestProcess() request_process.init_request_process(app=app) request_process.init_babel_locale_selector(babel=babel) # 错误处理 ErrorHandler(app) # Logger # Other log_udp = LogServerUDP() r = log_udp.init_app() if r: log_udp.log_server() weblog = WebLogger() weblog.init_app(app) if not os.path.exists(TEMP_STATIC_FOLDER): os.makedirs(TEMP_STATIC_FOLDER)
def init_before_startup(is_debug, csrf_enabled): """ 启动前初始化相关数据 :param is_debug: :param csrf_enabled: :return: """ start_info() start_info_print("\033[1;36m osroom staring...\033[0m") # 网站还未启动的时候, 临时连接数据库, 更新collections & 系统配置 from apps.core.utils.update_sys_data import update_mdb_collections, init_datas, compatible_processing database = DatabaseConfig() mdbs = {} # 创建局部临时数据库对象 for k, mdb_acc in DB_CONFIG["mongodb"].items(): mdbs[k] = MyMongo() # 初始化2次,第一次初始化是为了更新mdb的collections # 如果第一次更新后存在新的collections,需要再次初始化数据库供其他程序使用 db_init = 2 while db_init: try: for name, mdb in mdbs.items(): if db_init == 1: mdb.close() if name not in ["sys", "user", "web"]: msg = "[Error]: 由v1.x.x更新到v2.x.x需要请更新你的数据库配置文件apps/configs/db_config.py." \ "请参考同目录下的db_config_sample.py" start_info_print('\033[31m{}\033[0m'.format(msg)) sys.exit() mdb.init_app(config_prefix=name.upper(), db_config=database.__dict__["{}_URI".format( name.upper())]) except OperationFailure as e: msg = "\n[Mongodb] *{}\nMongodb validation failure, the user name, " \ "password mistake or database configuration errors.\n" \ "Tip: to open database authentication configuration".format(e) start_info_print('\033[31m{}\033[0m'.format(msg)) sys.exit(-1) if db_init == 2 and is_debug: # 更新数据库文档表 start_info_print(" * Check or update the database collection") update_mdb_collections(mdbs=mdbs) else: # 未更新数据库coll,无需二次初始化数据库,直接break break db_init -= 1 if not is_debug: # 更新配置文件 from apps.core.flask.update_config_file import update_config_file start_info_print(" * Update and sync config.py") r = update_config_file(mdbs=mdbs) if not r: start_info_print( "[Error] Update profile error, check log sys_start.log") sys.exit(-1) else: msgs = " * The following services need to be run in a non-debugger state.\n" \ " Including the following services:- Automatic update of Mongodb collections.\n" \ " - Automatic update of website routing rights control.\n" \ " - Automatically update and merge system configuration.\n\n" warning_msg = "\033[03m If the program runs incorrectly because the above configuration \n" \ " is not updated, you need to remove the debugger running program \n" \ " first to implement the update. After that, you can continue to run \n" \ " the program under the debugger." start_info_print('\033[33m{}{}\033[0m'.format(msgs, warning_msg)) # 调用兼容程序step 1 compatible_processing(mdbs=mdbs, stage=1) # 调用初始化数据 init_datas(mdbs=mdbs) for mdb in mdbs.values(): mdb.close() # 核心程序初始化+模块加载 from apps.core.flask.module_import import module_import from apps.init_core_module import init_core_module from apps.configs.sys_config import MODULES init_core_module(app, csrf_enabled=csrf_enabled) module_import(MODULES) # 调用兼容程序step 2 from apps.app import mdbs compatible_processing(mdbs=mdbs, stage=2) if not is_debug: start_info_print( " * Signal:(SIGCHLD, SIG_IGN).Prevent child processes from becoming [Defunct processes]." "(Do not need to comment out)") signal(SIGCHLD, SIG_IGN) start_info_print(" * Started successfully") else: start_info_print(" * Debugger: Started successfully")