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

How to serve static files in a directory #425

Closed
bernhardreiter opened this issue Dec 1, 2016 · 9 comments
Closed

How to serve static files in a directory #425

bernhardreiter opened this issue Dec 1, 2016 · 9 comments
Labels

Comments

@bernhardreiter
Copy link
Contributor

bernhardreiter commented Dec 1, 2016

It would be nice if there was an example how to serve static files from a directory from within hug, so that other hug mechanisms, like authentication work. This can be needed to load a single page web application. (For example I am using hug as a prototyp serving backend in https://github.com/Intevation/intelmq-fody where it would be useful to serve the static files.)

In #43 (comment) the need for this can be seen. (There not solved by hug, but by pecan using static..)

@spock
Copy link
Contributor

spock commented Dec 1, 2016

There is an example of serving an image file using hug (path to served image is wrong and fixed in #426). For a static HTML page one would only need to set proper output format: output=hug.output_format.html.

Does this solve the issue? If not, then what would you change/improve?

@bernhardreiter
Copy link
Contributor Author

@spock thanks for your consideration!

The example is about one single file, I would like to serve all files from a directory without
the need of specifying each file separately.

@bernhardreiter
Copy link
Contributor Author

bernhardreiter commented Dec 7, 2016

It already works as the example in my pull-request #429 shows.
I'd say it misses a bit of documentation.

a) the function decorated with hug.static() seems to be called before hug.startup() functions.
Is it only called once?

b) how are the filetypes determined?

@spock
Copy link
Contributor

spock commented Dec 8, 2016

regarding b), I'd guess its output_format.file

@ghost
Copy link

ghost commented Jan 30, 2017

Not having any luck with this using a PNG file. Is there another recommended way to do this?

@hug.get('/static/{static_file}', output=hug.output_format.file)
def static(static_file):
    if '..' in static_file:
        raise
    return open('static/{}'.format(static_file))

Thank you!

@bernhardreiter
Copy link
Contributor Author

@teran-mckinney if you are trying to serve all files from a directory, then consider trying my example code at #429 . Should be something like (untested):

@hug.static('/static')def my_static_dirs():
    return('/home/www/path-to-static-dir',)

@ghost
Copy link

ghost commented Feb 1, 2017

@m-ueno
Copy link

m-ueno commented Feb 6, 2017

Another solution here.
How about using werkzeug.wsgi.DispatcherMiddleware ?

https://gist.github.com/m-ueno/0ec3922c30c37424f13bcaecc98ab4db

@timothycrosley
Copy link
Collaborator

Looks like this question has been answered well, closing and will remember to expand the documentation, thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants