| 1 |
{-| |
| 2 |
hledger-web - a basic but robust web UI and JSON API server for hledger. |
| 3 |
|
| 4 |
SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
Copyright (c) 2007-2025 (each year in this range) Simon Michael <[email protected]> and contributors. |
| 6 |
|
| 7 |
This program is free software: you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation, either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 |
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
You should have received a copy of the GNU General Public License along with this program. |
| 16 |
If not, see <https://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
-} |
| 19 |
|
| 20 |
{-# LANGUAGE CPP #-} |
| 21 |
{-# LANGUAGE MultiWayIf #-} |
| 22 |
{-# LANGUAGE NamedFieldPuns #-} |
| 23 |
{-# LANGUAGE OverloadedStrings #-} |
| 24 |
|
| 25 |
module Hledger.Web.Main where |
| 26 |
|
| 27 |
import Control.Exception (bracket) |
| 28 |
#if MIN_VERSION_base(4,20,0) |
| 29 |
import Control.Exception.Backtrace (setBacktraceMechanismState, BacktraceMechanism(..)) |
| 30 |
#endif |
| 31 |
import Control.Monad (when) |
| 32 |
import Data.String (fromString) |
| 33 |
import Data.Text qualified as T |
| 34 |
import Network.Socket |
| 35 |
import Network.Wai (Application) |
| 36 |
import Network.Wai.Handler.Warp (runSettings, runSettingsSocket, defaultSettings, setHost, setPort) |
| 37 |
import Network.Wai.Handler.Launch (runHostPortFullUrl) |
| 38 |
import System.Directory (removeFile) |
| 39 |
import System.Environment ( getArgs, withArgs ) |
| 40 |
import System.IO (hFlush, stdout) |
| 41 |
import System.PosixCompat.Files (getFileStatus, isSocket) |
| 42 |
import Text.Printf (printf) |
| 43 |
import Yesod.Default.Config |
| 44 |
import Yesod.Default.Main (defaultDevelApp) |
| 45 |
|
| 46 |
import Hledger |
| 47 |
import Hledger.Cli hiding (progname,prognameandversion) |
| 48 |
import Hledger.Web.Application (makeApplication) |
| 49 |
import Hledger.Web.Settings (Extra(..), parseExtra) |
| 50 |
import Hledger.Web.Test (hledgerWebTest) |
| 51 |
import Hledger.Web.WebOptions |
| 52 |
|
| 53 |
-- Run in fast reloading mode for yesod devel. |
| 54 |
hledgerWebDev :: IO (Int, Application) |
| 55 |
hledgerWebDev = |
| 56 |
withJournal (cliopts_ defwebopts) (defaultDevelApp loader . makeApplication defwebopts) |
| 57 |
where |
| 58 |
loader = |
| 59 |
Yesod.Default.Config.loadConfig |
| 60 |
(configSettings Development) {csParseExtra = parseExtra} |
| 61 |
|
| 62 |
-- Run normally. |
| 63 |
hledgerWebMain :: IO () |
| 64 |
hledgerWebMain = handleExit $ withGhcDebug' $ do |
| 65 |
when (ghcDebugMode == GDPauseAtStart) $ ghcDebugPause' |
| 66 |
|
| 67 |
#if MIN_VERSION_base(4,20,0) |
| 68 |
-- Control ghc 9.10+'s stack traces. |
| 69 |
-- CostCentreBacktrace - collect cost-centre stack backtraces (only available when built with profiling) |
| 70 |
-- HasCallStackBacktrace - collect HasCallStack backtraces |
| 71 |
-- ExecutionBacktrace - collect backtraces from native execution stack unwinding |
| 72 |
-- IPEBacktrace - collect backtraces from Info Table Provenance Entries |
| 73 |
#ifdef DEBUG |
| 74 |
setBacktraceMechanismState HasCallStackBacktrace True |
| 75 |
#else |
| 76 |
setBacktraceMechanismState HasCallStackBacktrace False |
| 77 |
#endif |
| 78 |
#endif |
| 79 |
|
| 80 |
wopts@WebOpts{cliopts_=copts@CliOpts{debug_, rawopts_}} <- getHledgerWebOpts |
| 81 |
when (debug_ > 0) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show wopts) |
| 82 |
if |
| 83 |
| boolopt "help" rawopts_ -> runPager $ showModeUsage webmode ++ "\n" |
| 84 |
| boolopt "tldr" rawopts_ -> runTldrForPage "hledger-web" |
| 85 |
| boolopt "info" rawopts_ -> runInfoForTopic "hledger-web" Nothing |
| 86 |
| boolopt "man" rawopts_ -> runManForTopic "hledger-web" Nothing |
| 87 |
| boolopt "version" rawopts_ -> putStrLn prognameandversion |
| 88 |
-- boolopt "binary-filename" rawopts_ -> putStrLn (binaryfilename progname) |
| 89 |
| boolopt "test" rawopts_ -> do |
| 90 |
-- remove --test and --, leaving other args for hspec |
| 91 |
(`withArgs` hledgerWebTest) . filter (`notElem` ["--test","--"]) =<< getArgs |
| 92 |
| otherwise -> withJournal copts (web wopts) |
| 93 |
|
| 94 |
when (ghcDebugMode == GDPauseAtEnd) $ ghcDebugPause' |
| 95 |
|
| 96 |
-- | The hledger web command. |
| 97 |
web :: WebOpts -> Journal -> IO () |
| 98 |
web opts j = do |
| 99 |
let depthlessinitialq = filterQuery (not . queryIsDepth) . _rsQuery . reportspec_ $ cliopts_ opts |
| 100 |
j' = filterJournalTransactions depthlessinitialq j |
| 101 |
h = host_ opts |
| 102 |
p = port_ opts |
| 103 |
u = base_url_ opts |
| 104 |
staticRoot = T.pack <$> file_url_ opts -- XXX not used #2139 |
| 105 |
appconfig = AppConfig{appEnv = Development |
| 106 |
,appHost = fromString h |
| 107 |
,appPort = p |
| 108 |
,appRoot = T.pack u |
| 109 |
,appExtra = Extra "" Nothing staticRoot |
| 110 |
} |
| 111 |
app <- makeApplication opts j' appconfig |
| 112 |
|
| 113 |
-- show configuration |
| 114 |
let |
| 115 |
services |
| 116 |
| server_mode_ opts == ServeJson = "json API" |
| 117 |
| otherwise = "web UI and json API" |
| 118 |
prettyip ip |
| 119 |
| ip == "127.0.0.1" = ip ++ " (local access)" |
| 120 |
| ip == "0.0.0.0" = ip ++ " (all interfaces)" |
| 121 |
| otherwise = ip |
| 122 |
listenat = |
| 123 |
case socket_ opts of |
| 124 |
Just s -> printf "socket %s" s |
| 125 |
Nothing -> printf "IP address %s, port %d" (prettyip h) p |
| 126 |
printf "Serving %s at %s\nwith base url %s\n" (services::String) (listenat::String) u |
| 127 |
case file_url_ opts of |
| 128 |
Just fu -> printf "and static files base url %s\n" fu |
| 129 |
Nothing -> pure () |
| 130 |
|
| 131 |
-- start server and maybe browser |
| 132 |
if server_mode_ opts == ServeBrowse |
| 133 |
then do |
| 134 |
putStrLn "This server will exit after 2m with no browser windows open (or press ctrl-c)" |
| 135 |
putStrLn "Opening web browser..." |
| 136 |
hFlush stdout |
| 137 |
-- exits after 2m of inactivity (hardcoded) |
| 138 |
Network.Wai.Handler.Launch.runHostPortFullUrl h p u app |
| 139 |
|
| 140 |
else do |
| 141 |
putStrLn "Press ctrl-c to quit" |
| 142 |
hFlush stdout |
| 143 |
let warpsettings = setHost (fromString h) (setPort p defaultSettings) |
| 144 |
case socket_ opts of |
| 145 |
Just s -> do |
| 146 |
if isUnixDomainSocketAvailable then |
| 147 |
bracket |
| 148 |
(do |
| 149 |
sock <- socket AF_UNIX Stream 0 |
| 150 |
setSocketOption sock ReuseAddr 1 |
| 151 |
bind sock $ SockAddrUnix s |
| 152 |
listen sock maxListenQueue |
| 153 |
return sock |
| 154 |
) |
| 155 |
(\_ -> do |
| 156 |
sockstat <- getFileStatus s |
| 157 |
when (isSocket sockstat) $ removeFile s |
| 158 |
) |
| 159 |
(\sock -> Network.Wai.Handler.Warp.runSettingsSocket warpsettings sock app) |
| 160 |
else error' $ unlines |
| 161 |
["Unix domain sockets are not available on your operating system." |
| 162 |
,"Please try again without --socket." |
| 163 |
] |
| 164 |
|
| 165 |
Nothing -> Network.Wai.Handler.Warp.runSettings warpsettings app |
| 166 |
|