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

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

Open
rssdev10 opened this issue Aug 1, 2023 · 2 comments

Comments

@rssdev10
Copy link

rssdev10 commented Aug 1, 2023

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
@essenciary
Copy link
Member

essenciary commented Aug 3, 2023

@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 essenciary changed the title add a function for declating a custom static path add a function for declaring a custom static path Aug 3, 2023
@rssdev10
Copy link
Author

rssdev10 commented Aug 4, 2023

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.

@essenciary essenciary changed the title add a function for declaring a custom static path Add a feature for declaring a custom static paths ("public/" alternatives) + improve * paths Sep 7, 2023
@essenciary essenciary self-assigned this Sep 7, 2023
@essenciary essenciary changed the title Add a feature for declaring a custom static paths ("public/" alternatives) + improve * paths Add a feature for declaring a custom static paths ("public/" alternatives) Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants