api: --static-dir/-d option
This commit is contained in:
parent
69e2218b09
commit
ae9defb718
@ -41,6 +41,8 @@ doc :: Docopt
|
|||||||
doc = [docopt|
|
doc = [docopt|
|
||||||
hledger-api 0.27.98
|
hledger-api 0.27.98
|
||||||
|
|
||||||
|
Serves hledger data and reports as a JSON web API.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
hledger-api [options]
|
hledger-api [options]
|
||||||
hledger-api --version
|
hledger-api --version
|
||||||
@ -49,6 +51,8 @@ Usage:
|
|||||||
Options:
|
Options:
|
||||||
-f --file FILE use a different input file
|
-f --file FILE use a different input file
|
||||||
(default: $LEDGER_FILE or ~/.hledger.journal)
|
(default: $LEDGER_FILE or ~/.hledger.journal)
|
||||||
|
-d --static-dir DIR serve files from a different directory
|
||||||
|
(default: ./static/)
|
||||||
-p --port PORT use a different TCP port (default: 8001)
|
-p --port PORT use a different TCP port (default: 8001)
|
||||||
--version show version
|
--version show version
|
||||||
-h --help show this help
|
-h --help show this help
|
||||||
@ -66,12 +70,15 @@ main = do
|
|||||||
deff <- defaultJournalPath
|
deff <- defaultJournalPath
|
||||||
let f = getArgWithDefault args deff (longOption "file")
|
let f = getArgWithDefault args deff (longOption "file")
|
||||||
requireJournalFileExists f
|
requireJournalFileExists f
|
||||||
readJournalFile Nothing Nothing True f >>= either error' (serveApi f p)
|
let
|
||||||
|
defd = "static"
|
||||||
|
d = getArgWithDefault args defd (longOption "static-dir")
|
||||||
|
readJournalFile Nothing Nothing True f >>= either error' (serveApi p d f)
|
||||||
|
|
||||||
serveApi :: FilePath -> Int -> Journal -> IO ()
|
serveApi :: Int -> FilePath -> FilePath -> Journal -> IO ()
|
||||||
serveApi f p j = do
|
serveApi p d f j = do
|
||||||
printf "Starting web api for %s on port %d\nPress ctrl-c to quit\n" f p
|
printf "Starting web api on port %d using files from %s for %s\nPress ctrl-c to quit\n" p d f
|
||||||
Warp.run p $ hledgerApiApp j
|
Warp.run p $ hledgerApiApp d j
|
||||||
|
|
||||||
type HledgerApi =
|
type HledgerApi =
|
||||||
"accountnames" :> Get '[JSON] [AccountName]
|
"accountnames" :> Get '[JSON] [AccountName]
|
||||||
@ -82,8 +89,8 @@ type HledgerApi =
|
|||||||
:<|> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
|
:<|> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
|
||||||
:<|> Raw
|
:<|> Raw
|
||||||
|
|
||||||
hledgerApiApp :: Journal -> Wai.Application
|
hledgerApiApp :: FilePath -> Journal -> Wai.Application
|
||||||
hledgerApiApp j = Servant.serve api server
|
hledgerApiApp staticdir j = Servant.serve api server
|
||||||
where
|
where
|
||||||
api :: Proxy HledgerApi
|
api :: Proxy HledgerApi
|
||||||
api = Proxy
|
api = Proxy
|
||||||
@ -96,13 +103,13 @@ hledgerApiApp j = Servant.serve api server
|
|||||||
:<|> commoditiesH
|
:<|> commoditiesH
|
||||||
:<|> accountsH
|
:<|> accountsH
|
||||||
:<|> accounttransactionsH
|
:<|> accounttransactionsH
|
||||||
:<|> serveDirectory "static"
|
:<|> serveDirectory staticdir
|
||||||
where
|
where
|
||||||
accountnamesH = return $ journalAccountNames j
|
accountnamesH = return $ journalAccountNames j
|
||||||
transactionsH = return $ jtxns j
|
transactionsH = return $ jtxns j
|
||||||
pricesH = return $ jmarketprices j
|
pricesH = return $ jmarketprices j
|
||||||
commoditiesH = return $ (M.keys . jcommoditystyles) j
|
commoditiesH = return $ (M.keys . jcommoditystyles) j
|
||||||
accountsH = return $ laccounts $ ledgerFromJournal Hledger.Cli.Any j
|
accountsH = return $ laccounts $ ledgerFromJournal Hledger.Query.Any j
|
||||||
accounttransactionsH (a::AccountName) = do
|
accounttransactionsH (a::AccountName) = do
|
||||||
-- d <- liftIO getCurrentDay
|
-- d <- liftIO getCurrentDay
|
||||||
let
|
let
|
||||||
@ -114,9 +121,7 @@ hledgerApiApp j = Servant.serve api server
|
|||||||
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs
|
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs
|
||||||
return $ accountTransactionsReport ropts j q thisacctq
|
return $ accountTransactionsReport ropts j q thisacctq
|
||||||
|
|
||||||
-- brief toJSON definitions included to avoid https://github.com/bos/aeson/issues/290
|
instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290
|
||||||
-- use toEncoding = genericToEncoding defaultOptions instead ?
|
|
||||||
instance ToJSON ClearedStatus where toJSON = genericToJSON defaultOptions
|
|
||||||
instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
|
instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
|
||||||
instance ToJSON Decimal where
|
instance ToJSON Decimal where
|
||||||
toJSON = toJSON . show
|
toJSON = toJSON . show
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user