From fb32d54a90c3eada94e81e61cd79e49c87cdddda Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 18 Jan 2016 08:46:56 -0800 Subject: [PATCH] api: also serve files from static/; drop ServerT --- hledger-api/hledger-api.hs | 68 ++++++++++++++++------------------- hledger-api/static/README.txt | 1 + 2 files changed, 32 insertions(+), 37 deletions(-) create mode 100644 hledger-api/static/README.txt diff --git a/hledger-api/hledger-api.hs b/hledger-api/hledger-api.hs index fe780a413..e6fcf5a41 100644 --- a/hledger-api/hledger-api.hs +++ b/hledger-api/hledger-api.hs @@ -74,19 +74,6 @@ serveApi f p j = do printf "Starting web api for %s on port %d\nPress ctrl-c to quit\n" f p Warp.run p $ hledgerApiApp j -hledgerApiApp :: Journal -> Wai.Application -hledgerApiApp j = Servant.serve hledgerApi hledgerApiServer - where - hledgerApi :: Proxy HledgerApi - hledgerApi = Proxy - - -- add Reader to the server monad so handlers can see the journal - hledgerApiServer :: Servant.Server HledgerApi - hledgerApiServer = Servant.enter readerToEither hledgerAPIServer - where - readerToEither :: Reader Journal :~> EitherT ServantErr IO - readerToEither = Nat $ \r -> return (runReader r j) - type HledgerApi = "accountnames" :> Get '[JSON] [AccountName] :<|> "transactions" :> Get '[JSON] [Transaction] @@ -95,32 +82,39 @@ type HledgerApi = :<|> "accounts" :> Get '[JSON] [Account] :<|> "reports" :> "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport + :<|> Raw -hledgerAPIServer :: ServerT HledgerApi (Reader Journal) -hledgerAPIServer = - accountnamesH - :<|> transactionsH - :<|> pricesH - :<|> commoditiesH - :<|> accountsH - :<|> accounttransactionsH +hledgerApiApp :: Journal -> Wai.Application +hledgerApiApp j = Servant.serve api server where - accountnamesH = journalAccountNames <$> ask - transactionsH = jtxns <$> ask - pricesH = jmarketprices <$> ask - commoditiesH = (M.keys . jcommoditystyles) <$> ask - accountsH = laccounts . ledgerFromJournal Hledger.Cli.Any <$> ask - accounttransactionsH (a::AccountName) = do - j <- ask - -- d <- liftIO getCurrentDay - let - ropts = defreportopts - -- ropts' = ropts {depth_=Nothing - -- ,balancetype_=HistoricalBalance - -- } - q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts' - thisacctq = Acct $ accountNameToAccountRegex a -- includes subs - return $ accountTransactionsReport ropts j q thisacctq + api :: Proxy HledgerApi + api = Proxy + + server :: Server HledgerApi + server = + accountnamesH + :<|> transactionsH + :<|> pricesH + :<|> commoditiesH + :<|> accountsH + :<|> accounttransactionsH + :<|> serveDirectory "static" + where + accountnamesH = return $ journalAccountNames j + transactionsH = return $ jtxns j + pricesH = return $ jmarketprices j + commoditiesH = return $ (M.keys . jcommoditystyles) j + accountsH = return $ laccounts $ ledgerFromJournal Hledger.Cli.Any j + accounttransactionsH (a::AccountName) = do + -- d <- liftIO getCurrentDay + let + ropts = defreportopts + -- ropts' = ropts {depth_=Nothing + -- ,balancetype_=HistoricalBalance + -- } + q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts' + thisacctq = Acct $ accountNameToAccountRegex a -- includes subs + return $ accountTransactionsReport ropts j q thisacctq -- brief toJSON definitions included to avoid https://github.com/bos/aeson/issues/290 -- use toEncoding = genericToEncoding defaultOptions instead ? diff --git a/hledger-api/static/README.txt b/hledger-api/static/README.txt new file mode 100644 index 000000000..2b8913edb --- /dev/null +++ b/hledger-api/static/README.txt @@ -0,0 +1 @@ +Files under this directory are served by hledger-api when no other API route is matched.