Search code examples
python-2.7mod-wsgiwebfactionfalconframework

Python Falcon on Webfaction


I'm trying to get Falcon up and running on Webfaction. I'm not exactly a network guru, so I'm having a tough time wrapping my head around how these applications are served.

My Webfaction App is set up as mod_wsgi 4.5.3/Python 2.7

To my understanding, Falcon will run on any WSGI server. When I swet up my mod_wsgi server, is it automatically configured for something like Falcon to run on? Or do I still need to install something like Gunicorn?

When I set up my webfaction app, I received a directory structure like this:

app/htdocs/index.py

And inside the index.py file, I put the example found at Falcon Tutorial

import falcon

class ThingsResource(object):
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200
        resp.body = 'Hello world!'

# falcon.API instances are callable WSGI apps
wsgi_app = api = falcon.API()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
api.add_route('/things', things)

I understand there are also instructions for running WSGI, but that is where my confusion is at - is the webfaction server already running WSGI, or do I still need something like Gunicorn, and if so - what is the best way to configure? Do I need a cron to keep running Gunicorn?

Thanks!

Update:

I checked error logs and received a WSGI error about not having a variable named "application",

So I changed:

wsgi_app = api = falcon.API()

to:

application = falcon.API()

This cleared out the error, but now when I visit mydomain.com/things, I get an error 404 (Not found / Does not exist).

So, this brings me back to my original question of what the next steps are? It seems as if the url isn't being routed correctly, so it is most likely something to do with httpd.conf file or similar - again, this is my first go at getting something like this set up live.


Solution

  • Here is the answer (at least for the initial question, I'm willing to bet I'll mess up something else in the near future on the same project).

    Essentially, I was able to put the tutorial code in the index.py file that Webfaction generates when setting up an app & mounting on a domain. So, my tutorial code looks something like this:

    import falcon
    
    class ThingsResource(object):
            def on_get(self,req,resp):
                    resp.status = falcon.HTTP_200
                    resp.body = 'Hello World!'
    
    api = application  = falcon.API()
    
    things = ThingsResource()
    
    api.add_route('/things', things)
    

    Since I couldn't find much info for launching a Falcon app on Webfaction, I looked at how similar applications run on Webfaction (Flask in this example). That being said, I found a snippet on the Flask docu showing how to get set up on webfaction. I'm not sure if this means my entire application will work, but I do know that the Falcon tutorial code at least works. Essentially, I just had to edit the httpd.conf file per the instructions found here: Flask Webfaction

    WSGIPythonPath /home/yourusername/webapps/yourapp/htdocs/
    #If you do not specify the following directive the app *will* work but you will
    #see index.py in the path of all URLs
    WSGIScriptAlias / /home/yourusername/webapps/yourapp/htdocs/index.py
    
    <Directory /home/yourusername/webapps/yourapp/htdocs/>
       AddHandler wsgi-script .py
       RewriteEngine on
       RewriteBase /
       WSGIScriptReloading On
    </Directory>
    

    I hope this helps anybody with a similar issue.