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

feat: basepath routing for DevServer vhost emulation #59

Merged
merged 10 commits into from
Jan 8, 2024
9 changes: 8 additions & 1 deletion lib/mix/tasks/tableau.server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ defmodule Mix.Tasks.Tableau.Server do
Application.put_env(:tableau, :server, true)
Code.put_compiler_option(:ignore_module_conflict, true)

Logger.debug("server started on http://localhost:4999")
Logger.debug("server started on http://localhost:4999#{basepath()}")

Mix.Task.run("app.start", ["--preload-modules"])

Mix.Tasks.Run.run(["--no-halt"])
end

defp basepath do
case Application.get_env(:tableau, :config)[:base_path] do
"" -> ""
path -> Path.join("/", path)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case Application.get_env(:tableau, :config)[:base_path] do
"" -> ""
path -> Path.join("/", path)
end
Path.join("/", Application.get_env(:tableau, :config)[:base_path])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

end
end
1 change: 1 addition & 0 deletions lib/tableau.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Tableau do

* `:include_dir` - string - Directory that is just copied to the output directory. Defaults to `extra`.
* `:timezone` - string - Timezone to use when parsing date times. Defaults to `Etc/UTC`.
* `:base_path - string - base path to use with Github Pages or web-servers that use a location prefix for Vhosts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `:base_path - string - base path to use with Github Pages or web-servers that use a location prefix for Vhosts
* `:base_path - string - Development server root . Defaults to `/`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

* `:url` - string (required) - The URL of your website.
* `:markdown` - keyword
* `:mdex` - keyword - Options to pass to `MDEx.to_html/2`
Expand Down
1 change: 1 addition & 0 deletions lib/tableau/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Tableau.Config do

defstruct [
:url,
base_path: "",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation for this. Its in the Tableau moduledoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

include_dir: "extra",
timezone: "Etc/UTC",
reload_log: false,
Expand Down
4 changes: 3 additions & 1 deletion lib/tableau/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Tableau.Router do

require Logger

@base_path Path.join("/", Application.compile_env(:tableau, :config)[:base_path] || "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you actually need the || "" since it defaults to ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing it defaults to nil, which causes Path.join/2 to raise an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I was thinking of the default coming from the struct. This is fine then, should also add to the other spot instead of that case expression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a closer look at this. Turns out there is a small difference in the arguments accepted by Application.get_env and Application.compile_env. I believe the forms I used are correct, and tested it with and without a base_path configuration.


@not_found ~g'''
<!DOCTYPE html><html lang="en"><head></head><body>Not Found</body></html>
'''html
Expand All @@ -14,7 +16,7 @@ defmodule Tableau.Router do
plug :rerender

plug Tableau.IndexHtml
plug Plug.Static, at: "/", from: "_site", cache_control_for_etags: "no-cache"
plug Plug.Static, at: @base_path, from: "_site", cache_control_for_etags: "no-cache"

plug :match
plug :dispatch
Expand Down