danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exclusion list for static files middleware

fastbike opened this issue · comments

On of our projects has all of the ui designed by an external web agency. They deliver us a file structure like this

\www
    \html
    \js
    \img
    \css
    \etc

We point our static files middleware to the root directory so that the js/css/img/etc files can be served directly. However we wish to exclude the html directory as it contains page fragments that need to be rendered by the mustache engine (effectively it is our ui source code)

Currently we handle this with a separate middleware which blocks the static files middleware from serving specified directories under the "\www" root, such as the html directory.

I'm thinking this must be a common situation and would like to propose a solution, some ideas I'm considering are:

  • add an optional param to the current middleware constructor, or
  • inject an additional filter object into the current middleware constructor, or
  • add a filter property to the current middleware

I'll look into this in the coming week, but in the meantime happy to hear any other thoughts.

There is a specific config colder for templates. You need just configure that folder (check the server side example) and put the html there.

We've set

      // default view file extension
      Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
      // view path
      Config[TMVCConfigKey.ViewPath] := '\www\html';

However browsing to localhost:8060/www/html/index.html returns us the raw content of the index.html file rather than blocking the request.

We need to block any direct access to these html files. It should only be available via a registered controller path action, such as '/view/' in which case it will get rendered via the mustache engine and the appropriate substitutions take place.

Are there other settings that need to be configured ?

The ViewPath must not be addressable by the staticfile Middleware.

The ViewPath must not be addressable by the staticfile Middleware.

And that is the problem. The external design agency delivers a zip file with the html template file sitting alongside the other UI files (css/js/etc). The deployment is simple if we just copy all of their files to the server, without having to extract and move the html template files.
Currently we have another custom piece of middleware which rejects requests to the "html" folder. But it would be nice to have it optionally included in the static middleware.
My preference would be to define a filter interface and optionally pass a concrete instance in if this behaviour was desired.

Ah Yes! I got the point. I think we could just inject an anonmethod with a signature similar to the following:

procedure (const PathInfo: String; var Allow: Boolean)
begin
  // do your logic
end;

If the anonmethod is nil the the behavior remains as it is now.

Thanks. I'll play around with something tomorrow and come back with a proposal / example.

David, I had some time to wok on this, check this solution please dmvcframework\samples\middleware_staticfiles\

Very useful, thanks Daniele for the enhancement, and the additional flexibility in the last iteration of the solution.

I'm happy to accept that as the fix.