#serve-static #static-file #tide #web #macro

macro tide-serve-dir-macro

Macros to serve a directory of static files in tide

4 releases

0.2.0 Jun 4, 2024
0.1.2 Sep 16, 2022
0.1.1 Sep 16, 2022
0.1.0 Aug 19, 2022

#318 in GUI

Download history 4988/week @ 2024-07-28 4652/week @ 2024-08-04 5305/week @ 2024-08-11 4208/week @ 2024-08-18 5146/week @ 2024-08-25 4094/week @ 2024-09-01 4559/week @ 2024-09-08 4078/week @ 2024-09-15 4360/week @ 2024-09-22 4072/week @ 2024-09-29 4259/week @ 2024-10-06 4591/week @ 2024-10-13 5657/week @ 2024-10-20 5207/week @ 2024-10-27 5057/week @ 2024-11-03 4606/week @ 2024-11-10

20,736 downloads per month

MIT/Apache

12KB
164 lines

Tide Serve Directory Macros

This crate offers a few macros to serve a directory of static files in tide without using the serve_dir function itself. Instead it loops through the static directory at compile-time and generates direct serve_file routes for all the files it found, or directly embeds the files in the application binary using include_str.

This approach aims to be more safe than just serving an entire directory, since every single file that is going to be served is known at compile time. When embedding the files in the application binary, it can also drastically improve performance.

Usage

First add tide-serve-dir-macro to your dependencies:

[dependencies]
tide-serve-dir-macro = "0.2"

There are three macros available for serving a directory:

serve_dir!

Generates serve_file endpoints for all files found in the given directory. Files that are not given at compile time won't be served. If a file is missing at runtime, the app will panic, since the serve_file function is unwraped. Has the benefit that files can be modified at runtime. Furthermore it only loads the files when the given route is accessed.

let app = tide::new();
serve_dir!(app, "/path-prefix", "path/to/directory");

include_dir!

Generates endpoints that directly include the files content in the binary. This doesn't require the static files to be available at runtime and heavily improves response time when accessing the endpoint, but also comes at the cost of a bigger binary size for the application and more memory usage and is therefore not a good idea for large files. Furthermore, the entire app needs to be recompiled when the static files are changed. Since it is not a great idea to serve large files with this macro, you can provide a 4th optional parameter, that sets the maximum file size in bytes that a file should be directly included in the binary. If the file is larger, it's going to be served using serve_file;

let app = tide::new();
include_dir!(app, "/path-prefix", "path/to/directory");
// Only embed files smaller than 4KiB
include_dir!(app, "/path-prefix", "path/to/directory", 4096);

auto_serve_dir!

Helper macro that changes between the use of serve_dir! and include_dir! based on the build profile. Since the debug build should build as fast as possible and you probably want to change static files at runtime during development, this switches based on the build profile and uses include_dir only during a release build.

let app = tide::new();
auto_serve_dir!(app, "/path-prefix", "path/to/directory");
// Uses the same parameter set as include_dir!, although the maximum file size is ignored in the debug build
auto_serve_dir!(app, "/path-prefix", "path/to/directory", 4096);

Dependencies

~1.3–8MB
~73K SLoC