api: also serve files from static/; drop ServerT

This commit is contained in:
Simon Michael 2016-01-18 08:46:56 -08:00
parent cabcd80ad5
commit fb32d54a90
2 changed files with 32 additions and 37 deletions

View File

@ -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 printf "Starting web api for %s on port %d\nPress ctrl-c to quit\n" f p
Warp.run p $ hledgerApiApp j 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 = type HledgerApi =
"accountnames" :> Get '[JSON] [AccountName] "accountnames" :> Get '[JSON] [AccountName]
:<|> "transactions" :> Get '[JSON] [Transaction] :<|> "transactions" :> Get '[JSON] [Transaction]
@ -95,32 +82,39 @@ type HledgerApi =
:<|> "accounts" :> Get '[JSON] [Account] :<|> "accounts" :> Get '[JSON] [Account]
:<|> "reports" :> :<|> "reports" :>
"accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport "accounttransactions" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
:<|> Raw
hledgerAPIServer :: ServerT HledgerApi (Reader Journal) hledgerApiApp :: Journal -> Wai.Application
hledgerAPIServer = hledgerApiApp j = Servant.serve api server
accountnamesH
:<|> transactionsH
:<|> pricesH
:<|> commoditiesH
:<|> accountsH
:<|> accounttransactionsH
where where
accountnamesH = journalAccountNames <$> ask api :: Proxy HledgerApi
transactionsH = jtxns <$> ask api = Proxy
pricesH = jmarketprices <$> ask
commoditiesH = (M.keys . jcommoditystyles) <$> ask server :: Server HledgerApi
accountsH = laccounts . ledgerFromJournal Hledger.Cli.Any <$> ask server =
accounttransactionsH (a::AccountName) = do accountnamesH
j <- ask :<|> transactionsH
-- d <- liftIO getCurrentDay :<|> pricesH
let :<|> commoditiesH
ropts = defreportopts :<|> accountsH
-- ropts' = ropts {depth_=Nothing :<|> accounttransactionsH
-- ,balancetype_=HistoricalBalance :<|> serveDirectory "static"
-- } where
q = Hledger.Query.Any --filterQuery (not . queryIsDepth) $ queryFromOpts d ropts' accountnamesH = return $ journalAccountNames j
thisacctq = Acct $ accountNameToAccountRegex a -- includes subs transactionsH = return $ jtxns j
return $ accountTransactionsReport ropts j q thisacctq 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 -- brief toJSON definitions included to avoid https://github.com/bos/aeson/issues/290
-- use toEncoding = genericToEncoding defaultOptions instead ? -- use toEncoding = genericToEncoding defaultOptions instead ?

View File

@ -0,0 +1 @@
Files under this directory are served by hledger-api when no other API route is matched.