2017-09-22 21:21:11 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
2016-01-18 20:09:09 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2016-01-10 02:22:28 +03:00
|
|
|
|
2016-01-10 18:09:25 +03:00
|
|
|
module Main where
|
|
|
|
|
2016-04-05 03:44:29 +03:00
|
|
|
import Lens.Micro ((&), (.~))
|
2016-01-10 02:22:28 +03:00
|
|
|
import Control.Monad
|
|
|
|
import Data.Aeson
|
2016-01-22 01:16:00 +03:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BL8
|
2016-01-10 19:32:42 +03:00
|
|
|
import Data.Decimal
|
2018-04-17 00:47:04 +03:00
|
|
|
import Data.Default
|
2016-01-10 19:58:40 +03:00
|
|
|
import qualified Data.Map as M
|
2016-01-10 02:22:28 +03:00
|
|
|
import Data.Proxy
|
2016-11-21 19:01:06 +03:00
|
|
|
import Data.String (fromString)
|
2016-01-22 01:16:00 +03:00
|
|
|
import Data.Swagger
|
2016-01-17 21:19:48 +03:00
|
|
|
import Data.Text hiding (map,reverse)
|
2016-01-10 02:22:28 +03:00
|
|
|
import Network.Wai as Wai
|
|
|
|
import Network.Wai.Handler.Warp as Warp
|
2016-09-22 21:56:38 +03:00
|
|
|
import Network.Wai.Middleware.RequestLogger
|
2016-01-10 18:09:25 +03:00
|
|
|
import Safe
|
2016-01-10 02:22:28 +03:00
|
|
|
import Servant
|
2016-01-22 01:16:00 +03:00
|
|
|
import Servant.Swagger
|
2016-01-10 18:09:25 +03:00
|
|
|
import System.Console.Docopt
|
|
|
|
import System.Environment (getArgs)
|
2016-01-10 19:58:40 +03:00
|
|
|
import System.Exit
|
2016-01-10 02:22:28 +03:00
|
|
|
import Text.Printf
|
|
|
|
|
2016-01-17 21:19:48 +03:00
|
|
|
import Hledger.Query
|
2016-01-10 18:09:25 +03:00
|
|
|
import Hledger.Cli hiding (Reader, version)
|
2016-01-10 02:22:28 +03:00
|
|
|
|
2019-02-04 02:18:42 +03:00
|
|
|
#ifdef VERSION
|
|
|
|
hledgerApiVersion = VERSION
|
|
|
|
#else
|
|
|
|
hledgerApiVersion = "dev build"
|
|
|
|
#endif
|
2016-01-10 18:09:25 +03:00
|
|
|
|
2016-01-22 01:16:00 +03:00
|
|
|
-- https://github.com/docopt/docopt.hs#readme
|
2019-02-04 02:18:42 +03:00
|
|
|
-- XXX VERSION is "X.Y" and the quotes appear in the output
|
2016-01-10 18:09:25 +03:00
|
|
|
doc :: Docopt
|
|
|
|
doc = [docopt|
|
2019-02-04 02:18:42 +03:00
|
|
|
hledger-api VERSION
|
2016-01-10 02:22:28 +03:00
|
|
|
|
2016-01-18 20:37:34 +03:00
|
|
|
Serves hledger data and reports as a JSON web API.
|
|
|
|
|
2016-01-10 18:09:25 +03:00
|
|
|
Usage:
|
|
|
|
hledger-api [options]
|
2016-01-22 01:16:00 +03:00
|
|
|
start API server
|
|
|
|
hledger-api --swagger
|
|
|
|
print API docs in Swagger 2.0 format
|
2016-01-10 18:09:25 +03:00
|
|
|
hledger-api --version
|
2017-07-07 04:00:46 +03:00
|
|
|
hledger-api -h|--help
|
2016-01-10 18:09:25 +03:00
|
|
|
|
|
|
|
Options:
|
2016-11-21 19:01:06 +03:00
|
|
|
-f --file FILE use a different input file
|
|
|
|
(default: $LEDGER_FILE or ~/.hledger.journal)
|
|
|
|
-d --static-dir DIR serve files from a different directory
|
|
|
|
(default: .)
|
|
|
|
--host IPADDR listen on this IP address (default: 127.0.0.1)
|
|
|
|
-p --port PORT listen on this TCP port (default: 8001)
|
|
|
|
--version show version
|
2017-07-07 04:00:46 +03:00
|
|
|
-h --help show usage
|
2016-01-10 18:09:25 +03:00
|
|
|
|]
|
|
|
|
|
2016-01-25 00:37:34 +03:00
|
|
|
swaggerSpec :: Swagger
|
|
|
|
swaggerSpec = toSwagger (Proxy :: Proxy HledgerApi)
|
2016-04-06 01:18:50 +03:00
|
|
|
& info.title .~ "hledger API"
|
|
|
|
& info.version .~ pack hledgerApiVersion
|
|
|
|
& info.description .~ Just "This is the API provided by hledger-api for reading hledger data"
|
|
|
|
& info.license .~ Just (License "GPLv3+" (Nothing))
|
2016-01-25 00:37:34 +03:00
|
|
|
|
2016-01-10 18:09:25 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
args <- getArgs >>= parseArgsOrExit doc
|
2017-07-07 04:00:46 +03:00
|
|
|
when (isPresent args (shortOption 'h') || isPresent args (longOption "help")) $ exitWithUsage doc
|
2017-07-17 19:39:23 +03:00
|
|
|
when (isPresent args (longOption "version")) $ putStrLn ("hledger-api " ++ hledgerApiVersion) >> exitSuccess
|
2016-01-22 01:49:28 +03:00
|
|
|
when (isPresent args (longOption "swagger")) $ BL8.putStrLn (encode swaggerSpec) >> exitSuccess
|
2016-11-21 19:01:06 +03:00
|
|
|
let
|
|
|
|
defh = "127.0.0.1"
|
|
|
|
h = getArgWithDefault args defh (longOption "host")
|
|
|
|
defp = "8001"
|
2016-01-10 18:09:25 +03:00
|
|
|
p <- case readMay $ getArgWithDefault args defp (longOption "port") of
|
|
|
|
Nothing -> exitWithUsage doc
|
|
|
|
Just n -> return n
|
|
|
|
deff <- defaultJournalPath
|
|
|
|
let f = getArgWithDefault args deff (longOption "file")
|
|
|
|
requireJournalFileExists f
|
2016-01-18 20:37:34 +03:00
|
|
|
let
|
2016-01-25 00:37:34 +03:00
|
|
|
defd = "."
|
2016-01-18 20:37:34 +03:00
|
|
|
d = getArgWithDefault args defd (longOption "static-dir")
|
2018-04-18 01:13:13 +03:00
|
|
|
readJournalFile def f >>= either error' (serveApi h p d f)
|
2016-01-10 18:09:25 +03:00
|
|
|
|
2016-11-21 19:01:06 +03:00
|
|
|
serveApi :: String -> Int -> FilePath -> FilePath -> Journal -> IO ()
|
|
|
|
serveApi h p d f j = do
|
|
|
|
printf "Starting web api http://%s:%d/api/v1 for %s\n" h p f
|
|
|
|
printf "and file server http://%s:%d for %s/\n" h p d
|
2016-01-25 00:37:34 +03:00
|
|
|
printf "Press ctrl-c to quit\n"
|
2016-11-21 19:01:06 +03:00
|
|
|
let warpsettings = defaultSettings
|
|
|
|
& setHost (fromString h)
|
|
|
|
& setPort p
|
|
|
|
Warp.runSettings warpsettings $
|
2016-09-22 21:56:38 +03:00
|
|
|
logStdout $
|
|
|
|
hledgerApiApp d j
|
2016-01-10 18:09:25 +03:00
|
|
|
|
2016-01-10 02:22:28 +03:00
|
|
|
type HledgerApi =
|
2016-01-25 00:37:34 +03:00
|
|
|
"api" :> "v1" :>
|
|
|
|
(
|
|
|
|
"accountnames" :> Get '[JSON] [AccountName]
|
|
|
|
:<|> "transactions" :> Get '[JSON] [Transaction]
|
|
|
|
:<|> "prices" :> Get '[JSON] [MarketPrice]
|
2016-05-08 02:18:04 +03:00
|
|
|
:<|> "commodities" :> Get '[JSON] [CommoditySymbol]
|
2016-01-25 00:37:34 +03:00
|
|
|
:<|> "accounts" :> Get '[JSON] [Account]
|
|
|
|
:<|> "accounts" :> Capture "acct" AccountName :> Get '[JSON] AccountTransactionsReport
|
|
|
|
)
|
|
|
|
|
|
|
|
type HledgerSwaggerApi =
|
|
|
|
"swagger.json" :> Get '[JSON] Swagger
|
|
|
|
:<|> HledgerApi
|
|
|
|
|
|
|
|
type HledgerSwaggerFilesApi =
|
|
|
|
HledgerSwaggerApi
|
2016-01-18 19:46:56 +03:00
|
|
|
:<|> Raw
|
2016-01-10 02:22:28 +03:00
|
|
|
|
2016-01-18 20:37:34 +03:00
|
|
|
hledgerApiApp :: FilePath -> Journal -> Wai.Application
|
2016-01-25 00:37:34 +03:00
|
|
|
hledgerApiApp staticdir j = Servant.serve api server
|
2016-01-10 02:22:28 +03:00
|
|
|
where
|
2016-01-25 00:37:34 +03:00
|
|
|
api :: Proxy HledgerSwaggerFilesApi
|
2016-01-18 19:46:56 +03:00
|
|
|
api = Proxy
|
|
|
|
|
2016-01-25 00:37:34 +03:00
|
|
|
server :: Server HledgerSwaggerFilesApi
|
2016-01-18 19:46:56 +03:00
|
|
|
server =
|
2016-01-25 00:37:34 +03:00
|
|
|
(
|
|
|
|
return swaggerSpec
|
|
|
|
--
|
|
|
|
:<|> accountnamesH
|
2016-01-18 19:46:56 +03:00
|
|
|
:<|> transactionsH
|
|
|
|
:<|> pricesH
|
|
|
|
:<|> commoditiesH
|
|
|
|
:<|> accountsH
|
|
|
|
:<|> accounttransactionsH
|
2016-01-25 00:37:34 +03:00
|
|
|
)
|
|
|
|
--
|
2017-08-25 03:06:46 +03:00
|
|
|
:<|> serveDirectoryFileServer staticdir
|
2016-01-18 19:46:56 +03:00
|
|
|
where
|
|
|
|
accountnamesH = return $ journalAccountNames j
|
|
|
|
transactionsH = return $ jtxns j
|
2019-06-11 03:24:50 +03:00
|
|
|
pricesH = return $ map priceDirectiveToMarketPrice $ jpricedirectives j
|
2016-05-23 10:32:55 +03:00
|
|
|
commoditiesH = return $ (M.keys . jinferredcommodities) j
|
2016-01-19 12:35:28 +03:00
|
|
|
accountsH = return $ ledgerTopAccounts $ ledgerFromJournal Hledger.Query.Any j
|
2016-01-18 19:46:56 +03:00
|
|
|
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
|
2016-01-10 19:32:42 +03:00
|
|
|
|
2019-02-19 09:55:55 +03:00
|
|
|
-- avoiding https://github.com/bos/aeson/issues/290 - no longer needed ?
|
|
|
|
--instance ToJSON Status where toJSON = genericToJSON defaultOptions -- avoiding https://github.com/bos/aeson/issues/290
|
|
|
|
--instance ToJSON GenericSourcePos where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON Decimal where toJSON = toJSON . show
|
|
|
|
--instance ToJSON Amount where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON AmountStyle where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON Side where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON DigitGroupStyle where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON MixedAmount where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON BalanceAssertion where toJSON = genericToJSON defaultOptions
|
2019-06-04 03:26:27 +03:00
|
|
|
--instance ToJSON AmountPrice where toJSON = genericToJSON defaultOptions
|
2019-02-19 09:55:55 +03:00
|
|
|
--instance ToJSON MarketPrice where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON PostingType where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON Posting where
|
|
|
|
-- toJSON Posting{..} =
|
|
|
|
-- object
|
|
|
|
-- ["pdate" .= toJSON pdate
|
|
|
|
-- ,"pdate2" .= toJSON pdate2
|
|
|
|
-- ,"pstatus" .= toJSON pstatus
|
|
|
|
-- ,"paccount" .= toJSON paccount
|
|
|
|
-- ,"pamount" .= toJSON pamount
|
|
|
|
-- ,"pcomment" .= toJSON pcomment
|
|
|
|
-- ,"ptype" .= toJSON ptype
|
|
|
|
-- ,"ptags" .= toJSON ptags
|
|
|
|
-- ,"pbalanceassertion" .= toJSON pbalanceassertion
|
|
|
|
-- ,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction)
|
|
|
|
-- ]
|
|
|
|
--instance ToJSON Transaction where toJSON = genericToJSON defaultOptions
|
|
|
|
--instance ToJSON Account where
|
|
|
|
-- toJSON a =
|
|
|
|
-- object
|
|
|
|
-- ["aname" .= toJSON (aname a)
|
|
|
|
-- ,"aebalance" .= toJSON (aebalance a)
|
|
|
|
-- ,"aibalance" .= toJSON (aibalance a)
|
|
|
|
-- ,"anumpostings" .= toJSON (anumpostings a)
|
|
|
|
-- ,"aboring" .= toJSON (aboring a)
|
|
|
|
-- ,"aparentname" .= toJSON (maybe "" aname $ aparent a)
|
|
|
|
-- ,"asubs" .= toJSON (map toJSON $ asubs a)
|
|
|
|
-- ]
|
|
|
|
|
|
|
|
-- Convert things to JSON for serving to clients
|
|
|
|
instance ToJSON Status
|
|
|
|
instance ToJSON GenericSourcePos
|
|
|
|
instance ToJSON Decimal where toJSON = toJSON . show
|
|
|
|
instance ToJSON Amount
|
|
|
|
instance ToJSON AmountStyle
|
|
|
|
instance ToJSON Side
|
|
|
|
instance ToJSON DigitGroupStyle
|
|
|
|
instance ToJSON MixedAmount
|
|
|
|
instance ToJSON BalanceAssertion
|
2019-06-04 03:26:27 +03:00
|
|
|
instance ToJSON AmountPrice
|
2019-02-19 09:55:55 +03:00
|
|
|
instance ToJSON MarketPrice
|
|
|
|
instance ToJSON PostingType
|
2016-01-17 22:07:26 +03:00
|
|
|
instance ToJSON Posting where
|
|
|
|
toJSON Posting{..} =
|
|
|
|
object
|
|
|
|
["pdate" .= toJSON pdate
|
|
|
|
,"pdate2" .= toJSON pdate2
|
|
|
|
,"pstatus" .= toJSON pstatus
|
|
|
|
,"paccount" .= toJSON paccount
|
|
|
|
,"pamount" .= toJSON pamount
|
|
|
|
,"pcomment" .= toJSON pcomment
|
|
|
|
,"ptype" .= toJSON ptype
|
|
|
|
,"ptags" .= toJSON ptags
|
|
|
|
,"pbalanceassertion" .= toJSON pbalanceassertion
|
|
|
|
,"ptransactionidx" .= toJSON (maybe "" (show.tindex) ptransaction)
|
|
|
|
]
|
2019-02-19 09:55:55 +03:00
|
|
|
instance ToJSON Transaction
|
2016-01-17 22:07:26 +03:00
|
|
|
instance ToJSON Account where
|
|
|
|
toJSON a =
|
|
|
|
object
|
|
|
|
["aname" .= toJSON (aname a)
|
|
|
|
,"aebalance" .= toJSON (aebalance a)
|
|
|
|
,"aibalance" .= toJSON (aibalance a)
|
|
|
|
,"anumpostings" .= toJSON (anumpostings a)
|
|
|
|
,"aboring" .= toJSON (aboring a)
|
|
|
|
,"aparentname" .= toJSON (maybe "" aname $ aparent a)
|
|
|
|
,"asubs" .= toJSON (map toJSON $ asubs a)
|
|
|
|
]
|
2019-02-19 09:55:55 +03:00
|
|
|
|
|
|
|
-- convert things to Schema for swagger API description
|
2017-06-16 02:54:34 +03:00
|
|
|
instance ToSchema Status
|
2016-01-22 01:16:00 +03:00
|
|
|
instance ToSchema GenericSourcePos
|
|
|
|
instance ToSchema Decimal
|
|
|
|
where
|
2016-04-06 01:18:50 +03:00
|
|
|
declareNamedSchema _proxy = pure $ NamedSchema (Just "Decimal") schema
|
2016-01-22 01:16:00 +03:00
|
|
|
where
|
|
|
|
schema = mempty
|
2016-04-06 01:18:50 +03:00
|
|
|
& type_ .~ SwaggerNumber
|
|
|
|
& example .~ Just (toJSON (100 :: Decimal))
|
2016-01-22 01:16:00 +03:00
|
|
|
instance ToSchema Amount
|
|
|
|
instance ToSchema AmountStyle
|
|
|
|
instance ToSchema Side
|
|
|
|
instance ToSchema DigitGroupStyle
|
|
|
|
instance ToSchema MixedAmount
|
2018-10-12 06:37:20 +03:00
|
|
|
instance ToSchema BalanceAssertion
|
2019-06-04 03:26:27 +03:00
|
|
|
instance ToSchema AmountPrice
|
2017-09-22 21:21:11 +03:00
|
|
|
#if MIN_VERSION_swagger2(2,1,5)
|
|
|
|
where declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
|
|
|
|
#endif
|
2016-01-22 01:16:00 +03:00
|
|
|
instance ToSchema MarketPrice
|
|
|
|
instance ToSchema PostingType
|
|
|
|
instance ToSchema Posting
|
|
|
|
instance ToSchema Transaction
|
|
|
|
instance ToSchema Account
|
2019-01-25 01:39:38 +03:00
|
|
|
instance ToSchema AccountDeclarationInfo
|
2016-01-22 01:16:00 +03:00
|
|
|
-- instance ToSchema AccountTransactionsReport
|