Stiffstream / restinio

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use
Other
1.13k stars 93 forks source link

display webpage of directory listing #216

Open timbo100 opened 2 months ago

timbo100 commented 2 months ago

Hi someone else on my team has been responsible for build our application that uses Restinio and it's all working well. However, I need to figure out how to be able to allow users to navigate and view the directory contents of on our system. The directory is .../public/logs/, which will contain numerous *.csv files.

Please help or offer direction on where to get help. Thanks

ps My next questions will be how to I get the name of a selected file from that webpage, or how can I influence how the HTML code is rendered for each file so that when selected the page navigates to a new route that will open the file or render a new page?

Thanks again.

eao197 commented 2 months ago

Hi!

RESTinio doesn't provide a functionality to return a list of files in a directory as a HTML page. You have to serve public/logs path by yourself (it may be simpler if you are using express-based router). When you get a HTTP GET request for public/logs you collect a list of files (by using std::filesystem::directory_iterator for example), then you make a HTML page and return it in the response body. Each name in that HTML file will be represented as a reference (something like <a href="https://yoursite/public/logs/some-file.csv">some-file.csv</a>). When a user clicks on that reference in a browser you will get a new HTTP GET request with public/logs/some-file.csv as the target. Then you can handle this request by reading the content of some-file.csv and sending this content back as the response (or you can use sendfile functionality).