libcex  1.0.0
Modern C++11 embedded webserver library
Classes | Functions
filesystem.hpp File Reference

Filesystem middleware function. More...

#include <string>
#include <core.hpp>

Go to the source code of this file.

Classes

struct  cex::FilesystemOptions
 Contains all options for the filesystem middleware. More...
 

Functions

MiddlewareFunction cex::filesystem (std::string aPath)
 Creates a middleware that loads files from the filesystem with the given path as rootPath. More...
 
MiddlewareFunction cex::filesystem (std::shared_ptr< FilesystemOptions > opts=nullptr)
 Creates a middleware that loads files from the filesystem with the given options object. More...
 

Detailed Description

Filesystem middleware function.

Loads file contents from the local filesystem with the paths provided by the request URLs.

Example:

std::shared_ptr<cex::FilesystemOptions> opts(new cex::FilesystemOptions());
opts.get()->rootPath= "/my/root/path;
app.use("/docs", cex::filesystem(opts));

The Content-Type header is set accordingly for the mimetype of the queried file. The mimetype is determined by the file extension in the request-URL. libcex contains a list of known mimetypes to look up the exact Content-Type as well as binary/text information.

The defaultEncoding is added to the Content-Type if it was set and the determined mimetype is not a binary type.

If no mimetype could be found in the internal list, Content-Type falls back to text/plain with the defaultEncoding.

Function Documentation

◆ filesystem() [1/2]

MiddlewareFunction cex::filesystem ( std::string  aPath)

Creates a middleware that loads files from the filesystem with the given path as rootPath.

Parameters
pathThe path to use as rootPath

The path of request URLs will be appended as relative paths when accessing files .

◆ filesystem() [2/2]

MiddlewareFunction cex::filesystem ( std::shared_ptr< FilesystemOptions opts = nullptr)

Creates a middleware that loads files from the filesystem with the given options object.

Parameters
optsThe FilesystemOptions object containing rootPath and defaultEncoding

The path of request URLs will be appended as relative paths when accessing files .