Aiohttp: serving static index page example?

Created on 27 Sep 2016  路  10Comments  路  Source: aio-libs/aiohttp

The 1.0.1 release has the following line in the list of changes:
Use text/html content type for displaying index pages by static file handler.
which sounds like there's a way to create standard handler for serving static index page (e.g. index.html).

However, I can't find any info or working example on how to actually do this (except with jinja). add_static doesn't seem to provide required functionality for that. Am I missing something or confusing what this change is about? Is there a simple one-liner to serve index.html file at the default '/' route (for development purposes)?

Thanks!

Most helpful comment

Hi Andrew,

Probably, I should've described more clearly. The aim is not to display a list of static files (which is achieved with show_index=True), but rather to render one particular file (e.g. index.html) as a main page directly. In other words, when I go to http://localhost:5555, I want the content of index.html be displayed instead of just an ftp-like list of files. Is it possible?

All 10 comments

Hi Andrew,

Probably, I should've described more clearly. The aim is not to display a list of static files (which is achieved with show_index=True), but rather to render one particular file (e.g. index.html) as a main page directly. In other words, when I go to http://localhost:5555, I want the content of index.html be displayed instead of just an ftp-like list of files. Is it possible?

You might use FileSender https://github.com/KeepSafe/aiohttp/blob/master/aiohttp/file_sender.py
aiohttp doesn't assume that index.html is default name for root page.
It's useful for serving static sites only, aiohttp doesn't best fit to this purpose.

I see, thanks! Probably, I'll just stick to jinja-based solution as it's very simple and all I need is a quick workaround for serving root page with aiohttp in test environment on my local machine (w/o nginx).

Thanks for doing great job anyway! I'm absolute fan of this framework!

:)

What happened with file_sender.py ?

Available as web.FileResponse

I think you could use a redirect

import aiohttp

async def root_handler(request):
    return aiohttp.web.HTTPFound('/index.html')

app = aiohttp.web.Application()
app.router.add_route('*', '/', root_handler)
app.router.add_static('/', './static')

if __name__ == '__main__':
    web.run_app(app)

I created this functionality with a middleware that returns FileResponse objects.

In my use case, I just wanted to use the server for static file hosting -- nothing else. You will need probably need to branch on request.path if you want this to live with other request handling.

from pathlib import Path

from aiohttp import web

static_dir_path = Path('dist')

@web.middleware
async def static_serve(request, handler):
    relative_file_path = Path(request.path).relative_to('/')  # remove root '/'
    file_path = static_dir_path / relative_file_path  # rebase into static dir
    if not file_path.exists():
        return web.HTTPNotFound()
    if file_path.is_dir():
        file_path /= 'index.html'
        if not file_path.exists():
            return web.HTTPNotFound()
    return web.FileResponse(file_path)

app = web.Application(middlewares=[static_serve])

The issue is resolved.
Please create a new one if needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhmiao picture zhmiao  路  3Comments

AtomsForPeace picture AtomsForPeace  路  5Comments

alxpy picture alxpy  路  5Comments

deckar01 picture deckar01  路  4Comments

jonringer picture jonringer  路  4Comments