Exemple #1
0
def get_app(config):
    """
    Get a Bottle app instance with all the routes set-up.

    :return: The built bottle app.
    """
    get_session = database.init_db(config["database"], config["search_index"])

    app = bottle.default_app()
    app.install(DatabasePlugin(get_session))
    app.install(ConfigPlugin(config))
    app.config.setdefault("canister.log_level", logging.root.level)
    app.config.setdefault("canister.log_path", None)
    app.config.setdefault("canister.debug", False)
    app.install(canister.Canister())
    # Use DateAwareJSONEncoder to dump JSON strings
    # From http://stackoverflow.com/questions/21282040/bottle-framework-how-to-return-datetime-in-json-response#comment55718456_21282666.  pylint: disable=locally-disabled,line-too-long
    bottle.install(
        bottle.JSONPlugin(
            json_dumps=functools.partial(json.dumps, cls=DateAwareJSONEncoder)
        )
    )

    # API v1 routes
    app.route("/api/v1/", "GET", api_routes.index_v1)

    app.route("/api/v1/time_to_places", "GET",
              api_routes.time_to_places_v1)

    app.route("/api/v1/flats", "GET", api_routes.flats_v1)
    app.route("/api/v1/flats/status/:status", "GET",
              api_routes.flats_by_status_v1)

    app.route("/api/v1/flat/:flat_id", "GET", api_routes.flat_v1)
    app.route("/api/v1/flat/:flat_id/status", "POST",
              api_routes.update_flat_status_v1)
    app.route("/api/v1/flat/:flat_id/notes", "POST",
              api_routes.update_flat_notes_v1)
    app.route("/api/v1/flat/:flat_id/notation", "POST",
              api_routes.update_flat_notation_v1)

    app.route("/api/v1/search", "POST", api_routes.search_v1)

    # Index
    app.route("/", "GET", lambda: _serve_static_file("index.html"))

    # Static files
    app.route("/favicon.ico", "GET",
              lambda: _serve_static_file("favicon.ico"))
    app.route(
        "/assets/<filename:path>", "GET",
        lambda filename: _serve_static_file("/assets/{}".format(filename))
    )
    app.route(
        "/img/<filename:path>", "GET",
        lambda filename: _serve_static_file("/img/{}".format(filename))
    )

    return app
Exemple #2
0
def get_app(config):
    """
    Get a Bottle app instance with all the routes set-up.

    :return: The built bottle app.
    """
    get_session = database.init_db(config["database"], config["search_index"])

    app = bottle.Bottle()
    app.install(DatabasePlugin(get_session))
    app.install(ConfigPlugin(config))
    app.config.setdefault("canister.log_level", "DISABLED")
    app.config.setdefault("canister.log_path", False)
    app.config.setdefault("canister.debug", False)
    app.install(canister.Canister())
    # Use DateAwareJSONEncoder to dump JSON strings
    # From http://stackoverflow.com/questions/21282040/bottle-framework-how-to-return-datetime-in-json-response#comment55718456_21282666.  pylint: disable=locally-disabled,line-too-long
    app.install(
        bottle.JSONPlugin(json_dumps=functools.partial(
            json.dumps, cls=DateAwareJSONEncoder)))

    # Enable CORS
    @app.hook("after_request")
    def enable_cors():
        """
        Add CORS headers at each request.
        """
        # The str() call is required as we import unicode_literal and WSGI
        # headers list should have plain str type.
        bottle.response.headers[str("Access-Control-Allow-Origin")] = str("*")
        bottle.response.headers[str("Access-Control-Allow-Methods")] = str(
            "PUT, GET, POST, DELETE, OPTIONS, PATCH")
        bottle.response.headers[str("Access-Control-Allow-Headers")] = str(
            "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token")

    # API v1 routes
    app.route("/api/v1", ["GET", "OPTIONS"], api_routes.index_v1)

    app.route("/api/v1/time_to_places", ["GET", "OPTIONS"],
              api_routes.time_to_places_v1)

    app.route("/api/v1/flats", ["GET", "OPTIONS"], api_routes.flats_v1)
    app.route("/api/v1/flats/:flat_id", ["GET", "OPTIONS"], api_routes.flat_v1)
    app.route("/api/v1/flats/:flat_id", ["PATCH", "OPTIONS"],
              api_routes.update_flat_v1)

    app.route("/api/v1/ics/visits.ics", ["GET", "OPTIONS"],
              api_routes.ics_feed_v1)

    app.route("/api/v1/search", ["POST", "OPTIONS"], api_routes.search_v1)

    app.route("/api/v1/opendata", ["GET", "OPTIONS"],
              api_routes.opendata_index_v1)
    app.route(
        "/api/v1/opendata/postal_codes",
        ["GET", "OPTIONS"],
        api_routes.opendata_postal_codes_v1,
    )

    app.route("/api/v1/metadata", ["GET", "OPTIONS"], api_routes.metadata_v1)
    app.route("/api/v1/import", ["GET", "OPTIONS"], api_routes.import_v1)

    # Index
    app.route("/", "GET", lambda: _serve_static_file("index.html"))

    # Static files
    app.route("/favicon.ico", "GET", lambda: _serve_static_file("favicon.ico"))
    app.route(
        "/assets/<filename:path>",
        "GET",
        lambda filename: _serve_static_file("/assets/{}".format(filename)),
    )
    app.route(
        "/img/<filename:path>",
        "GET",
        lambda filename: _serve_static_file("/img/{}".format(filename)),
    )
    app.route(
        "/.well-known/<filename:path>",
        "GET",
        lambda filename: _serve_static_file("/.well-known/{}".format(filename)
                                            ),
    )
    app.route(
        "/data/img/<filename:path>",
        "GET",
        lambda filename: bottle.static_file(
            filename, root=os.path.join(config["data_directory"], "images")),
    )

    return app
Exemple #3
0
#!/usr/bin/env python3

# ab -n 1000 -c 10 http://localhost:8080/hello/world
# ab -n 2 -c 2 http://127.0.0.1:8080/hello/world

import sys
sys.path.insert(0, '..')
import canister
import time
import bottle
from canister import session

app = bottle.Bottle()
app.install(canister.Canister())


@app.get('/')
def index():
    return '''
        <pre>
            Session sid: %s
            Session user: %s
        </pre>
        <form target="/login">
        <a href="/login?username">My private area</a> (username: alice, password: my-secret)</a>
    ''' % (session.sid, session.user)


@app.get('/login')
def login(username, password):
    session.user = username
Exemple #4
0
from bottle import *
import json, requests, jwt, bottle, canister

app = bottle.default_app()
app.install(canister.Canister())  #logger

secret = "A)13(*&&qh2#@$!!!qq"  #encrypt those tokens


#get the main page
@app.get("/")
def welcome():
    filename = "index.html"
    return static_file(filename, root="static")


#cors handling
@hook('after_request')
def allow_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type'


#resources
@app.route("/api/menu-bars", method=(['GET', 'OPTIONS'])
           )  #define routes this way or the hook won't work
def getMenuBar():
    return ({
        "01": "/img/menubar1.png",
        "02": "/img/menubar2.png",
        "03": "/img/menubar3.png"