fix:web: make --base-url work again [#2127], [#2100]

This commit is contained in:
Simon Michael 2023-12-15 13:31:51 -10:00
parent 7bc077cd8e
commit 13ebf18d24
2 changed files with 28 additions and 10 deletions

View File

@ -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"

View File

@ -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