# -*- coding: utf-8 -*- import json import logging from bottle import response from miscutils.bottlecl import BottleCL class App(BottleCL): def __init__(self, config): super(App, self).__init__() logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) 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) def __expand(self, aoid): response.content_type = 'application/json' response.set_header('Access-Control-Allow-Origin', '*') return json.dumps(self._factory.expand(aoid)) def __normalize(self, aoid): response.content_type = 'application/json' response.set_header('Access-Control-Allow-Origin', '*') return json.dumps(self._factory.normalize(aoid)) 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)) @staticmethod def basic_error_handler(error): response.content_type = 'application/json' response.set_header('Access-Control-Allow-Origin', '*') return json.dumps(dict(error=error.status))