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.
This commit is contained in:
Simon Michael 2016-11-21 07:38:58 -08:00
parent 73c198bc99
commit 1bcc091a44
8 changed files with 138 additions and 94 deletions

View File

@ -59,7 +59,7 @@ makeApplication opts j conf = do
return $ logWare app return $ logWare app
where where
logWare | development = logStdoutDev logWare | development = logStdoutDev
| server_ opts = logStdout | serve_ opts = logStdout
| otherwise = id | otherwise = id
makeFoundation :: AppConfig DefaultEnv Extra -> WebOpts -> IO App makeFoundation :: AppConfig DefaultEnv Extra -> WebOpts -> IO App

View File

@ -71,7 +71,7 @@ web opts j = do
d <- getCurrentDay d <- getCurrentDay
let initq = queryFromOpts d $ reportopts_ $ cliopts_ opts let initq = queryFromOpts d $ reportopts_ $ cliopts_ opts
j' = filterJournalTransactions initq j j' = filterJournalTransactions initq j
h = "127.0.0.1" h = host_ opts
p = port_ opts p = port_ opts
u = base_url_ opts u = base_url_ opts
staticRoot = pack <$> file_url_ opts staticRoot = pack <$> file_url_ opts
@ -82,8 +82,9 @@ web opts j = do
,appExtra = Extra "" Nothing staticRoot ,appExtra = Extra "" Nothing staticRoot
} }
app <- makeApplication opts j' appconfig app <- makeApplication opts j' appconfig
_ <- printf "Starting web app on host %s port %d with base url %s\n" h p u -- XXX would like to allow a host name not just an IP address here
if server_ opts _ <- printf "Starting web app on IP address %s port %d with base url %s\n" h p u
if serve_ opts
then do then do
putStrLn "Press ctrl-c to quit" putStrLn "Press ctrl-c to quit"
hFlush stdout hFlush stdout

View File

