def main(): #Find a way to check if dev server. if get_default_version_hostname() == 'localhost:8080': debug(True) else: @error(500) def Error500(code): logging.error('There was an internal server error') message = 'Internal Server Error' return template('templates/simple.tpl', body=message) run_wsgi_app(bottle.default_app())
def initRoutes(app=None): if not app: app = bottle.default_app() @app.route('/') @decorators.private @bottle.view('index_default') def default(): return dict(content='hello, '+users.get_current_user().nickname()) @app.route('/logout') @app.route('/logout/') def logout(): user = users.get_current_user() if user: bottle.redirect(users.create_logout_url(bottle.request.url)) else: return 'sucessfully logged out'
class StripPathMiddleware(object): """ Ignore trailing slashes from url path http://bottlepy.org/docs/dev/recipes.html#ignore-trailing-slashes """ def __init__(self, app): self.app = app def __call__(self, e, h): e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') return self.app(e,h) # app, outbox and ses instance app = StripPathMiddleware(default_app()) outbox = Outbox(maxsize=OUTBOX_MAXSIZE) ses = AmazonSES(AMAZON_KEY, AMAZON_SECRET) # thread-safe counters queued = itertools.count() rejected = itertools.count() def error_msg(msg): """ Return error in json format """ data = {'status': 'error', 'message': msg} return json_encode(data)
def get_script_rel_path(filepath): script_dir = os.path.dirname(os.path.realpath(__file__)) return path.join(script_dir, filepath) # remove ending slash from requests @hook('before_request') def strip_path(): request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/') tpl_path = path.join(get_script_rel_path("templates")) bottle.TEMPLATE_PATH.insert(0, tpl_path) try: with open(get_script_rel_path("config.json")) as f: config = json.load(f) except: pass if __name__ == '__main__': parser = argparse.ArgumentParser(description='starts a lists server') parser.add_argument('--config', help='specifies the config file location (default: ./config.json)', default="./config.json") args = parser.parse_args() with open(args.config) as f: config = json.load(f) run(host='0.0.0.0', port=config['port']) app = default_app()
from optparse import OptionParser parser = OptionParser() parser.add_option('-s', '--server', dest='server', help='bottle.py WSGI server backend') parser.add_option('-p', '--port', dest='port', help='server port') parser.add_option('-b', '--buildsdir', dest='builds_directory', help='directory for builds') optargs = parser.parse_args() for key in SETTINGS.keys(): if optargs[0].__dict__.get(key): SETTINGS[key] = optargs[0].__dict__[key] os.chdir(os.path.dirname(os.path.abspath(__file__))) run(server=SETTINGS['server'], host='0.0.0.0', port=SETTINGS['port']) def setup(): '''setup method''' BaseTemplate.defaults['STUSKEYS'] = STUSKEYS setup() if __name__ == "__main__": main() else: # pylint: disable=invalid-name application = bottle.default_app() application.catchall = False # vim: set ts=8 sw=4 tw=0 :
# remove ending slash from requests @hook('before_request') def strip_path(): request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/') tpl_path = path.join(get_script_rel_path("templates")) bottle.TEMPLATE_PATH.insert(0, tpl_path) try: with open(get_script_rel_path("config.json")) as f: config = json.load(f) except: pass if __name__ == '__main__': parser = argparse.ArgumentParser(description='starts a lists server') parser.add_argument( '--config', help='specifies the config file location (default: ./config.json)', default="./config.json") args = parser.parse_args() with open(args.config) as f: config = json.load(f) run(host='0.0.0.0', port=config['port']) app = default_app()
#!/usr/bin/env python # -*- coding: utf-8 -*- import main from lib import bottle from lib.gaesessions import SessionMiddleware app = bottle.default_app() app = SessionMiddleware(app, cookie_key='a random and long string here for secret') main.gae_env()