Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to static routes #468

Closed
playpauseandstop opened this issue Aug 13, 2015 · 11 comments
Closed

Improvements to static routes #468

playpauseandstop opened this issue Aug 13, 2015 · 11 comments
Labels

Comments

@playpauseandstop
Copy link
Contributor

I got into two problems with static routes recently,

  1. There are no default ability to enable HEAD requests to static routes
  2. Sometimes we need to serve lone static files as routes instead of serving static files inside of static dir

Workaround to 1st problem,

app.router.add_static('/static/', static_dir, name='static')
route = web.StaticRoute(None, '/static/', static_dir)
route._method = 'HEAD'
app.router.register_route(route)

Not very elegant solution, IMO


Workaround to 2nd problem,

def view_factory(url, path):
    async def static_view(request):
        prefix = url.rsplit('/', 1)[0] or '/'
        route = web.StaticRoute(None, prefix, static_dir)

        request.match_info['filename'] = path
        return await route.handle(request)
    return static_view

view = static_view('/', 'pages/index.html')
app.router.add_route('GET', '/', view_factory('/', 'pages/index.html'))
app.router.add_route('GET', 
                     '/favicon.ico', 
                     view_factory('/favicon.ico', 'favicon/favicon.ico'))

In total, I propose imrpove static routes by,

  • Adding ability to setup routing HEAD requests to the static files on adding static route
  • Adding shortcut or refactor Static Route to be able serve separate files
@asvetlov
Copy link
Member

Sounds good.
Are you volunteered for the task ?

@playpauseandstop
Copy link
Contributor Author

@asvetlov

Ok, will try to make a PR today or next week

@redixin
Copy link
Contributor

redixin commented Oct 16, 2015

IMO router shouldn't have anything related to serving files. We can have some handler for this:

    from aiohttp import handlers
    app.router.add_route('GET', '/media/', handlers.static('/var/www/media/', indexes=True))

As for HEAD we can allow specifying multiple methods:

    app.router.add_route({'GET', 'HEAD'}, 'media', handlers.static('/var/www/media/'))

And ok. We can still have an alias for setting static route, which will just call add_route with proper handler:

    app.router.add_static('/static/', static_dir, name='static')

and inside add_static:

     def add_static(self, *args, **kw):
          self.add_route({'GET', 'HEAD'}, handlers.static(*args, **kw))

@asvetlov
Copy link
Member

@redixin path param for .add_route should specify full url path, not prefix only. That makes path ugly for static file serving.

@rutsky
Copy link
Member

rutsky commented Oct 20, 2015

@redixin, you mentined indexes=True --- any plans for implementing directory listings for static directories?
I think simple directory listings is a good feature.

@redixin
Copy link
Contributor

redixin commented Oct 20, 2015

@rutsky I want to finish logging first(#572). After that I can try to look deep into routes/static/indexes.

@playpauseandstop
Copy link
Contributor Author

@redixin

Great suggestion on having handlers for serving static files. Totally agreed with it!

@binhnq94
Copy link

Plzz, illustrate where the docs for this solution. 💃

@eteamin
Copy link
Contributor

eteamin commented Jun 19, 2016

@redixin Thank you

@asvetlov
Copy link
Member

Thanks to @dkuznetsov and #901 now we have a FileSender class for sending local file via the wire.

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants