feat: web: serve openapi.json

Serving the OpenAPI JSON schema means that clients can auto-discover how
to use the API. I am particularly interested in this because open-webui
can use OpenAPI to add context to LLM responses.
This commit is contained in:
Ben Sima 2025-05-23 14:24:57 -04:00 committed by Simon Michael
parent e360e50497
commit 4e9aa5825d
4 changed files with 18 additions and 0 deletions

View File

@ -19,10 +19,14 @@ module Hledger.Web.Handler.MiscR
, getManageR
, getRobotsR
, getRootR
, getOpenApiR
) where
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Yaml as Yaml
import qualified Data.ByteString as BS
import Data.FileEmbed (embedFile)
import Yesod.Default.Handlers (getFaviconR, getRobotsR)
import Hledger
@ -107,3 +111,13 @@ getAccounttransactionsR a = do
styleAmounts (journalCommodityStylesWith HardRounding j) $
accountTransactionsReport rspec{_rsQuery=Any} j thisacctq
openApiYaml :: BS.ByteString
openApiYaml = $(embedFile "config/openapi.yaml")
getOpenApiR :: Handler Value
getOpenApiR =
case Yaml.decodeEither' openApiYaml of
Left _ -> notFound
Right openapi -> do
addHeader "Content-Type" "application/json"
return openapi

View File

@ -2,6 +2,8 @@
/robots.txt RobotsR GET
/static StaticR Static getStatic
/openapi.json OpenApiR GET
/ RootR GET
/journal JournalR GET
/register RegisterR GET

View File

@ -176,6 +176,7 @@ library
, data-default
, directory >=1.2.3.0
, extra >=1.7.11
, file-embed >=0.0.16.0
, filepath
, githash >=0.1.6.2
, hjsmin

View File

@ -123,6 +123,7 @@ library:
- Decimal >=0.5.1
- directory >=1.2.3.0
- extra >=1.7.11
- file-embed >=0.0.16.0
- filepath
- githash >=0.1.6.2
- hjsmin