Esempio n. 1
0
def create_graphql_app(root_object: PentContextfulObject,
                       schema: GraphQLSchema,
                       debug: bool = True) -> Sanic:
    """ Creates a Sanic app and adds a graphql/graphiql endpoint """
    app = Sanic(__name__)
    app.debug = debug

    if debug:
        # hack from https://github.com/channelcat/sanic/issues/168
        # for some reason pylint is confused by this import
        #E0611 no name in module
        #pylint: disable=E0611
        from aoiklivereload import LiveReloader
        reloader = LiveReloader()
        reloader.start_watcher_thread()

    # this is totally gross and I need a better story for managing context lifetime
    # versus loader lifetime. The context houses the in-memory shards in in memory
    # case currently so you cannot create a new one with every request. However
    # you do need a new loader every request because it is affined with its
    # asyncio event loop
    def root_factory() -> Any:
        root_object.context.loader = PentLoader(root_object.context)
        return root_object

    app.add_route(
        GraphQLView.as_view(schema=schema,
                            graphiql=True,
                            root_factory=root_factory,
                            context=root_object.context), '/graphql')
    return app
Esempio n. 2
0
def main():
    """
    Main function.

    :return:
        None.
    """
    try:
        # Get the `src` directory's absolute path
        src_path = os.path.dirname(
            # `aoiklivereload` directory's absolute path
            os.path.dirname(
                # `demo` directory's absolute path
                os.path.dirname(
                    # This file's absolute path
                    os.path.abspath(__file__))))

        # If the `src` directory path is not in `sys.path`
        if src_path not in sys.path:
            # Add to `sys.path`.
            #
            # This aims to save user setting PYTHONPATH when running this demo.
            #
            sys.path.append(src_path)

        # Import reloader class
        from aoiklivereload import LiveReloader

        # Create reloader
        reloader = LiveReloader()

        # Start watcher thread
        reloader.start_watcher_thread()

        # Server host
        server_host = '0.0.0.0'

        # Server port
        server_port = 8000

        # Get message
        msg = '# ----- Run server -----\nHost: {}\nPort: {}'.format(
            server_host, server_port)

        # Print message
        print(msg)

        # Create request handler
        @bottle.get('/')
        def hello_handler():  # pylint: disable=unused-variable
            """
            Request handler.

            :return:
                Response body.
            """
            # Return response body
            return 'hello'

        # Run server
        bottle.run(host=server_host, port=server_port)

    # If have `KeyboardInterrupt`
    except KeyboardInterrupt:
        # Not treat as error
        pass
Esempio n. 3
0
from aoiklivereload import LiveReloader

import asyncio
import asyncio_redis
import os

name = "SanicScrum"
version = "0.1a"

current_admin_passwd = "e7ce7267a56b42d4be4c"

app_root = os.path.dirname(__file__)
static_root = os.path.join(app_root, 'static')
templates_root = app_root
reloader = LiveReloader()
reloader.start_watcher_thread()

app = Sanic(__name__)
app_loop = asyncio.get_event_loop()

app.static('/static', static_root)
app.static('/templates', templates_root)
app.config.AUTH_LOGIN_URL = '/'

jinja = SanicJinja2(app)


class Redis:
    """
	A simple wrapper class that allows you to share a connection
	pool across your application.
Esempio n. 4
0
def main():
    """
    Main function.

    :return:
        None.
    """
    try:
        # Get the `src` directory's absolute path
        src_path = os.path.dirname(
            # `aoiklivereload` directory's absolute path
            os.path.dirname(
                # `demo` directory's absolute path
                os.path.dirname(
                    # This file's absolute path
                    os.path.abspath(__file__))))

        # If the `src` directory path is not in `sys.path`
        if src_path not in sys.path:
            # Add to `sys.path`.
            #
            # This aims to save user setting PYTHONPATH when running this demo.
            #
            sys.path.append(src_path)

        # Import reloader class
        from aoiklivereload import LiveReloader

        # Create reloader
        reloader = LiveReloader(
            # Reload mode.
            #
            # In windows, have to use `spawn_exit` reload mode and force the
            # current process to exit immediately, otherwise will get the
            # error:
            # ```
            # OSError: [WinError 10048] Only one usage of each socket address
            # (protocol/network address/port) is normally permitted
            # ```
            #
            # Notice in `spawn_exit` reload mode, the user will not be able
            # to kill the new process using Ctrl-c.
            #
            reload_mode=('spawn_exit' if sys.platform == 'win32' else 'exec'),
            force_exit=True,
        )

        # Start watcher thread
        reloader.start_watcher_thread()

        # Server host
        server_host = '0.0.0.0'

        # Server port
        server_port = 8000

        # Get message
        msg = '# ----- Run server -----\nHost: {}\nPort: {}'.format(
            server_host, server_port)

        # Print message
        print(msg)

        # Create request handler
        class HelloHandler(tornado.web.RequestHandler):
            """
            Request handler class.
            """
            def get(self):
                """
                Request handler.

                :return:
                    None.
                """
                # Write response body
                self.write('hello')

        # List of tuples that maps URL pattern to handler
        handler_tuples = [
            ('/', HelloHandler),
        ]

        # Create Tornado app
        tornado_app = tornado.web.Application(
            handler_tuples,
            # Disable Tornado's reloader
            debug=False,
        )

        # Start listening
        tornado_app.listen(server_port, address=server_host)

        # Get event loop
        io_loop = tornado.ioloop.IOLoop.current()

        # Run event loop
        io_loop.start()

    # If have `KeyboardInterrupt`
    except KeyboardInterrupt:
        # Not treat as error
        pass
Esempio n. 5
0
def live_reload():
    reloader = LiveReloader()
    reloader.start_watcher_thread()
Esempio n. 6
0
from sanic import Sanic
from sanic_cors import CORS
from aoiklivereload import LiveReloader

from crud import crud_bp, db, ShanghaiPersonInfo, LOGO
from config import base_config

# How is Support hot reload in Sanic?
# Just do it !
reloader = LiveReloader()
reloader.start_watcher_thread()

app = Sanic(__name__)

# but due to not support http `options` method in sanic core (https://github.com/channelcat/sanic/issues/251).
# So have to use third package extension for Sanic-Cors. Thank you @ashleysommer!

CORS(app,
     automatic_options=True)  # resolve pre-flight request problem (https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request)

app.config.LOGO = LOGO.format(
    ', 顾鲍尔')


@app.middleware('response')
async def custom_banner(request, response):
    response.headers["content-type"] = "application/json"


app.blueprint(crud_bp)
app.config.from_object(base_config)