From b42abff16472a571d4b73e841166d559531e3403 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 15 Dec 2023 13:31:51 -1000 Subject: [PATCH] fix:web: make --base-url work again [#2127], [#2100] --- hledger-web/Hledger/Web/App.hs | 16 +++++++++++++++- hledger-web/Hledger/Web/Test.hs | 22 +++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/hledger-web/Hledger/Web/App.hs b/hledger-web/Hledger/Web/App.hs index 5c62b0a1d3a6..ef348836250f 100644 --- a/hledger-web/Hledger/Web/App.hs +++ b/hledger-web/Hledger/Web/App.hs @@ -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" diff --git a/hledger-web/Hledger/Web/Test.hs b/hledger-web/Hledger/Web/Test.hs index a868c02c03d0..36acfacd6d3e 100644 --- a/hledger-web/Hledger/Web/Test.hs +++ b/hledger-web/Hledger/Web/Test.hs @@ -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