Esempio n. 1
0
def run_rest_server(manager, debug, num_processes, num_threads):
    """Runs the REST server."""
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

    host = manager.config['server']['rest_host']
    port = manager.config['server']['rest_port']

    install(SaveEnvironmentPlugin(manager))
    install(CheckJsonPlugin())
    install(oauth2_provider.check_oauth())
    install(CookieAuthenticationPlugin())
    install(UserVerifiedPlugin())
    install(PublicUserPlugin())
    install(ErrorAdapter())

    # Replace default JSON plugin with one that handles datetime objects
    # Note: ErrorAdapter must come before JSONPlugin to catch serialization errors
    uninstall(JSONPlugin())
    install(JSONPlugin(json_dumps=DatetimeEncoder().encode))

    # JsonApiPlugin must come after JSONPlugin, to inspect and modify response
    # dicts before they are serialized into JSON
    install(JsonApiPlugin())

    for code in range(100, 600):
        default_app().error(code)(error_handler)

    root_app = Bottle()
    root_app.mount('/rest', default_app())

    # Look for templates in codalab-worksheets/views
    bottle.TEMPLATE_PATH = [
        os.path.join(
            os.path.dirname(
                os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            'views')
    ]

    # Increase the request body size limit to 8 MiB
    bottle.BaseRequest.MEMFILE_MAX = 8 * 1024 * 1024

    # We use gunicorn to create a server with multiple processes, since in
    # Python a single process uses at most 1 CPU due to the Global Interpreter
    # Lock.
    sys.argv = sys.argv[:1]  # Small hack to work around a Gunicorn arg parsing
    # bug. None of the arguments to cl should go to
    # Gunicorn.
    run(
        app=root_app,
        host=host,
        port=port,
        debug=debug,
        server='gunicorn',
        workers=num_processes,
        worker_class='gthread',
        threads=num_threads,
        worker_tmp_dir='/tmp',  # don't use globally set tempdir
        timeout=5 * 60,
    )
 def install_plugins(self):
     # install json api plugins
     json_kw = {'indent': 2, 'sort_keys': True}
     self.install(AuthorizationPlugin())
     self.install(JsonpPlugin(**json_kw))
     self.install(JSONPlugin(**json_kw))
     self.install(JsonApiResponsePlugin())
     self.install(JsonApiRequestPlugin())
     self.install(JsonApiRequestTypePlugin())
Esempio n. 3
0
def create_rest_app(manager=CodaLabManager()):
    """Creates and returns a rest app."""
    install(SaveEnvironmentPlugin(manager))
    install(CheckJsonPlugin())
    install(oauth2_provider.check_oauth())
    install(CookieAuthenticationPlugin())
    install(UserVerifiedPlugin())
    install(PublicUserPlugin())
    install(ErrorAdapter())

    # Replace default JSON plugin with one that handles datetime objects
    # Note: ErrorAdapter must come before JSONPlugin to catch serialization errors
    uninstall(JSONPlugin())
    install(JSONPlugin(json_dumps=DatetimeEncoder().encode))

    # JsonApiPlugin must come after JSONPlugin, to inspect and modify response
    # dicts before they are serialized into JSON
    install(JsonApiPlugin())

    for code in range(100, 600):
        default_app().error(code)(error_handler)

    root_app = Bottle()
    root_app.mount('/rest', default_app())

    # Look for templates in codalab-worksheets/views
    bottle.TEMPLATE_PATH = [
        os.path.join(
            os.path.dirname(
                os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
            'views')
    ]

    # Increase the request body size limit to 8 MiB
    bottle.BaseRequest.MEMFILE_MAX = 8 * 1024 * 1024
    return root_app
Esempio n. 4
0
    def __init__(self, conf=None):
        try:
            self.conf = conf or Config('gefoo', 'ubiety', require_load=True)
            self.pingers = self.create_pingers()

            # Setup restapi
            self.restapi = Bottle()
            self.restapi.get(
                path="/pinger/<pinger_name>",
                callback=self.get_pinger,
                name="get_pinger"
            )
            self.restapi.get(
                path="/pinger",
                callback=self.get_pingers,
                name="get_pingers"
            )
            self.restapi.install(
                JSONPlugin(
                    json_dumps=lambda s: jsonify(s, cls=PingerJSONEncoder)
                )
            )
        except OSError as exc:
            raise OSError(exc)
Esempio n. 5
0
def create_application():
    application = Bottle()
    application.install(
        JSONPlugin(
            json_dumps=lambda object: json.dumps(object, cls=JSONEncoder)))
    return application
Esempio n. 6
0
    def apply(self, callback, route):
        def wrap(*args, **kwargs):
            with connect(self.cfg):
                return callback(*args, **kwargs)

        return wrap


def json_serial(obj):
    if isinstance(obj, (datetime, date)):
        return obj.isoformat()
    raise TypeError("Type %s not serializable" % type(obj))


to_json = lambda x: json.dumps(x, default=json_serial)
install(JSONPlugin(json_dumps=to_json))


@route('/')
def index():
    return static_file('index.html', root='static')


@route('/static/<path:path>')
def callback(path):
    return static_file(path, root='static')


@route('/search_table/<prefix>')
def search_table(prefix):
    max_len = 10
Esempio n. 7
0
    def __init__(self, redis_url=None):
        self._init_logging()

        if getattr(sys, 'frozen', False):
            self.static_root = os.path.join(sys._MEIPASS, 'webrecorder',
                                            'static/')
        else:
            self.static_root = resource_filename('webrecorder', 'static/')

        bottle_app = Bottle()
        self.bottle_app = bottle_app

        # JSON encoding for datetime objects
        self.bottle_app.install(
            JSONPlugin(
                json_dumps=lambda s: json.dumps(s, cls=CustomJSONEncoder)))

        config = load_wr_config()

        # Init Redis
        if not redis_url:
            redis_url = os.environ['REDIS_BASE_URL']

        self.redis = redis.StrictRedis.from_url(redis_url)
        self.browser_redis = redis.StrictRedis.from_url(
            os.environ['REDIS_BROWSER_URL'], decode_responses=True)
        self.session_redis = redis.StrictRedis.from_url(
            os.environ['REDIS_SESSION_URL'])

        # Init Jinja
        jinja_env = self.init_jinja_env(config)

        # Init Content Loader/Rewriter
        content_app = ContentController(app=bottle_app,
                                        jinja_env=jinja_env,
                                        config=config,
                                        redis=self.redis)

        # Init Browser Mgr
        self.browser_mgr = BrowserManager(config, self.browser_redis,
                                          content_app)

        # Init Cork
        self.cork = WebRecCork.create_cork(self.redis, config)

        # Init Manager
        manager = RedisDataManager(self.redis, self.cork, content_app,
                                   self.browser_redis, self.browser_mgr,
                                   config)

        # Init Sesion temp_prefix
        Session.temp_prefix = config['temp_prefix']

        # Init Core app controllers
        for controller_type in self.ALL_CONTROLLERS:
            x = controller_type(app=bottle_app,
                                jinja_env=jinja_env,
                                manager=manager,
                                config=config)

        # Set Error Handler
        bottle_app.default_error_handler = self.make_err_handler(
            bottle_app.default_error_handler)

        final_app = RedisSessionMiddleware(bottle_app, self.cork,
                                           self.session_redis, config)

        super(AppController, self).__init__(final_app, jinja_env, manager,
                                            config)
Esempio n. 8
0
from reqlog.support.config_support import get_config
from reqlog.support.jwt_plugin import JWTProviderPlugin
from reqlog.support.log_tools import init_logger
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from ujson import dumps as json_dumps


logger = init_logger()

Base = declarative_base()
db_plugin = None

application = bottle.Bottle(autojson=False)
application.logger = logger
application.install(JSONPlugin(json_dumps=lambda s: json_dumps(s, sort_keys=True, indent=4)))


def init_base_and_engine(logger, config):
    db_connection_string = config.get("db_connection", "connection_string")
    produce_sql_echo = config.getboolean("db_connection", "produce_echo")

    logger.info("db_connection_string = '{}'".format(db_connection_string))
    logger.info("produce_sql_echo     = '{}'".format(produce_sql_echo))

    engine = sqlalchemy.create_engine(db_connection_string, echo=produce_sql_echo)

    return engine


class ImprovedSqlAlchemyPlugin(bottle_sqlalchemy.SQLAlchemyPlugin):
Esempio n. 9
0
@get('/flights/<flight_id:int>')
def flight(flight_id):
    result = db.flight.find_one({'_id': flight_id})
    if not result:
        raise HTTPError(404, "the flight does not exist")
    return result


class JSONEncoder(json.JSONEncoder):
    def default(self, o):
        try:
            json.JSONEncoder.default(self, o)
        except TypeError:
            return str(o)


if __name__ == "__main__":
    if not MongoClient(host="mongo")['flightlens'].flight.count():
        load_excel('FlightLegs-2017-07-24.xlsx')
    # install plugins
    install(JSONPlugin(json_dumps=JSONEncoder().encode))
    install(ErrorsRestPlugin())
    run(port=4422,
        host="0.0.0.0",
        server="gunicorn",
        workers=2 * cpu_count() + 1,
        worker_class="egg:meinheld#gunicorn_worker",
        quiet=True,
        debug=False,
        timeout=600)
Esempio n. 10
0
def send_email(address, subject, body):
    message = MIMEText(body.encode('ISO-8859-1'))
    message['Subject'] = subject
    message['From'] = '%s <%s>' % (config.site_name, config.site_email)
    message['To'] = address

    smtp = smtplib.SMTP('localhost')
    smtp.sendmail(config.site_email, [address], message.as_string())


def gen_pw_reset_payload(user):
    # bundle old pw into hmac code to make this reset valid only
    # for the current pw
    return {'email': user.email, 'password': user.password}


def slug(s):
    return slugify(s, to_lower=True, max_length=100)


class JsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return int(obj.strftime('%s'))
        if isinstance(obj, datetime.date):
            return int(obj.strftime('%s'))
        return json.JSONEncoder.default(self, obj)


jsonplugin = JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=JsonEncoder))
Esempio n. 11
0
# coding:utf-8
__author__ = "chenghao"

