Feature: serve_file
This seems like generic enough functionality it could make sense to put in HTTP.jl. I don't have an api figured out, but just as a placeholder/starting point
# ?serve_file
# serve_file(path; mimetype="infer", extension="infer", ...) :: HTTP.Response
# serve_file(stream::HTTP.Stream, ...) :: Nothing
using HTTP
function handle_file(req::HTTP.Request)
return HTTP.serve_file("file.txt")
end
Some pointers to other implementations
PkgServer - https://github.com/JuliaPackaging/PkgServer.jl/blob/065235d39b4f66018dbc51ce3cb6861429f095e2/src/resource.jl#L394
Genie - https://github.com/GenieFramework/Genie.jl/blob/899ea32d3a7a95aab3bfe508fc9d6821bde2a1e0/src/Router.jl#L992
Flask - https://github.com/pallets/flask/blob/632f85b65354ad573bb33c54a0a9dd3ffcef371f/src/flask/helpers.py#L447
Yeah, I've thought this would be a useful "default" handler to have in place; I'd be in favor
See https://github.com/JuliaWeb/HTTP.jl/issues/653
See also #658 , serving a file can be a bigger project than it sounds, and it might benefit from being separated from HTTP.jl, or perhaps as part of a router package. A good file server should set the right cache & mime headers, support ranged requests, HEAD requests, and more.
There's a solid implementation that go includes in their stdlib here. If someone wanted to take a stab at translating to Julia, I think it would be fairly straightforward. I'm not going to mark this as 1.0 as it's an easy add-on post 1.0. If someone is interested, just comment here and I'll assign it; otherwise I'll get to this eventually in the future.