Code Monkey home page Code Monkey logo

Comments (4)

ashleysommer avatar ashleysommer commented on June 14, 2024 1

I simply want to use Sanic behind gunicorn or Zappa or whatever WSGI server I want, nothing more. Can I do that with this?

What you need for that is a WSGI-to-Sanic adaptor, not a Sanic-to-WSGI adapter. Sanic-Dispatcher cannot do what you want in this case.

But good news! Sanic has recently been given its own gunicorn worker, which does do exactly what you are asking about.
See the deployment docs for how to use it: (See under "Running via Gunicorn")
https://github.com/channelcat/sanic/blob/master/docs/sanic/deploying.md#running-via-gunicorn

from sanic-dispatcher.

ashleysommer avatar ashleysommer commented on June 14, 2024

There are two trivial examples of usage of the Sanic-to-WSGI adapter already in the README.md file.
Any time you use the register_wsgi_application to add an app to the dispatcher you are telling the dispatcher to use the Sanic-to-WSGI adapter to run that invoke that application.

Copied from the README.md file:

What if the other App is a Flask App?

from flask import Flask, make_response

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)
flaskapp = Flask("MyFlaskApp")

# register the wsgi_app method from the flask app into the dispatcher
dispatcher.register_wsgi_application(flaskapp.wsgi_app, "/flaskprefix")

@flaskapp.route('/')
def index():
    return make_response("Hello World from Flask App")

Browsing to url /flaskprefix/ will invoke the Flask App, and call the / route which displays "Hello World from Flask App"

What if the other App is a Django App?

import my_django_app

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)
# register the django wsgi application into the dispatcher
dispatcher.register_wsgi_application(my_django_app.wsgi.application,
                                     "/djangoprefix")

Browsing to url /djangoprefix/ will invoke the Django App.

from sanic-dispatcher.

ashleysommer avatar ashleysommer commented on June 14, 2024

In response to your question about a passthrough; Yes!, that is possible.
Just register your application using the route_prefix '/' like so:

from flask import Flask, make_response

app = Sanic(__name__)

dispatcher = SanicDispatcherMiddlewareController(app)
flaskapp = Flask("MyFlaskApp")

# register the wsgi_app method from the flask app into the dispatcher
dispatcher.register_wsgi_application(flaskapp.wsgi_app, "/")

@flaskapp.route('/')
def index():
    return make_response("Hello World from Flask App")

In this example specifying the prefix '/' will cause all routes to be passed into the Flask application.

from sanic-dispatcher.

ashleysommer avatar ashleysommer commented on June 14, 2024

Closing due to no response in over 27 days.

from sanic-dispatcher.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.