Comments (12)
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.
@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.
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.
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.
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.
What do you think: d7fff12
from sanic.
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.
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.
@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.
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.
#104 has been merged. Does this issue still needs to be opened?
from sanic.
@simnalamburt good call, closing with #104
from sanic.
Related Issues (20)
- all workers are being blocked through the use of Unix sockets HOT 5
- Sanic hangs for around 20 seconds after every 3 minutes HOT 4
- Can't use optional CLI arguments HOT 4
- Error: aborted in sanic 23.12 HOT 2
- 使用注入的方式封装获取的json数据,结果是空的
- SERVER_EXCEPTION_REPORT signal handler does not get passed app positional, docs discrepancy HOT 2
- Multiplexer and Manager out of sync on number of expected arguments HOT 1
- INSPECTOR worker may handle HTTP request unexpectly when `Sanic.start_method="fork"` and `INSPECTOR=True`
- DeprecationWarning: Attribute s is deprecated and will be removed in Python 3.14 HOT 1
- CLI: Terminal becomes no-echo if not entering REPL mode or exiting without Ctrl+D HOT 3
- Trusted proxies HOT 2
- Sanic stops working after trying to open a WS connection HOT 1
- How can I use the Response streaming technique in a POST method? HOT 2
- Shutdown never finishes when background tasks are cancelled but need some time to finish
- EOFError in Server Manager HOT 2
- Start-up exception HOT 1
- openapi json exception
- The RESTful API has redundant path parameters HOT 1
- Response streaming produces [ERROR] Invalid response type None (need HTTPResponse)
- Unexpected behavior with bp.middleware() using classes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sanic.