sara
sara

Reputation: 169

How to start application in Falcon with Gunicorn

I have simple application,

# app.py
import falcon

class ThingsResource:
def on_get(self, req, resq) :
    # something

class SomeResource:
    def on_get(self, req, resq) :
        # something

def create_things(self):    
    app = falcon.API(middleware=[(Middleware1())])
    things = ThingsResource()
    app.add_route('/things', things)

def create_some(self):
    app = falcon.API(middleware=[(Middleware2(exempt_routes=['/health']))])
    some = SomeResource()
    app.add_route('/some', some)

The problem is that, because i have different middleware for route's for one route is Middleware1 and for another is Middleware2

I need run app.py application, but this:

gunicorn -b 0.0.0.0:8000 app --reload

[Failed to find application object 'application' in 'app']

not work

I do not know how to run this application

I should run

gunicorn -b 0.0.0.0:8000 app:app --reload

But 'app' it's inside the method

Someone has an idea?

Upvotes: 1

Views: 1598

Answers (1)

Ahmed Dhanani
Ahmed Dhanani

Reputation: 861

What you can do is, return app instance from these function and assign it to a variable in your file (outside any function) like this:

def create_things():    
    app = falcon.API(middleware=[(Middleware1())])
    things = ThingsResource()
    app.add_route('/things', things)

    return app

def create_some():
    app = falcon.API(middleware=[(Middleware2(exempt_routes=['/health']))])
    some = SomeResource()
    app.add_route('/some', some)

    return app


app = create_some()

and run it using

gunicorn -b 0.0.0.0:8000 <file_name>:app --reload

Upvotes: 1

Related Questions