72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
from datetime import datetime, date
|
|
from json import JSONEncoder
|
|
|
|
from flask import Flask
|
|
|
|
""" 程序入口"""
|
|
|
|
|
|
class Singleton(type):
|
|
_instances = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
if cls not in cls._instances:
|
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
return cls._instances[cls]
|
|
|
|
|
|
class App(Flask):
|
|
__metaclass__ = Singleton
|
|
|
|
def __init__(self, import_name=__name__, **kwargs):
|
|
super().__init__(import_name, **kwargs)
|
|
self.bootstrap()
|
|
|
|
def bootstrap(self):
|
|
pass
|
|
|
|
|
|
class CustomJSONEncoder(JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, datetime):
|
|
return obj.strftime('%Y-%m-%d %H:%M:%S')
|
|
elif isinstance(obj, date):
|
|
return obj.strftime('%Y-%m-%d')
|
|
else:
|
|
return JSONEncoder.default(self, obj)
|
|
|
|
|
|
class ApiApp(App):
|
|
def bootstrap(self):
|
|
with self.app_context():
|
|
import config
|
|
import api
|
|
from core.extensions import db, siwa
|
|
self.config.from_object(config)
|
|
# if config.SENTRY_DSN:
|
|
# sentry.init_app(self)
|
|
|
|
# if config.CELERY_BROKER_URL:
|
|
# celery.init_app(self)
|
|
|
|
db.init_app(self)
|
|
|
|
siwa.init_app(self)
|
|
|
|
api.init_logger(self)
|
|
api.init_app(self)
|
|
|
|
self.json_encoder = CustomJSONEncoder
|
|
|
|
|
|
class ClientApp(App):
|
|
def bootstrap(self):
|
|
super().bootstrap()
|
|
with self.app_context():
|
|
import config
|
|
from core.extensions import db
|
|
|
|
self.config.from_object(config)
|
|
|
|
db.init_app(self)
|