From 1bcc091a44d14bb0f7bb1151e3948b2088ccb93e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 21 Nov 2016 07:38:58 -0800 Subject: [PATCH] web: add --host, rename --server to --serve (#429) This came up in the context of Docker, but it seems it wasn't possible for hledger-web to serve remote clients directly (without a proxy) because of 127.0.0.1 being hardcoded ? Now that can be changed with --host=IPADDR. The default base url also uses this address, rather than "localhost" being hardcoded. Also, the --server flag sounded too close in meaning to --host so I've renamed it to --serve. The old spelling is still accepted, at least through the next major release I suppose. --- hledger-web/Application.hs | 2 +- hledger-web/Hledger/Web/Main.hs | 7 +-- hledger-web/Hledger/Web/WebOptions.hs | 33 +++++++----- hledger-web/Settings.hs | 14 ++--- hledger-web/doc/hledger-web.1 | 35 +++++++----- hledger-web/doc/hledger-web.1.info | 37 ++++++++----- hledger-web/doc/hledger-web.1.m4.md | 28 ++++++---- hledger-web/doc/hledger-web.1.txt | 76 +++++++++++++++------------ 8 files changed, 138 insertions(+), 94 deletions(-) diff --git a/hledger-web/Application.hs b/hledger-web/Application.hs index 99b43b958..821aabdd9 100644 --- a/hledger-web/Application.hs +++ b/hledger-web/Application.hs @@ -59,7 +59,7 @@ makeApplication opts j conf = do return $ logWare app where logWare | development = logStdoutDev - | server_ opts = logStdout + | serve_ opts = logStdout | otherwise = id makeFoundation :: AppConfig DefaultEnv Extra -> WebOpts -> IO App diff --git a/hledger-web/Hledger/Web/Main.hs b/hledger-web/Hledger/Web/Main.hs index 28eb3600f..0d8daaa2e 100644 --- a/hledger-web/Hledger/Web/Main.hs +++ b/hledger-web/Hledger/Web/Main.hs @@ -71,7 +71,7 @@ web opts j = do d <- getCurrentDay let initq = queryFromOpts d $ reportopts_ $ cliopts_ opts j' = filterJournalTransactions initq j - h = "127.0.0.1" + h = host_ opts p = port_ opts u = base_url_ opts staticRoot = pack <$> file_url_ opts @@ -82,8 +82,9 @@ web opts j = do ,appExtra = Extra "" Nothing staticRoot } app <- makeApplication opts j' appconfig - _ <- printf "Starting web app on host %s port %d with base url %s\n" h p u - if server_ opts + -- XXX would like to allow a host name not just an IP address here + _ <- printf "Starting web app on IP address %s port %d with base url %s\n" h p u + if serve_ opts then do putStrLn "Press ctrl-c to quit" hFlush stdout diff --git a/hledger-web/Hledger/Web/WebOptions.hs b/hledger-web/Hledger/Web/WebOptions.hs index be71439d7..681d756a8 100644 --- a/hledger-web/Hledger/Web/WebOptions.hs +++ b/hledger-web/Hledger/Web/WebOptions.hs @@ -21,14 +21,12 @@ version = "" prognameandversion :: String prognameandversion = progname ++ " " ++ version :: String -defbaseurlexample :: String -defbaseurlexample = (reverse $ drop 4 $ reverse $ defbaseurl defport) ++ "PORT" - webflags :: [Flag [([Char], [Char])]] webflags = [ - flagNone ["server"] (setboolopt "server") ("log requests, and don't browse or auto-exit") - ,flagReq ["port"] (\s opts -> Right $ setopt "port" s opts) "PORT" ("set the tcp port (default: "++show defport++")") - ,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "BASEURL" ("set the base url (default: "++defbaseurlexample++")") + flagNone ["serve","server"] (setboolopt "serve") ("serve and log requests, don't browse or auto-exit") + ,flagReq ["host"] (\s opts -> Right $ setopt "host" s opts) "IPADDR" ("listen on this IP address (default: "++defhost++")") + ,flagReq ["port"] (\s opts -> Right $ setopt "port" s opts) "PORT" ("listen on this TCP port (default: "++show defport++")") + ,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "BASEURL" ("set the base url (default: http://IPADDR:PORT)") ,flagReq ["file-url"] (\s opts -> Right $ setopt "file-url" s opts) "FILEURL" ("set the static files url (default: BASEURL/static)") ] @@ -48,7 +46,8 @@ webmode = (mode "hledger-web" [("command","web")] -- hledger-web options, used in hledger-web and above data WebOpts = WebOpts { - server_ :: Bool + serve_ :: Bool + ,host_ :: String ,port_ :: Int ,base_url_ :: String ,file_url_ :: Maybe String @@ -62,17 +61,22 @@ defwebopts = WebOpts def def def + def -- instance Default WebOpts where def = defwebopts rawOptsToWebOpts :: RawOpts -> IO WebOpts rawOptsToWebOpts rawopts = checkWebOpts <$> do cliopts <- rawOptsToCliOpts rawopts - let p = fromMaybe defport $ maybeintopt "port" rawopts + let + h = fromMaybe defhost $ maybestringopt "host" rawopts + p = fromMaybe defport $ maybeintopt "port" rawopts + b = maybe (defbaseurl h p) stripTrailingSlash $ maybestringopt "base-url" rawopts return defwebopts { - port_ = p - ,server_ = boolopt "server" rawopts - ,base_url_ = maybe (defbaseurl p) stripTrailingSlash $ maybestringopt "base-url" rawopts + serve_ = boolopt "serve" rawopts + ,host_ = h + ,port_ = p + ,base_url_ = b ,file_url_ = stripTrailingSlash <$> maybestringopt "file-url" rawopts ,cliopts_ = cliopts } @@ -80,7 +84,12 @@ rawOptsToWebOpts rawopts = checkWebOpts <$> do stripTrailingSlash = reverse . dropWhile (=='/') . reverse -- yesod don't like it checkWebOpts :: WebOpts -> WebOpts -checkWebOpts = id +checkWebOpts wopts = + either optserror (const wopts) $ do + let h = host_ wopts + if any (not . (`elem` ".0123456789")) h + then Left $ "--host requires an IP address, not "++show h + else Right () getHledgerWebOpts :: IO WebOpts getHledgerWebOpts = processArgs webmode >>= return . decodeRawOpts >>= rawOptsToWebOpts diff --git a/hledger-web/Settings.hs b/hledger-web/Settings.hs index 3c147dc29..759851895 100644 --- a/hledger-web/Settings.hs +++ b/hledger-web/Settings.hs @@ -20,22 +20,22 @@ import Settings.Development import Data.Default (def) import Text.Hamlet -import Text.Printf (printf) - hledgerorgurl, manualurl :: String hledgerorgurl = "http://hledger.org" manualurl = hledgerorgurl++"/manual" +-- | The default IP address to listen on. May be overridden with --host. +defhost :: String +defhost = "127.0.0.1" + -- | The default TCP port to listen on. May be overridden with --port. defport :: Int defport = 5000 -defbaseurl :: Int -> String -defbaseurl port = printf "http://localhost:%d" port - - - +defbaseurl :: String -> Int -> String +defbaseurl host port = + "http://" ++ host ++ if port /= 80 then ":" ++ show port else "" -- Static setting below. Changing these requires a recompile diff --git a/hledger-web/doc/hledger-web.1 b/hledger-web/doc/hledger-web.1 index 7e50fc4af..4883a63fa 100644 --- a/hledger-web/doc/hledger-web.1 +++ b/hledger-web/doc/hledger-web.1 @@ -59,21 +59,32 @@ Web\ app\ will\ auto\-exit\ after\ a\ few\ minutes\ with\ no\ browsers\ (or\ pre \f[] .fi .PP -With \f[C]\-\-server\f[], it starts the web app in non\-transient mode +With \f[C]\-\-serve\f[], it starts the web app in non\-transient mode and logs requests to the console. -Typically when running hledger web as part of a website you\[aq]ll want -to use \f[C]\-\-base\-url\f[] to set the protocol/hostname/port/path to -be used in hyperlinks. -The \f[C]\-\-file\-url\f[] option allows static files to be served from -a different url, eg for better caching or cookie\-less serving. .PP -You can use \f[C]\-\-port\f[] to listen on a different TCP port, eg if -you are running multiple hledger\-web instances. -This need not be the same as the PORT in the base url. +By default the server listens on IP address 127.0.0.1, accessible only +to local requests. +You can use \f[C]\-\-host\f[] to change this, eg +\f[C]\-\-host\ 0.0.0.0\f[] to listen on all configured addresses. .PP -Note there is no built\-in access control, so you will need to hide -hledger\-web behind an authenticating proxy (such as apache or nginx) if -you want to restrict who can see and add entries to your journal. +Similarly, use \f[C]\-\-port\f[] to set a TCP port other than 5000, eg +if you are running multiple hledger\-web instances. +.PP +You can use \f[C]\-\-base\-url\f[] to change the protocol, hostname, +port and path that appear in hyperlinks, useful for integrating +hledger\-web within a larger website. +The default is \f[C]http://HOST:PORT/\f[] using the server\[aq]s +configured host address and TCP port. +.PP +With \f[C]\-\-file\-url\f[] you can set a different base url for static +files, eg for better caching or cookie\-less serving on high performance +websites. +.PP +Note there is no built\-in access control (aside from listening on +127.0.0.1 by default). +So you will need to hide hledger\-web behind an authenticating proxy +(such as apache or nginx) if you want to restrict who can see and add +entries to your journal. .PP Command\-line options and arguments may be used to set an initial filter on the data. diff --git a/hledger-web/doc/hledger-web.1.info b/hledger-web/doc/hledger-web.1.info index c23a07ba5..41f6dbd24 100644 --- a/hledger-web/doc/hledger-web.1.info +++ b/hledger-web/doc/hledger-web.1.info @@ -38,20 +38,29 @@ Starting web app on port 5000 with base url http://localhost:5000 Starting web browser if possible Web app will auto-exit after a few minutes with no browsers (or press ctrl-c) - With `--server', it starts the web app in non-transient mode and -logs requests to the console. Typically when running hledger web as part -of a website you'll want to use `--base-url' to set the -protocol/hostname/port/path to be used in hyperlinks. The `--file-url' -option allows static files to be served from a different url, eg for -better caching or cookie-less serving. + With `--serve', it starts the web app in non-transient mode and logs +requests to the console. - You can use `--port' to listen on a different TCP port, eg if you -are running multiple hledger-web instances. This need not be the same as -the PORT in the base url. + By default the server listens on IP address 127.0.0.1, accessible +only to local requests. You can use `--host' to change this, eg `--host +0.0.0.0' to listen on all configured addresses. - Note there is no built-in access control, so you will need to hide -hledger-web behind an authenticating proxy (such as apache or nginx) if -you want to restrict who can see and add entries to your journal. + Similarly, use `--port' to set a TCP port other than 5000, eg if you +are running multiple hledger-web instances. + + You can use `--base-url' to change the protocol, hostname, port and +path that appear in hyperlinks, useful for integrating hledger-web +within a larger website. The default is `http://HOST:PORT/' using the +server's configured host address and TCP port. + + With `--file-url' you can set a different base url for static files, +eg for better caching or cookie-less serving on high performance +websites. + + Note there is no built-in access control (aside from listening on +127.0.0.1 by default). So you will need to hide hledger-web behind an +authenticating proxy (such as apache or nginx) if you want to restrict +who can see and add entries to your journal. Command-line options and arguments may be used to set an initial filter on the data. This is not shown in the web UI, but it will be @@ -193,7 +202,7 @@ before options as shown above.  Tag Table: Node: Top90 -Node: OPTIONS2997 -Ref: #options3084 +Node: OPTIONS3307 +Ref: #options3394  End Tag Table diff --git a/hledger-web/doc/hledger-web.1.m4.md b/hledger-web/doc/hledger-web.1.m4.md index 7a3434f42..73f133733 100644 --- a/hledger-web/doc/hledger-web.1.m4.md +++ b/hledger-web/doc/hledger-web.1.m4.md @@ -62,19 +62,25 @@ Starting web browser if possible Web app will auto-exit after a few minutes with no browsers (or press ctrl-c) ``` -With `--server`, it starts the web app in non-transient mode and logs -requests to the console. Typically when running hledger web as part -of a website you'll want to use `--base-url` to set the -protocol/hostname/port/path to be used in hyperlinks. The -`--file-url` option allows static files to be served from a different -url, eg for better caching or cookie-less serving. +With `--serve`, it starts the web app in non-transient mode and logs +requests to the console. -You can use `--port` to listen on a different TCP port, eg if you are -running multiple hledger-web instances. This need not be the same as -the PORT in the base url. +By default the server listens on IP address 127.0.0.1, accessible only to local requests. +You can use `--host` to change this, eg `--host 0.0.0.0` to listen on all configured addresses. -Note there is no built-in access control, so you will need to hide -hledger-web behind an authenticating proxy (such as apache or nginx) +Similarly, use `--port` to set a TCP port other than 5000, eg if you are +running multiple hledger-web instances. + +You can use `--base-url` to change the protocol, hostname, port and path that appear in hyperlinks, +useful eg for integrating hledger-web within a larger website. +The default is `http://HOST:PORT/` using the server's configured host address and TCP port +(or `http://HOST` if PORT is 80). + +With `--file-url` you can set a different base url for static files, +eg for better caching or cookie-less serving on high performance websites. + +Note there is no built-in access control (aside from listening on 127.0.0.1 by default). +So you will need to hide hledger-web behind an authenticating proxy (such as apache or nginx) if you want to restrict who can see and add entries to your journal. Command-line options and arguments may be used to set an initial diff --git a/hledger-web/doc/hledger-web.1.txt b/hledger-web/doc/hledger-web.1.txt index 525e97fd6..e8ae255ce 100644 --- a/hledger-web/doc/hledger-web.1.txt +++ b/hledger-web/doc/hledger-web.1.txt @@ -48,32 +48,40 @@ DESCRIPTION Starting web browser if possible Web app will auto-exit after a few minutes with no browsers (or press ctrl-c) - With --server, it starts the web app in non-transient mode and logs - requests to the console. Typically when running hledger web as part of - a website you'll want to use --base-url to set the protocol/host- - name/port/path to be used in hyperlinks. The --file-url option allows - static files to be served from a different url, eg for better caching - or cookie-less serving. + With --serve, it starts the web app in non-transient mode and logs + requests to the console. - You can use --port to listen on a different TCP port, eg if you are - running multiple hledger-web instances. This need not be the same as - the PORT in the base url. + By default the server listens on IP address 127.0.0.1, accessible only + to local requests. You can use --host to change this, eg + --host 0.0.0.0 to listen on all configured addresses. - Note there is no built-in access control, so you will need to hide - hledger-web behind an authenticating proxy (such as apache or nginx) if - you want to restrict who can see and add entries to your journal. + Similarly, use --port to set a TCP port other than 5000, eg if you are + running multiple hledger-web instances. + + You can use --base-url to change the protocol, hostname, port and path + that appear in hyperlinks, useful for integrating hledger-web within a + larger website. The default is http://HOST:PORT/ using the server's + configured host address and TCP port. + + With --file-url you can set a different base url for static files, eg + for better caching or cookie-less serving on high performance websites. + + Note there is no built-in access control (aside from listening on + 127.0.0.1 by default). So you will need to hide hledger-web behind an + authenticating proxy (such as apache or nginx) if you want to restrict + who can see and add entries to your journal. Command-line options and arguments may be used to set an initial filter - on the data. This is not shown in the web UI, but it will be applied + on the data. This is not shown in the web UI, but it will be applied in addition to any search query entered there. With journal and timeclock files (but not CSV files, currently) the web - app detects changes made by other means and will show the new data on - the next request. If a change makes the file unparseable, hledger-web + app detects changes made by other means and will show the new data on + the next request. If a change makes the file unparseable, hledger-web will show an error until the file has been fixed. OPTIONS - Note: if invoking hledger-web as a hledger subcommand, write -- before + Note: if invoking hledger-web as a hledger subcommand, write -- before options as shown above. --server @@ -84,21 +92,21 @@ OPTIONS set the TCP port to listen on (default: 5000) --base-url=URL - set the base url (default: http://localhost:PORT). You would + set the base url (default: http://localhost:PORT). You would change this when sharing over the network, or integrating within a larger website. --file-url=URL set the static files url (default: BASEURL/static). hledger-web - normally serves static files itself, but if you wanted to serve - them from another server for efficiency, you would set the url + normally serves static files itself, but if you wanted to serve + them from another server for efficiency, you would set the url with this. hledger general options: -h show general usage (or after COMMAND, the command's usage) - --help show the current program's manual as plain text (or after an + --help show the current program's manual as plain text (or after an add-on COMMAND, the add-on's manual) --man show the current program's manual with man @@ -115,7 +123,7 @@ OPTIONS use a different input file. For stdin, use - --rules-file=RULESFILE - Conversion rules file to use when reading CSV (default: + Conversion rules file to use when reading CSV (default: FILE.rules) --alias=OLD=NEW @@ -148,7 +156,7 @@ OPTIONS multiperiod/multicolumn report by year -p --period=PERIODEXP - set start date, end date, and/or reporting interval all at once + set start date, end date, and/or reporting interval all at once (overrides the flags above) --date2 @@ -176,29 +184,29 @@ OPTIONS show amounts in their cost price's commodity --pivot TAG - will transform the journal before any other processing by - replacing the account name of every posting having the tag TAG + will transform the journal before any other processing by + replacing the account name of every posting having the tag TAG with content VALUE by the account name "TAG:VALUE". - The TAG will only match if it is a full-length match. The pivot will - only happen if the TAG is on a posting, not if it is on the transac- - tion. If the tag value is a multi:level:account:name the new account + The TAG will only match if it is a full-length match. The pivot will + only happen if the TAG is on a posting, not if it is on the transac- + tion. If the tag value is a multi:level:account:name the new account name will be "TAG:multi:level:account:name". --anon show anonymized accounts and payees ENVIRONMENT LEDGER_FILE The journal file path when not specified with -f. Default: - ~/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.jour- + ~/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.jour- nal). FILES - Reads data from one or more files in hledger journal, timeclock, time- - dot, or CSV format specified with -f, or $LEDGER_FILE, or - $HOME/.hledger.journal (on windows, perhaps + Reads data from one or more files in hledger journal, timeclock, time- + dot, or CSV format specified with -f, or $LEDGER_FILE, or + $HOME/.hledger.journal (on windows, perhaps C:/Users/USER/.hledger.journal). BUGS - The need to precede options with -- when invoked from hledger is awk- + The need to precede options with -- when invoked from hledger is awk- ward. -f- doesn't work (hledger-web can't read from stdin). @@ -212,7 +220,7 @@ BUGS REPORTING BUGS - Report bugs at http://bugs.hledger.org (or on the #hledger IRC channel + Report bugs at http://bugs.hledger.org (or on the #hledger IRC channel or hledger mail list) @@ -226,7 +234,7 @@ COPYRIGHT SEE ALSO - hledger(1), hledger-ui(1), hledger-web(1), hledger-api(1), + hledger(1), hledger-ui(1), hledger-web(1), hledger-api(1), hledger_csv(5), hledger_journal(5), hledger_timeclock(5), hledger_time- dot(5), ledger(1)