From afd61dae19b76990220820c3719141e04de1a696 Mon Sep 17 00:00:00 2001 From: jar3b Date: Wed, 21 Sep 2016 17:19:45 +0300 Subject: [PATCH] Add "OPTIONS" handling for more restful responses --- aore/miscutils/bottlecl.py | 24 ------------- aore/phias.py | 71 +++++++++++++++++++++----------------- requirements.txt | 1 + wsgi.py | 9 ++--- 4 files changed, 45 insertions(+), 60 deletions(-) delete mode 100644 aore/miscutils/bottlecl.py diff --git a/aore/miscutils/bottlecl.py b/aore/miscutils/bottlecl.py deleted file mode 100644 index 371338a..0000000 --- a/aore/miscutils/bottlecl.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- - -from bottle import Bottle - - -class BottleCL(object): - def __init__(self): - self._app = Bottle() - self.init_routes() - - def init_routes(self): - pass - - def add_route(self, route_path, handler): - self._app.route(route_path, callback=handler) - - def add_error(self, error_code, handler): - if not self._app.error_handler: - self._app.error_handler = {error_code: handler} - else: - self._app.error_handler[error_code] = handler - - def start(self, **kwargs): - self._app.run(**kwargs) diff --git a/aore/phias.py b/aore/phias.py index 1f7897a..b6c5a26 100644 --- a/aore/phias.py +++ b/aore/phias.py @@ -5,53 +5,60 @@ import logging from bottle import response, request from aore.search.fiasfactory import FiasFactory -from .miscutils.bottlecl import BottleCL +from borest import app, Route, Error -class App(BottleCL): +class App(object): + _factory = None + def __init__(self, log_filename): - super(App, self).__init__() logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG, filename=log_filename) - self._factory = FiasFactory() + App._factory = FiasFactory() - def get_app(self): - return self._app + @Route(r'/expand/') + class Expand(object): + def get(self, aoid): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - def init_routes(self): - self.add_route(r'/expand/', self.__expand) - self.add_route(r'/normalize/', self.__normalize) - self.add_route(r'/gettext/', self.__gettext) - self.add_route(r'/find/', self.__find) - self.add_error(404, self.basic_error_handler) - self.add_error(500, self.basic_error_handler) + return json.dumps(App._factory.expand(aoid)) - def __expand(self, aoid): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + @Route(r'/normalize/') + class Normalize(object): + def get(self, aoid): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - return json.dumps(self._factory.expand(aoid)) + return json.dumps(App._factory.normalize(aoid)) - def __normalize(self, aoid): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + @Route(r'/find/') + class Find(object): + def get(self, text): + strong = 'strong' in request.query and request.query.strong == '1' + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - return json.dumps(self._factory.normalize(aoid)) + return json.dumps(App._factory.find(text, strong)) - def __find(self, text): - strong = 'strong' in request.query and request.query.strong == '1' - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + @Route(r'/gettext/') + class GetText(object): + def get(self, aoid): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - return json.dumps(self._factory.find(text, strong)) - - def __gettext(self, aoid): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') - - return json.dumps(self._factory.gettext(aoid)) + return json.dumps(App._factory.gettext(aoid)) @staticmethod + @Error(404) + def basic_error_handler(error): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') + + return json.dumps(dict(error=error.status)) + + @staticmethod + @Error(500) def basic_error_handler(error): response.content_type = 'application/json' response.set_header('Access-Control-Allow-Origin', '*') diff --git a/requirements.txt b/requirements.txt index 7591e28..05506dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ rarfile requests>=2.8.1 soap2py>=1.16 sphinx-py3-api>=2.2.9 +bottle-oop-rest>=0.0.3 \ No newline at end of file diff --git a/wsgi.py b/wsgi.py index da2543f..7568689 100644 --- a/wsgi.py +++ b/wsgi.py @@ -8,11 +8,12 @@ try: except ImportError: assert "No config" -# Define main app -phias_app = phias.App(config.BasicConfig.logfile) +# Create main app +phias.App(config.BasicConfig.logfile) + # Define wsgi app -application = phias_app.get_app() +application = phias.app # Run bottle WSGI server if no external if __name__ == '__main__': - phias_app.start(host='0.0.0.0', port=8087, debug=True) + application.run(host='0.0.0.0', port=8087, debug=True)