from bottle import Bottle, request, redirect, response, JSONPlugin, jinja2_view as view
from json import dumps
import handler, util
from dal import admin_dal

admin_app = Bottle()
admin_app.install(JSONPlugin(json_dumps=lambda s: dumps(s, cls=util.ComplexEncoder)))


######################### 后台管理 #########################
@admin_app.get("/", apply=[view("./admin/index")])
def index():
	return {}



######################### 后台登录 #########################
@admin_app.get("/login", apply=[view("./admin/login")])
def login():
	"""
	显示登录页面, 如果已经登录就重定向到后台管理主页
	:return:
	"""
	rid = handler.get_user_id()
	if rid:
		redirect("/admin")
	else:
		return {}
Esempio n. 12
0
            'page_title': rev.page_title,
            'page_id': rev.page_id,
            'rev_id': rev.rev_id,
            'rev_parent_id': rev.rev_parent_id,
            'user_text': rev.user_text,
            'user_id': rev.user_id,
            'time': str(rev.time),
            'time_delta': time_delta.total_seconds(),
            'length': rev.length,
            'sha1': rev.sha1,
            'comment': rev.comment,
            'tags': rev.tags,
            'sections': rev_stats
        })
    return {'title': title, 'revisions': stats, 'total_revs': len(revs)}


