diff --git a/aore/miscutils/bottlecl.py b/aore/miscutils/bottlecl.py new file mode 100644 index 0000000..210d8b5 --- /dev/null +++ b/aore/miscutils/bottlecl.py @@ -0,0 +1,24 @@ +# -*- 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, host, port): + self._app.run(host=host, port=port) diff --git a/aore/phias.py b/aore/phias.py index 0e0d80c..6b2bd77 100644 --- a/aore/phias.py +++ b/aore/phias.py @@ -2,44 +2,48 @@ import json import logging -from bottle import Bottle, response - -from aore.search.fiasfactory import FiasFactory - -logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) -app = Bottle() -fias_factory = FiasFactory() +from bottle import response +from miscutils.bottlecl import BottleCL -@app.route(r'/expand/') -def expand(aoid): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') +class App(BottleCL): + def __init__(self, config): + super(App, self).__init__() + logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) - return json.dumps(fias_factory.expand(aoid)) + self._config = config + # self._factory = FiasFactory() + def init_routes(self): + self.add_route(r'/expand/', self.__expand) + self.add_route(r'/normalize/', self.__normalize) + self.add_route(r'/find/', self.__find) + self.add_route(r'/find//', self.__find) + self.add_error(404, self.basic_error_handler) + self.add_error(500, self.basic_error_handler) -@app.route(r'/normalize/') -def normalize(aoid): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + def __expand(self, aoid): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - return json.dumps(fias_factory.normalize(aoid)) + return json.dumps(self._factory.expand(aoid)) + def __normalize(self, aoid): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') -@app.route(r'/find/') -@app.route(r'/find//') -def find(text, strong=False): - strong = (strong == "strong") - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + return json.dumps(self._factory.normalize(aoid)) - return json.dumps(fias_factory.find(text, strong)) + def __find(self, text, strong=False): + strong = (strong == "strong") + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') + return json.dumps(self._factory.find(text, strong)) -@app.error(404) -def error404(error): - response.content_type = 'application/json' - response.set_header('Access-Control-Allow-Origin', '*') + @staticmethod + def basic_error_handler(error): + response.content_type = 'application/json' + response.set_header('Access-Control-Allow-Origin', '*') - return json.dumps(dict(error="Page not found")) + return json.dumps(dict(error=error.status)) diff --git a/passenger_wsgi.py b/passenger_wsgi.py index 1f5a062..c0845cb 100644 --- a/passenger_wsgi.py +++ b/passenger_wsgi.py @@ -1,25 +1,26 @@ # -*- coding: utf-8 -*- from aore import phias, config +from bottle import run # Config section -config.sphinx_conf.listen = "192.168.0.37:9312" -config.sphinx_conf.var_dir = "C:\\Sphinx" - -config.db_conf.database = "pyfias" -config.db_conf.host = "192.168.0.37" -config.db_conf.port = 5432 -config.db_conf.user = "postgres" -config.db_conf.password = "intercon" - -config.unrar_config.path = "C:\\Program Files (x86)\\WinRAR\\unrar.exe" -config.folders.temp = "E:\\!TEMP" - -config.basic.logging = True +# config.sphinx_conf.listen = "192.168.0.37:9312" +# config.sphinx_conf.var_dir = "C:\\Sphinx" +# +# config.db_conf.database = "pyfias" +# config.db_conf.host = "192.168.0.37" +# config.db_conf.port = 5432 +# config.db_conf.user = "postgres" +# config.db_conf.password = "intercon" +# +# config.unrar_config.path = "C:\\Program Files (x86)\\WinRAR\\unrar.exe" +# config.folders.temp = "E:\\!TEMP" +# +# config.basic.logging = True # Define main app -application = phias.app +application = phias.App('test-config-fname') # Run bottle WSGI server if no external if __name__ == '__main__': - application.run(host='0.0.0.0', port=8087, debug=True) + application.start('0.0.0.0', 8087)