Skip to main content

Serving Static Files

Exposing static files​

Graphul provides an easy way to create endpoints using folders with static files or exposing just one file.

This could be a great utility in case you want to load a SPA or HTML in a route.

Graphul separates the concept of serving static files into two different functions, one for the Directories and the other for an individual file.

Besides we need to define the kind of FileConfig or FolderConfig for every case.

Directories​

To serve local files from specific directories and sub-directories, the function static_files provides support for it. As we said above you need to define the FolderConfig. Graphul provides two default configurations, the default option that just exposes the files into the folder and the spa option that exposes the route as a SPA.

Example​

use graphul::{Graphul, FolderConfig};

#[tokio::main]
async fn main() {
let mut app = Graphul::new();

// path = "/static", dir = public
app.static_files("/static", "public", FolderConfig::default());

// single page application
app.static_files("/", "app/build", FolderConfig::spa());

app.run("127.0.0.1:8000").await;
}

Individual file​

To serve a single file, the function static_file provides support for it. As we said above you need to define the FileConfig. Currently, we don't have many kinds of FileConfig, at the moment we support just one default configuration or the custom config.

Example​

use graphul::{Graphul, FileConfig};

#[tokio::main]
async fn main() {
let mut app = Graphul::new();

app.static_file("/about", "templates/about.html", FileConfig::default());

app.run("127.0.0.1:8000").await;
}