diff --git a/default.nix b/default.nix index ce081f3..0dbef43 100644 --- a/default.nix +++ b/default.nix @@ -1,9 +1,9 @@ { mkDerivation, aeson, annotated-exception, base, bytestring , containers, directory, ekg-core, exceptions, filepath -, generic-lens, http-client, http-types, katip, lens, lens-aeson -, lib, monad-control, mtl, optparse-generic, pandoc, pipes -, pipes-concurrency, retry, servant, servant-server, stm, streamly -, streamly-core, text, time, transformers-base, unliftio +, generic-lens, githash, http-client, http-types, katip, lens +, lens-aeson, lib, monad-control, mtl, optparse-generic, pandoc +, pipes, pipes-concurrency, retry, servant, servant-server, stm +, streamly, streamly-core, text, time, transformers-base, unliftio , unordered-containers, wai, warp, wreq }: mkDerivation { @@ -14,14 +14,14 @@ mkDerivation { isExecutable = true; libraryHaskellDepends = [ aeson annotated-exception base bytestring containers directory - ekg-core exceptions filepath generic-lens http-client http-types - katip lens lens-aeson monad-control mtl optparse-generic pandoc - pipes pipes-concurrency retry servant servant-server stm streamly - streamly-core text time transformers-base unliftio + ekg-core exceptions filepath generic-lens githash http-client + http-types katip lens lens-aeson monad-control mtl optparse-generic + pandoc pipes pipes-concurrency retry servant servant-server stm + streamly streamly-core text time transformers-base unliftio unordered-containers wai warp wreq ]; executableHaskellDepends = [ base ]; testHaskellDepends = [ base ]; - license = lib.licenses.bsd3; + license = lib.meta.getLicenseFromSpdxId "BSD-3-Clause"; mainProgram = "wallabag-tagger"; } diff --git a/src/Server.hs b/src/Server.hs index 87fc5cb..a634a6b 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -21,6 +21,7 @@ import System.Metrics.Distribution qualified as D import UnliftIO (liftIO) import UnliftIO.IORef (readIORef) import Data.Coerce (coerce) +import Version (Version, version) data Health = OK @@ -57,7 +58,8 @@ newtype Metrics = Metrics (HashMap Text SampleValue) data WallabagRoutes mode = WallabagRoutes { _getHealth :: mode :- "health" :> Get '[PlainText, JSON] Health, - _getMetrics :: mode :- "metrics" :> Get '[JSON] Metrics + _getMetrics :: mode :- "metrics" :> Get '[JSON] Metrics, + _getVersion :: mode :- "metrics" :> Get '[JSON] Version } deriving (Generic) @@ -67,12 +69,16 @@ wallabagServer :: IORef Health -> WallabagRoutes (AsServerT M) wallabagServer health = WallabagRoutes { _getHealth = getHealth health, - _getMetrics = getMetrics + _getMetrics = getMetrics, + _getVersion = getVersion } getHealth :: IORef Health -> M Health getHealth = readIORef +getVersion :: M Version +getVersion = pure version -- Comes from TH during compile time + getMetrics :: M Metrics getMetrics = do sample <- view metricsStore >>= liftIO . EKG.sampleAll diff --git a/src/Version.hs b/src/Version.hs new file mode 100644 index 0000000..4c41e38 --- /dev/null +++ b/src/Version.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE TemplateHaskell #-} +{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} +{-# HLINT ignore "Unused LANGUAGE pragma" #-} +module Version where + +import GitHash (tGitInfoCwd, giHash, giDirty, giCommitDate, giCommitMessage) +import qualified Data.Aeson as A +import qualified Data.ByteString.Lazy.Char8 as BL +import GHC.Generics (Generic) + + +-- Populate the version via the 'githash' package +data Version = Version + { versionCommit :: String + , versionDirty :: Bool + , versionCommitDate :: String + , versionCommitMessage :: String + } + deriving (Show, Generic) + +instance A.ToJSON Version + +version :: Version +version = Version + { versionCommit = giHash gi + , versionDirty = giDirty gi + , versionCommitDate = giCommitDate gi + , versionCommitMessage = giCommitMessage gi + } + where + gi = $$tGitInfoCwd + +test :: IO () +test = BL.putStrLn $ A.encode version diff --git a/wallabag-tagger.cabal b/wallabag-tagger.cabal index 59ff5fc..19c67f9 100644 --- a/wallabag-tagger.cabal +++ b/wallabag-tagger.cabal @@ -27,6 +27,7 @@ library HTTP Server Tracer + Version -- other-modules: -- other-extensions: build-depends: base ^>=4.18.2.1 @@ -64,6 +65,7 @@ library , generic-lens , optparse-generic , containers + , githash hs-source-dirs: src default-language: GHC2021