Genie.jl icon indicating copy to clipboard operation
Genie.jl copied to clipboard

Add a feature for declaring a custom static paths ("public/" alternatives)

Open rssdev10 opened this issue 11 months ago • 2 comments

Use case. I have a REST service and want to use some Genie components. But I want to have my own project structure and keep in some convenient place for maintenance some static files that should be publicly available.

This function can be implemented in two ways: by implementing a method static_path( url_path, local_directory) or by means of allowing wildcard segments where I can specify a dynamic part of the url_path.

The workaround is ugly now, but it works:

function static_path(path::String, dir::String)
    normalized_path = norm_uri_path(path * "/")
    normalized_dir = normpath(dir)
    @debug string("Registering static path: ", normalized_path, " for the local dir: ", normalized_dir)

    for (root, dirs, files) in walkdir(normalized_dir)
        relative_path = root[length(normalized_dir)+1:end] * "/"

        for fn in files
            Genie.route(norm_uri_path(normalized_path * relative_path * fn), method=Genie.GET) do
                serve_static_file(fn, root=root)
            end
        end
    end
end

rssdev10 avatar Aug 01 '23 23:08 rssdev10

@rssdev10 I don't fully understand the use case so maybe you can provide an example of what you're trying to serve. I can think of a few approaches but without understanding exactly what you want to achieve, I don't know what's best.

But before we dive into that, it's worth noting that the approach you propose would not be recommended for many files, as you're registering a route for each static file.

Now for the options:

1/ in the upcoming Genie 6 (currently in the v6 branch) I've added support for star subroutes(?! lacking a better name) - so you could register a route like

# Genie 6 code
route("/components/*") do params 
  serve_static_file(joinpath(your_components_folder, params["_"]))
end

The route part corresponding to the * is added to params["_"]. (Maybe a better key than _ would be good). Note to myself to test and document the feature. This can also be backported to v5, it's just a few lines of code.

2/ As is, Genie already first checks if the path of a request is a static file. So if you put them inside the public folder, that should already work. If you don't want to put them into the public folder (why not?), we could add support for multiple "public" folders. So we could add multiple such folder and Genie could check in all of them (which would still be much more efficient that registering a route for each file).

3/ You could add a router hook - this would cause Genie to automatically invoke the function you define before running the route matching code. In your hook you could process the request and return the static file response if it's a match (this might also work better in v6 due to recent changes).

What do you think? What makes more sense for your use case? I can take a closer look.

essenciary avatar Aug 03 '23 13:08 essenciary

Thanks, the case with route("/components/*") do params ... end looks good. The only point I want to add here - the value of params["_"] must be sanitized by default (at least to remove a relative path). See an example https://guides.rubyonrails.org/security.html#file-uploads. Regarding the name, may be it would be good to copy RoR approach...

The case with the custom location of the public path happened when we used Genie by components. In this case, we don't have a typical set of directories in the project. And our static files are in different locations. The case with multiple static public paths can also be useful if we have some different groups of files.

Other case with a custom static path - use the webpack or something like that to implement the frontend.

As for the backport to Genie 5, I can check it out next week.

rssdev10 avatar Aug 04 '23 02:08 rssdev10