Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusion list for static files middleware #548

Closed
fastbike opened this issue Apr 1, 2022 · 8 comments
Closed

Exclusion list for static files middleware #548

fastbike opened this issue Apr 1, 2022 · 8 comments
Assignees
Labels
accepted Issue has been accepted and inserted in a future milestone enhancement

Comments

@fastbike
Copy link
Contributor

fastbike commented Apr 1, 2022

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.

@danieleteti
Copy link
Owner

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

@fastbike
Copy link
Contributor Author

fastbike commented Apr 3, 2022

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 ?

@danieleteti
Copy link
Owner

The ViewPath must not be addressable by the staticfile Middleware.

@fastbike
Copy link
Contributor Author

fastbike commented Apr 4, 2022

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.

@danieleteti
Copy link
Owner

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.

@fastbike
Copy link
Contributor Author

fastbike commented Apr 4, 2022

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

danieleteti added a commit that referenced this issue Apr 4, 2022
@danieleteti
Copy link
Owner

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

danieleteti added a commit that referenced this issue Apr 4, 2022
danieleteti added a commit that referenced this issue Apr 4, 2022
@fastbike
Copy link
Contributor Author

fastbike commented Apr 4, 2022

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.

@danieleteti danieleteti added enhancement accepted Issue has been accepted and inserted in a future milestone labels Apr 4, 2022
@danieleteti danieleteti added this to the 3.2.2-nitrogen milestone Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Issue has been accepted and inserted in a future milestone enhancement
Projects
None yet
Development

No branches or pull requests

2 participants