main.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from configure import configure, conf
  2. import os
  3. import logging
  4. import threading
  5. env_dict = os.environ
  6. hblog_conf = env_dict.get("HBLOG_CONF")
  7. if hblog_conf is None:
  8. logging.info("Configure file ./etc/conf.json")
  9. configure("./etc/conf.json")
  10. else:
  11. logging.info(f"Configure file {hblog_conf}")
  12. configure(hblog_conf)
  13. from app import HBlogFlask
  14. from waitress import serve
  15. app = HBlogFlask(__name__)
  16. app.register_all_blueprint()
  17. from sql.cache import restart_clear_cache
  18. from sql.cache_refresh import refresh
  19. restart_clear_cache() # 清理缓存
  20. @app.before_first_request
  21. def before_first_requests():
  22. class FirstRefresh(threading.Thread):
  23. def __init__(self):
  24. super(FirstRefresh, self).__init__()
  25. self.daemon = True # 设置为守护进程
  26. def run(self):
  27. refresh()
  28. class TimerRefresh(threading.Timer):
  29. def __init__(self):
  30. super(TimerRefresh, self).__init__(conf["CACHE_REFRESH_INTERVAL"], refresh)
  31. self.daemon = True # 设置为守护进程
  32. FirstRefresh().start()
  33. TimerRefresh().start()
  34. if __name__ == '__main__':
  35. logging.info("Server start on 127.0.0.1:8080")
  36. serve(app, host='0.0.0.0', port="8080")