Search code examples
pythonwebapp2static-content

Python webapp2 serve static content


Our team migrated a project from GAE to AWS. One component is a web application built on top of webapp2, a framework which is easy to integrate with GAE. We kept the webapp2 framework in AWS too, with some minor changes to make it work.

The web application works fine in the cloud, but I'm trying to find a way to run it on the local development machines too. When we were using the GAE environment it was an easy task because Google provides the App Engine Launcher, a tool which simulates the cloud environment very well.

In AWS we continued making some hacks in order to start the web application using App Engine Launcher, but now we want to discard it. So, I modified the python script and it starts successfully but I don't know how to serve the static content. The static files (CSS, JS) are added to the HTML templates like link rel="stylesheet" type="text/css" href="{{statics_bucket}}/statics/css/shared.css"/, where {{statics_bucket}} is an environment variable which points to a specific Amazon S3 bucket per environment. Of course, this doesn't work on localhost because nobody is serving static content on http://localhost:8080/statics/css/shared.css for example. The Google App Engine launcher had this feature and it did all the hard job.

Could somebody point out a way to achieve my goal?


Solution

  • I managed to achieve my goal with the following script:

    import os.path
    import application
    
    from paste import httpserver
    from paste.cascade import Cascade
    from paste.urlparser import StaticURLParser
    
    def main():
        web_client = application.application
        here = os.path.dirname(os.path.abspath(__file__))
        static_app = StaticURLParser(here)
    
        app = Cascade([web_client, static_app])
        httpserver.serve(app, host='localhost', port='8080')
    
    if __name__ == '__main__':
        main()
    

    The script starts the cloud application and also a component which serves the static files, both on the same port inside the same server.