Giter Club home page Giter Club logo

Comments (12)

sm5art avatar sm5art commented on May 2, 2024 3

im working on this, i want to make it similar to how express handles static files.

app.static('/static'(uri for static files), "./images"(directory with static files))

from sanic.

channelcat avatar channelcat commented on May 2, 2024 1

@sm5art I'm not a big fan of flask's way of defining static files. I find this syntax to be much simpler :). If it could support serving both directories and single files that would be awesome.

from sanic.

imbolc avatar imbolc commented on May 2, 2024 1

I don't think it's a good idea at all to handle static files with sanic in production. We can mention in docs to use middleware only in dev enviroment like:

if settigs.DEBUG:
    app.static.override(folder='./static', url_prefix='/')

So it will have no affect on production perfomance.
And we can add to docs some proxy servers configs into deploying section. E.g. for nginx:

    location / {
        try_files $uri @proxy;
    }
    location @proxy {
        proxy_pass http://127.0.0.1:8000;
    }

from sanic.

imbolc avatar imbolc commented on May 2, 2024 1

And it's common for python async frameworks to use nginx for static on production. Aiohttp docs (it has stream support): "The best way to handle static in production is setting up reverse proxy like NGINX or using CDN services.". Tornado docs: "In production, you probably want to serve static files from a more optimized static file server like nginx."

from sanic.

imbolc avatar imbolc commented on May 2, 2024

I think it good to take in account the case of serving files from root path. For example file ./static/robots.txt should be served not as /static/robotx.txt, but as /robots.txt.

And if we configure it like

app.static('/', './static')
app.route('*')(handler)

It should first look for file in static folder and if it isn't exist use handler to handle this url.
And if you need standard binding of /static/* urls to ./static folder you can just configure it like:

app.route(...)
app.route(...)
app.static('/static', './static')

So static handler should not throw 404 error for not existing files, but work like middleware and handle next route.

from sanic.

imbolc avatar imbolc commented on May 2, 2024

What do you think: d7fff12

from sanic.

channelcat avatar channelcat commented on May 2, 2024

Added in static file support based on the pull request, but I didn't see this comment and don't want to close this until we can support missing static files falling back to routes. I'm currently thinking the middleware might be a good solution, but adding middleware by default slows requests. Maybe we could make optional middleware to support it?

from sanic.

imbolc avatar imbolc commented on May 2, 2024

I also think that it's good idea to have both static routes and overriding middleware. Because they serve some different purposes. Static route is good to maintain independent apps based on blueprints, they can contain they own handlers, assets (and maybe templates in the future). And middleware can be used at the project level.

from sanic.

blakev avatar blakev commented on May 2, 2024

@imbolc I think it looks pretty good -- the sendfile method could easily be written in async by reading the file with a CHUNKSIZE and yielding the response.

Is somebody working on streaming responses?

from sanic.

imbolc avatar imbolc commented on May 2, 2024

Nginx has a lot of optimization like sendfile support. I don't think it impossible to achieve similar performance with sanic. I'm just not sure it's worth the effort.

from sanic.

simnalamburt avatar simnalamburt commented on May 2, 2024

#104 has been merged. Does this issue still needs to be opened?

from sanic.

seemethere avatar seemethere commented on May 2, 2024

@simnalamburt good call, closing with #104

from sanic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.