@route('/<title>')
def get_sec_stats(title):
    return section_stats(title)


if __name__ == '__main__':
    better_dumps = partial(bottle.json_dumps,
                           indent=2,
                           sort_keys=True,
                           default=repr)
    bottle.debug(True)
    bottle.install(JSONPlugin(better_dumps))
    run(port=8080)
Esempio n. 13
0
                            JsonFileNotFoundError, get_json,
                            get_latest_version_commit, get_repo_info,
                            validate_manifest, ProjectValidationError)
from ext_api.s3.ext_images import (upload_images, validate_image_url,
                                   get_user_images, delete_images,
                                   ImageUrlValidationError, FileTooLargeError)
from ext_api.helpers.aws import get_url_prefix
from ext_api.helpers.http_client import http
from ext_api.db import check_migration_consistency
from ext_api.helpers.response import ErrorResponse
from ext_api.helpers.cors import allow_options_requests, add_options_route
from ext_api.config import max_images_per_uer, commit, deployed_on, github_api_token, github_api_user
from ext_api.helpers.logging_utils import bottle_request_logger

app = Bottle(autojson=False)
app.install(JSONPlugin(json_dumps=dumps))
app.install(allow_options_requests)
app.install(bottle_auth_plugin)
app.install(bottle_request_logger)
add_options_route(app)

logger = logging.getLogger(__name__)

response.content_type = 'application/json'

allowed_sort_by = ['GithubStars', 'CreatedAt']
allowed_sort_order = ['-1', '1']

# pylint: disable=no-member,unsubscriptable-object

Esempio n. 14
0
    # Also might have used error class
    if (product_reservation == None):
        return HTTPError(status=404, body={'error': "Product not found."})

    cc = CreditCard(request.json['cc_number'], request.json['expiration'],
                    request.json['security_code'])
    charge = payments_service.charge(cc, product_reservation.product.price,
                                     'USD')

    if (charge.success):
        return product_dispense_response(product_reservation)

    # if the charge didn't go through, release the inventory back into stock
    products_db.release_product_reservation(product_reservation.product.id)
    return HTTPError(status=401, body={'error': charge.error})


@app.error(400)
@app.error(401)
@app.error(404)
def error_handler(error):
    response.status = error.status
    return error.body


install(
    JSONPlugin(
        json_dumps=lambda body: json.dumps(body, default=json_util.default)))
run(app, host='localhost', port=8080, debug=True)
Esempio n. 15
0
        del action_log["number"]
        del action_log["type"]
        replay_document[key] = action_log

    replay_document["action_count"] = action_count
    replay_document["last_modified"] = last_modified

    return replay_document


class ServerJsonEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return str(obj.strftime("%Y-%m-%dT%H:%M:%SZ"))
        if isinstance(obj, Enum):
            return obj.name
        if isinstance(obj, ObjectId):
            return str(obj)
        return JSONEncoder.default(self, obj)


def server_document_to_string(document):
    return dumps(document, indent=4, cls=ServerJsonEncoder, sort_keys=False)


app.install(
    JSONPlugin(
        json_dumps=lambda document: server_document_to_string(document)))

# To run the server: uwsgi --http :8080 --wsgi-file server.py --callable app --master --py-autoreload=1
Esempio n. 16
0
#coding:utf-8
__author__ = "chenghao"

from bottle import Bottle, request, redirect, response, JSONPlugin
from json import dumps
import util

blog_app = Bottle()
blog_app.install(
    JSONPlugin(json_dumps=lambda s: dumps(s, cls=util.ComplexEncoder)))