Add bottle class proto (and simple class method routing)

This commit is contained in:
jar3b 2016-03-12 18:24:02 +03:00
parent 3fe80ecef8
commit b0914d58f5
3 changed files with 73 additions and 44 deletions

View File

@ -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)

View File

@ -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/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>')
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/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__expand)
self.add_route(r'/normalize/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>', self.__normalize)
self.add_route(r'/find/<text>', self.__find)
self.add_route(r'/find/<text>/<strong>', self.__find)
self.add_error(404, self.basic_error_handler)
self.add_error(500, self.basic_error_handler)
@app.route(r'/normalize/<aoid:re:[\w]{8}(-[\w]{4}){3}-[\w]{12}>')
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/<text>')
@app.route(r'/find/<text>/<strong>')
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))

View File

@ -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)