@ -21,14 +21,12 @@ version = ""
prognameandversion :: String prognameandversion :: String
prognameandversion = progname ++ " " ++ version :: String prognameandversion = progname ++ " " ++ version :: String
defbaseurlexample :: String
defbaseurlexample = (reverse $ drop 4 $ reverse $ defbaseurl defport) ++ "PORT"
webflags :: [Flag [([Char], [Char])]] webflags :: [Flag [([Char], [Char])]]
webflags = [ webflags = [
flagNone ["server"] (setboolopt "server") ("log requests, and don't browse or auto-exit") flagNone ["serve","server"] (setboolopt "serve") ("serve and log requests, don't browse or auto-exit")
,flagReq ["port"] (\s opts -> Right $ setopt "port" s opts) "PORT" ("set the tcp port (default: "++show defport++")") ,flagReq ["host"] (\s opts -> Right $ setopt "host" s opts) "IPADDR" ("listen on this IP address (default: "++defhost++")")
,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "BASEURL" ("set the base url (default: "++defbaseurlexample++")") ,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)") ,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 -- hledger-web options, used in hledger-web and above
data WebOpts = WebOpts { data WebOpts = WebOpts {
server_ :: Bool serve_ :: Bool
,host_ :: String
,port_ :: Int ,port_ :: Int
,base_url_ :: String ,base_url_ :: String
,file_url_ :: Maybe String ,file_url_ :: Maybe String
@ -62,17 +61,22 @@ defwebopts = WebOpts
def def
def def
def def
def
-- instance Default WebOpts where def = defwebopts -- instance Default WebOpts where def = defwebopts
rawOptsToWebOpts :: RawOpts -> IO WebOpts rawOptsToWebOpts :: RawOpts -> IO WebOpts
rawOptsToWebOpts rawopts = checkWebOpts <$> do rawOptsToWebOpts rawopts = checkWebOpts <$> do
cliopts <- rawOptsToCliOpts rawopts 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 { return defwebopts {
port_ = p serve_ = boolopt "serve" rawopts
,server_ = boolopt "server" rawopts ,host_ = h
,base_url_ = maybe (defbaseurl p) stripTrailingSlash $ maybestringopt "base-url" rawopts ,port_ = p
,base_url_ = b
,file_url_ = stripTrailingSlash <$> maybestringopt "file-url" rawopts ,file_url_ = stripTrailingSlash <$> maybestringopt "file-url" rawopts
,cliopts_ = cliopts ,cliopts_ = cliopts
} }
@ -80,7 +84,12 @@ rawOptsToWebOpts rawopts = checkWebOpts <$> do
stripTrailingSlash = reverse . dropWhile (=='/') . reverse -- yesod don't like it stripTrailingSlash = reverse . dropWhile (=='/') . reverse -- yesod don't like it
checkWebOpts :: WebOpts -> WebOpts 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 :: IO WebOpts
getHledgerWebOpts = processArgs webmode >>= return . decodeRawOpts >>= rawOptsToWebOpts getHledgerWebOpts = processArgs webmode >>= return . decodeRawOpts >>= rawOptsToWebOpts

View File

@ -20,22 +20,22 @@ import Settings.Development
import Data.Default (def) import Data.Default (def)
import Text.Hamlet import Text.Hamlet
import Text.Printf (printf)
hledgerorgurl, manualurl :: String hledgerorgurl, manualurl :: String
hledgerorgurl = "http://hledger.org" hledgerorgurl = "http://hledger.org"
manualurl = hledgerorgurl++"/manual" 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. -- | The default TCP port to listen on. May be overridden with --port.
defport :: Int defport :: Int
defport = 5000 defport = 5000
defbaseurl :: Int -> String defbaseurl :: String -> Int -> String
defbaseurl port = printf "http://localhost:%d" port defbaseurl host port =
"http://" ++ host ++ if port /= 80 then ":" ++ show port else ""
-- Static setting below. Changing these requires a recompile -- Static setting below. Changing these requires a recompile

View File

@ -59,21 +59,32 @@ Web\ app\ will\ auto\-exit\ after\ a\ few\ minutes\ with\ no\ browsers\ (or\ pre
\f[] \f[]
.fi .fi
.PP .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. 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 .PP
You can use \f[C]\-\-port\f[] to listen on a different TCP port, eg if By default the server listens on IP address 127.0.0.1, accessible only
you are running multiple hledger\-web instances. to local requests.
This need not be the same as the PORT in the base url. 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 .PP
Note there is no built\-in access control, so you will need to hide Similarly, use \f[C]\-\-port\f[] to set a TCP port other than 5000, eg
hledger\-web behind an authenticating proxy (such as apache or nginx) if if you are running multiple hledger\-web instances.
you want to restrict who can see and add entries to your journal. .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 .PP
Command\-line options and arguments may be used to set an initial filter Command\-line options and arguments may be used to set an initial filter
on the data. on the data.

View File

@ -38,20 +38,29 @@ Starting web app on port 5000 with base url http://localhost:5000
Starting web browser if possible Starting web browser if possible
Web app will auto-exit after a few minutes with no browsers (or press ctrl-c) 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 With `--serve', it starts the web app in non-transient mode and logs
logs requests to the console. Typically when running hledger web as part requests to the console.
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.
You can use `--port' to listen on a different TCP port, eg if you By default the server listens on IP address 127.0.0.1, accessible
are running multiple hledger-web instances. This need not be the same as only to local requests. You can use `--host' to change this, eg `--host
the PORT in the base url. 0.0.0.0' to listen on all configured addresses.
Note there is no built-in access control, so you will need to hide Similarly, use `--port' to set a TCP port other than 5000, eg if you
hledger-web behind an authenticating proxy (such as apache or nginx) if are running multiple hledger-web instances.
you want to restrict who can see and add entries to your journal.
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 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 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: Tag Table:
Node: Top90 Node: Top90
Node: OPTIONS2997 Node: OPTIONS3307
Ref: #options3084 Ref: #options3394
 
End Tag Table End Tag Table

View File

@ -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) 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 With `--serve`, it starts the web app in non-transient mode and logs
requests to the console. Typically when running hledger web as part requests to the console.
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.
You can use `--port` to listen on a different TCP port, eg if you are By default the server listens on IP address 127.0.0.1, accessible only to local requests.
running multiple hledger-web instances. This need not be the same as You can use `--host` to change this, eg `--host 0.0.0.0` to listen on all configured addresses.
the PORT in the base url.
Note there is no built-in access control, so you will need to hide Similarly, use `--port` to set a TCP port other than 5000, eg if you are
hledger-web behind an authenticating proxy (such as apache or nginx) 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. 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 Command-line options and arguments may be used to set an initial

View File

@ -48,32 +48,40 @@ DESCRIPTION
Starting web browser if possible Starting web browser if possible
Web app will auto-exit after a few minutes with no browsers (or press ctrl-c) 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 With --serve, it starts the web app in non-transient mode and logs
requests to the console. Typically when running hledger web as part of requests to the console.
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.
You can use --port to listen on a different TCP port, eg if you are By default the server listens on IP address 127.0.0.1, accessible only
running multiple hledger-web instances. This need not be the same as to local requests. You can use --host to change this, eg
the PORT in the base url. --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 Similarly, use --port to set a TCP port other than 5000, eg if you are
hledger-web behind an authenticating proxy (such as apache or nginx) if running multiple hledger-web instances.
you want to restrict who can see and add entries to your journal.
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 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. in addition to any search query entered there.
With journal and timeclock files (but not CSV files, currently) the web 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 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 the next request. If a change makes the file unparseable, hledger-web
will show an error until the file has been fixed. will show an error until the file has been fixed.
OPTIONS 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. options as shown above.
--server --server
@ -84,21 +92,21 @@ OPTIONS
set the TCP port to listen on (default: 5000) set the TCP port to listen on (default: 5000)
--base-url=URL --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 change this when sharing over the network, or integrating within
a larger website. a larger website.
--file-url=URL --file-url=URL
set the static files url (default: BASEURL/static). hledger-web set the static files url (default: BASEURL/static). hledger-web
normally serves static files itself, but if you wanted to serve normally serves static files itself, but if you wanted to serve
them from another server for efficiency, you would set the url them from another server for efficiency, you would set the url
with this. with this.
hledger general options: hledger general options:
-h show general usage (or after COMMAND, the command's usage) -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) add-on COMMAND, the add-on's manual)
--man show the current program's manual with man --man show the current program's manual with man
@ -115,7 +123,7 @@ OPTIONS
use a different input file. For stdin, use - use a different input file. For stdin, use -
--rules-file=RULESFILE --rules-file=RULESFILE
Conversion rules file to use when reading CSV (default: Conversion rules file to use when reading CSV (default:
FILE.rules) FILE.rules)
--alias=OLD=NEW --alias=OLD=NEW
@ -148,7 +156,7 @@ OPTIONS
multiperiod/multicolumn report by year multiperiod/multicolumn report by year
-p --period=PERIODEXP -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) (overrides the flags above)
--date2 --date2
@ -176,29 +184,29 @@ OPTIONS
show amounts in their cost price's commodity show amounts in their cost price's commodity
--pivot TAG --pivot TAG
will transform the journal before any other processing by will transform the journal before any other processing by
replacing the account name of every posting having the tag TAG replacing the account name of every posting having the tag TAG
with content VALUE by the account name "TAG:VALUE". with content VALUE by the account name "TAG:VALUE".
The TAG will only match if it is a full-length match. The pivot will 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- 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 tion. If the tag value is a multi:level:account:name the new account
name will be "TAG:multi:level:account:name". name will be "TAG:multi:level:account:name".
--anon show anonymized accounts and payees --anon show anonymized accounts and payees
ENVIRONMENT ENVIRONMENT
LEDGER_FILE The journal file path when not specified with -f. Default: 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). nal).
FILES FILES
Reads data from one or more files in hledger journal, timeclock, time- Reads data from one or more files in hledger journal, timeclock, time-
dot, or CSV format specified with -f, or $LEDGER_FILE, or dot, or CSV format specified with -f, or $LEDGER_FILE, or
$HOME/.hledger.journal (on windows, perhaps $HOME/.hledger.journal (on windows, perhaps
C:/Users/USER/.hledger.journal). C:/Users/USER/.hledger.journal).
BUGS 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. ward.
-f- doesn't work (hledger-web can't read from stdin). -f- doesn't work (hledger-web can't read from stdin).
@ -212,7 +220,7 @@ BUGS
REPORTING 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) or hledger mail list)
@ -226,7 +234,7 @@ COPYRIGHT
SEE ALSO 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- hledger_csv(5), hledger_journal(5), hledger_timeclock(5), hledger_time-
dot(5), ledger(1) dot(5), ledger(1)