Skip to content

Commit

Permalink
fix:web: make --base-url work again [#2127], [#2100]
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Dec 15, 2023
1 parent ac3a067 commit b42abff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
16 changes: 15 additions & 1 deletion hledger-web/Hledger/Web/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,21 @@ type Form a = Html -> MForm Handler (FormResult a, Widget)
-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod App where
approot = guessApprootOr (ApprootMaster $ appRoot . settings)

-- Configure the app root, AKA base url, which is prepended to relative hyperlinks.
-- Broadly, we'd like this:
-- 1. when --base-url has been specified, use that;
-- 2. otherwise, guess it from request headers, which helps us respond from the
-- same hostname/IP address when hledger-web is accessible at multiple IPs;
-- 3. otherwise, leave it empty (relative links stay relative).
-- But it's hard to see how to achieve this.
-- For now we do (I believe) 1 or 3, with 2 unfortunately not supported.
-- Issues include: #2099, #2100, #2127
approot =
-- ApprootRelative
-- ApprootMaster $ appRoot . settings
-- guessApprootOr (ApprootMaster $ appRoot . settings)
ApprootMaster $ \(App{settings=AppConfig{appRoot=r}, appOpts=WebOpts{base_url_=bu}}) -> if null bu then r else T.pack bu

makeSessionBackend _ = do
hledgerdata <- getXdgDirectory XdgCache "hledger"
Expand Down
22 changes: 13 additions & 9 deletions hledger-web/Hledger/Web/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,23 @@ hledgerWebTest = do
bodyContains "id=\"transaction-2-1\""
bodyContains "id=\"transaction-2-2\""

runTests "hledger-web with --base-url"
[("base-url","https://base")] nulljournal $ do

yit "hyperlinks respect --base-url" $ do
get JournalR
statusIs 200
bodyContains "href=\"https://base"
bodyContains "src=\"https://base"
-- #2127
-- XXX I'm pretty sure this test lies, ie does not match production behaviour.
-- (test with curl -s http://localhost:5000/journal | rg '(href)="[\w/].*?"' -o )
-- App root setup is a maze of twisty passages, all alike.
-- runTests "hledger-web with --base-url"
-- [("base-url","https://base")] nulljournal $ do
-- yit "hyperlinks respect --base-url" $ do
-- get JournalR
-- statusIs 200
-- bodyContains "href=\"https://base"
-- bodyContains "src=\"https://base"

-- #2139
-- XXX Not passing.
-- Static root setup is a maze of twisty passages, all different.
-- runTests "hledger-web with --base-url, --file-url"
-- [("base-url","https://base"), ("file-url","https://files")] nulljournal $ do

-- yit "static file hyperlinks respect --file-url, others respect --base-url" $ do
-- get JournalR
-- statusIs 200
Expand Down

0 comments on commit b42abff

Please sign in to comment.