Created
January 25, 2019 03:54
-
-
Save vvvhung/66f26008b0166caa5ebdae334e95b418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Connection: | |
config = None | |
conn = None | |
def __init__(self, config): | |
self.parse_config(config) | |
def parse_config(self, config): | |
pass | |
def get_conn(self): | |
pass | |
def get_client(self): | |
pass | |
def __enter__(self): | |
try: | |
self.get_conn() | |
except Exception as err: | |
raise err | |
return self.conn | |
def __exit__(self, _type, _value, _traceback): | |
if self.conn is not None: | |
self.conn.close() | |
class BasicResource: | |
def __init__(self, config): | |
self.config = config | |
def on_options(self, req, resp): | |
resp.set_header('Access-Control-Allow-Origin', self.config.get('app_client', '*')) | |
resp.set_header('Access-Control-Allow-Methods', 'POST') | |
resp.set_header('Access-Control-Allow-Headers', | |
'access-control-allow-origin, access-control-allow-headers') | |
@falcon.before(hook) | |
def on_post(self, req, resp): | |
try: | |
data = json.loads(req.stream.read()) | |
data['context'] = req.context | |
data['user_agent'] = str(req.user_agent) | |
data['ip_address'] = req.remote_addr | |
data['cookies'] = req.cookies | |
data['validation'] = self.validate(data) | |
res = self.handle(data) | |
resp.set_header('Access-Control-Allow-Origin', self.config.get('app_client', '*')) | |
resp.set_header('Access-Control-Allow-Credentials', 'true') | |
if 'cookie' in res: | |
if res['cookie']['value'] == 'unset': | |
resp.unset_cookie(res['cookie']['name']) | |
else: | |
resp.set_cookie(res['cookie']['name'], res['cookie']['value']) | |
if 'body' in res: | |
resp.body = json.dumps(res['body']) | |
except Exception as e: | |
self.exception(e) | |
def handle(self, data): | |
return {'body': data} | |
@staticmethod | |
def exception(e): | |
raise e | |
def validate(self, data): | |
pass | |
@staticmethod | |
def bad_request(title='Bad request', description='Sorry, we can not handle your request!'): | |
raise falcon.HTTPBadRequest(title=title, description=description) | |
@staticmethod | |
def server_error(title='Server error', description='Sorry, our server has a problem!'): | |
raise falcon.HTTPInternalServerError(title=title, description=description) | |
@staticmethod | |
def unauthorized(title='Bad request', description='Sorry, you are not authorized!'): | |
raise falcon.HTTPUnauthorized(title=title, description=description) | |
class BasicBlueprint: | |
def __init__(self, config, name, template_folder, static_folder): | |
self.config = config | |
self.name = name | |
self.bp = Blueprint(name, __name__, | |
template_folder=template_folder, static_folder=static_folder) | |
def create_app(self, template): | |
bp = self.bp | |
handler = self.handle_request | |
@bp.route('/') | |
def index(): | |
try: | |
context = handler(request) | |
return render_template(template, context=context) | |
except TemplateNotFound: | |
abort(404) | |
except: | |
abort(500) | |
return bp | |
def handle_request(self, req): | |
return {} | |
def exception(): | |
msg = traceback.format_exc() # get info from sys.exc_info() | |
return msg | |
def log(msg, handler=None): | |
def handle(): | |
if handler is None: | |
print(msg()) | |
else: | |
handler(msg()) | |
return handle | |
default_log_exception = log(exception) | |
def log_wrap(func, except_res=True, log_exception=default_log_exception): | |
def _new_func(*args, **kargs): | |
try: | |
return func(*args, **kargs) | |
except: | |
log_exception() | |
return except_res | |
return _new_func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment