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 auto extensions #33

Merged
merged 9 commits into from
Jan 8, 2024
Merged

Conversation

pcantrell
Copy link
Contributor

@pcantrell pcantrell commented Jan 4, 2024

Detailed description

This adds support for implement file extensions, a la Apache MultiViews, which fixes #16. For example, a server started as follows:

Adsf::Server.new(..., auto_extensions: [".html", ".htm"])

…would look for first /foo/bar.html and then /foo/bar.htm for the request /foo/bar if and only if the file /foo/bar itself does not exist.

This implementation works in some quick manual tests, but before I add regression tests, docs, and CLI flags, I wanted to float the PR and make sure this is an agreeable direction for the maintainers. Let me know!

To do

(Include the to-do list for this PR to be finished here.)

  • Tests
  • Documentation
  • Command line flags

Related issues

adsf/lib/adsf/server.rb Outdated Show resolved Hide resolved
@pcantrell pcantrell marked this pull request as draft January 4, 2024 06:34
@denisdefreyne
Copy link
Owner

I like this! Plenty of (production) web servers allow this set-up, and it makes sense for adsf to allow it too.

@denisdefreyne denisdefreyne marked this pull request as ready for review January 4, 2024 12:17
@denisdefreyne denisdefreyne marked this pull request as draft January 4, 2024 12:17
@pcantrell pcantrell marked this pull request as ready for review January 4, 2024 17:14
@pcantrell
Copy link
Contributor Author

I think this is ready to go. (We’ll see what CI says!)

A few potentially controversial things that might require discussion:

  • Auto extensions, both the programmatic option and at the CLI, require the leading dot: ".html", not "html". Passing html will attempt to serve a request for /foo with /foohtml. This is more flexible, but may be a point of confusion. (I tried to make the docs unambiguous, but we know what that’s worth!)

  • I reordered the sections in the README. When I first visited the adsf repo, I was extremely confused about the initial discussion of the IndexFileFinder, and at first thought the Rack config code was an example of how to start the whole server programmatically. Moving the server section first would have been helpful to me!

    If this change requires separate discussion, I'm happy to move it to its own PR.

  • I made the bin script modify Ruby’s LOAD_PATH so it always uses the lib dir relative to the script, instead of using the locally installed gem. This allows local testing, and also potentially prevents version confusion.

    Again, if this change requires separate discussion, I'm happy to move it to its own PR.

@pcantrell pcantrell changed the title [WIP] Add auto extensions Add auto extensions Jan 4, 2024
@denisdefreyne
Copy link
Owner

Auto extensions, both the programmatic option and at the CLI, require the leading dot: ".html", not "html". Passing html will attempt to serve a request for /foo with /foohtml. This is more flexible, but may be a point of confusion. (I tried to make the docs unambiguous, but we know what that’s worth!)

I think I’d prefer not to have this. The chance of it being too confusing is too high.

Ruby does have . in the extensions in general (e.g. File.extname('blah.tmp') evaluates to ".tmp") but perhaps could be useful to require a . in auto_extensions, and fail if the dot is missing. WDYT?

I reordered the sections in the README. When I first visited the adsf repo, I was extremely confused about the initial discussion of the IndexFileFinder, and at first thought the Rack config code was an example of how to start the whole server programmatically. Moving the server section first would have been helpful to me!

If this change requires separate discussion, I'm happy to move it to its own PR.

This is a good change! I’ve extracted the changes (without the auto_extensions) into its own standalone PR (#34, already merged).

I made the bin script modify Ruby’s LOAD_PATH so it always uses the lib dir relative to the script, instead of using the locally installed gem. This allows local testing, and also potentially prevents version confusion.

A better approach (which I’ve used) is to use bundle exec, which will set the right load path for you. For example:

% bundle exec adsf/bin/adsf --help
Usage: adsf [options]
[snip]

% cd adsf

% bundle exec bin/adsf --help
Usage: adsf [options]
[snip]

As far as I know, the bin directory is not guaranteed to remain next to the lib directory. It typically is, but some Linux distributions choose to have it differently (Debian maybe? I’m not sure).

adsf/bin/adsf Outdated Show resolved Hide resolved
@denisdefreyne
Copy link
Owner

Looks good (and mergeable) apart from:

  • option description (should be a trivial change)

  • merge conflict (sorry! that was your separate README PR)

  • Rubocop fixes (I think bundle exec rubocop -a / bundle exec rubocop --autocorrect will fix it for you)

@denisdefreyne
Copy link
Owner

One more thing: can you add a test that checks the following?

If

  • Either /foo or /foo/ is requested
  • [".blorp"] is the list of auto extensions
  • ["index.html"] is the list of index filenames
  • Files at /foo/index.html and /foo.blorp exist

Then

  • /foo/index.html is served

I think that would be the correct behavior. It’s probably a little ambiguous, but it‘s good to have that tied down.

@pcantrell
Copy link
Contributor Author

I think I’d prefer not to have this [precedent dots]. The chance of it being too confusing is too high.

Having slept on it, I agree completely.

A better approach (which I’ve used) is to use bundle exec, which will set the right load path for you

TIL! I'll yank that commit.

I'll see if I can sneak in the revised commits and Rubocop appeasements before my afternoon appointment.

@pcantrell
Copy link
Contributor Author

pcantrell commented Jan 7, 2024

Leading dots removed, test added for index / auto extension interaction.

I had some hesitation about whether test_server.rb ought to have an assert_redirect helper instead of 3 lines of duplicated code. I'd call that a matter of taste, so I'll defer to your judgement if you think it's worth the code.

CI failed on Ruby 3.0, with what might be a test race condition about the port already being in use. ?!

@denisdefreyne
Copy link
Owner

CI can be a bit flaky on occasion (never managed to reproduce the issue) but it’ll all fine after re-running!

Thanks for your contribution :)

@denisdefreyne denisdefreyne merged commit 4e69c0d into denisdefreyne:main Jan 8, 2024
4 checks passed
@denisdefreyne
Copy link
Owner

This is now released in adsf 1.5.0 (first minor version in 6+ years!)

@pcantrell
Copy link
Contributor Author

Hooray!

Thanks for the library. It really does a nice job of scratch a very specific itch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for optional .html extension
2 participants