Update feed handler to respond with atom+xml Content-Type; nix flake update

This commit is contained in:
James Eversole 2024-10-17 09:31:34 -05:00
parent 754302e543
commit a11fcd37f7
3 changed files with 23 additions and 20 deletions

30
flake.lock generated
View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"haskell-flake": { "haskell-flake": {
"locked": { "locked": {
"lastModified": 1707835791, "lastModified": 1728845985,
"narHash": "sha256-oQbDPHtver9DO8IJCBMq/TVbscCkxuw9tIfBBti71Yk=", "narHash": "sha256-0KkAWCRBNpno3f+E1rvV9TOr0iuweqncWGn1KtbrGmo=",
"owner": "srid", "owner": "srid",
"repo": "haskell-flake", "repo": "haskell-flake",
"rev": "5113f700d6e92199fbe0574f7d12c775bb169702", "rev": "2393b55948866f39afcfa7d8a53893a096bcd284",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -33,20 +33,14 @@
}, },
"nixpkgs-lib": { "nixpkgs-lib": {
"locked": { "locked": {
"dir": "lib", "lastModified": 1727825735,
"lastModified": 1706550542, "narHash": "sha256-0xHYkMkeLVQAMa7gvkddbPqpxph+hDzdu1XdGPJR+Os=",
"narHash": "sha256-UcsnCG6wx++23yeER4Hg18CXWbgNpqNXcHIo5/1Y+hc=", "type": "tarball",
"owner": "NixOS", "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz"
"repo": "nixpkgs",
"rev": "97b17f32362e475016f942bbdfda4a4a72a8a652",
"type": "github"
}, },
"original": { "original": {
"dir": "lib", "type": "tarball",
"owner": "NixOS", "url": "https://github.com/NixOS/nixpkgs/archive/fb192fec7cc7a4c26d51779e9bab07ce6fa5597a.tar.gz"
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
} }
}, },
"parts": { "parts": {
@ -54,11 +48,11 @@
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs-lib": "nixpkgs-lib"
}, },
"locked": { "locked": {
"lastModified": 1706830856, "lastModified": 1727826117,
"narHash": "sha256-a0NYyp+h9hlb7ddVz4LUn1vT/PLwqfrWYcHMvFB1xYg=", "narHash": "sha256-K5ZLCyfO/Zj9mPFldf3iwS6oZStJcU4tSpiXTMYaaL0=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "b253292d9c0a5ead9bc98c4e9a26c6312e27d69f", "rev": "3d04084d54bedc3d6b8b736c70ef449225c361b1",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -1,6 +1,6 @@
cabal-version: 3.0 cabal-version: 3.0
name: sampu name: sampu
version: 0.9.2 version: 0.10.0
license: ISC license: ISC
author: James Eversole author: James Eversole
maintainer: james@eversole.co maintainer: james@eversole.co
@ -23,6 +23,7 @@ executable sampu
, feed >= 1.3.2.0 , feed >= 1.3.2.0
, filemanip >= 0.3.6.1 , filemanip >= 0.3.6.1
, filepath >= 1.4.2.2 , filepath >= 1.4.2.2
, http-types
, lucid >= 2.11.0 , lucid >= 2.11.0
, text >= 2.0 , text >= 2.0
, time >= 1.12.0 , time >= 1.12.0

View File

@ -8,11 +8,13 @@ import Fragments.Styles as S
import qualified Text.Atom.Feed as Atom import qualified Text.Atom.Feed as Atom
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Lazy (ByteString)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy.Encoding as TLE import qualified Data.Text.Lazy.Encoding as TLE
import Data.Time.Clock (UTCTime(..), getCurrentTime) import Data.Time.Clock (UTCTime(..), getCurrentTime)
import Data.Time.Format (formatTime, defaultTimeLocale) import Data.Time.Format (formatTime, defaultTimeLocale)
import Lucid (Html) import Lucid (Html)
import Network.HTTP.Types (status200, hContentType)
import System.Directory (doesFileExist, getModificationTime) import System.Directory (doesFileExist, getModificationTime)
import System.FilePath.Find ( always, extension, fileName, find, (&&?) import System.FilePath.Find ( always, extension, fileName, find, (&&?)
, (/~?), (==?) ) , (/~?), (==?) )
@ -68,9 +70,15 @@ feed = do
<*> fmap (\x -> timeFormat x) getCurrentTime <*> fmap (\x -> timeFormat x) getCurrentTime
feedData <- liftIO $ mapM (makePost baseUrl) postNames feedData <- liftIO $ mapM (makePost baseUrl) postNames
-- Send an XML response with an automatically populated Atom feed -- Send an XML response with an automatically populated Atom feed
send $ xml $ TLE.encodeUtf8 $ renderFeed send $ atom
$ TLE.encodeUtf8
$ renderFeed
$ autoFeed (baseFeed title time baseUrl) feedData $ autoFeed (baseFeed title time baseUrl) feedData
where where
-- Atom feed response headers
atom :: ByteString -> Response
atom f = withHeader (hContentType, "application/atom+xml") $ raw status200 [] $ f
-- Base feed data structure which we populate with entries -- Base feed data structure which we populate with entries
baseFeed :: String -> String -> String -> Atom.Feed baseFeed :: String -> String -> String -> Atom.Feed
baseFeed title time baseUrl = Atom.nullFeed baseFeed title time baseUrl = Atom.nullFeed