Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a static webpage from falcon application?

I am new to python and hence falcon. I started developing a RESTful API and falcon so far is great for it. There is some other requirement to serve a static web page and I dont want to write an app or spawn a server for that.

Is it possible from the falcon app to serve the static web page?

like image 645
wayfare Avatar asked Jan 16 '16 00:01

wayfare


1 Answers

You can better control the routes to your static file like this:

import falcon
class StaticResource(object):
    def on_get(self, req, resp, filename):
        # do some sanity check on the filename
        resp.status = falcon.HTTP_200
        resp.content_type = 'appropriate/content-type'
        with open(filename, 'r') as f:
            resp.body = f.read()


app = falcon.API()
app.add_route('/static/{filename}', StaticResource())
like image 80
joarleymoraes Avatar answered Oct 25 '22 15:10

joarleymoraes