Automatic creation of 'All Posts' post index based on existing MD files at application init

This commit is contained in:
2024-02-24 11:34:25 -06:00
parent 7f97da838f
commit c793b17bed
9 changed files with 102 additions and 40 deletions

View File

@ -15,16 +15,14 @@ import Web.Twain
-- Get the port to listen on from the ENV and start the webserver
main :: [FilePath] -> IO ()
main mdFiles = do
main postNames = do
port <- Conf.appPort
run (read port :: Int) $
foldr ($) (notFound Handle.missing) (app mdFiles)
where
app mdFiles = preProcessors
++ routes
++ (map mdFileToRoute mdFiles)
++ postProcessors
let app = preProcessors
++ (routes postNames)
++ (buildMdRoutes postNames)
++ postProcessors
run (read port) $
foldr ($) (notFound Handle.missing) app
-- These Middlewares are executed before any routes are reached
preProcessors :: [Middleware]
@ -37,9 +35,17 @@ postProcessors :: [Middleware]
postProcessors = []
-- The application's core routes expressed as a list of WAI Middlewares
routes :: [Middleware]
routes =
[ get "/" Handle.index ]
routes :: [FilePath] -> [Middleware]
routes postNames =
[ get "/" Handle.index
, get "/posts" $ Handle.postsIndex postNames
, get "/contact" Handle.contact
, get "/feed" Handle.feed
]
-- Takes a post's name extracted from the filepath and returns a valid route
mdFileToRoute :: FilePath -> Middleware
mdFileToRoute fp = get (fromString $ "/posts/" ++ fp) (Handle.posts fp)
mdFileToRoute postName = get (fromString $ "/posts/" ++ postName) (Handle.posts postName)
buildMdRoutes :: [FilePath] -> [Middleware]
buildMdRoutes postNames = map mdFileToRoute postNames

View File

@ -11,24 +11,31 @@ import Web.Twain
index :: ResponderM a
index = do
-- Probably going to want to add ReaderT to the stack for this instead
title <- liftIO Conf.appTitle
-- Probably going to want to do this file reading and processing at app init
homeMd <- liftIO $ mdFileToLucid "./data/base/home.md"
sendLucidFragment
$ baseDoc title
$ baseNav
<> baseHome homeMd
homeMd <- liftIO $ mdFileToLucid "./data/posts/home.md"
sendLucidFragment $ basePage title (baseHome homeMd)
postsIndex :: [FilePath] -> ResponderM a
postsIndex postNames = do
title <- liftIO Conf.appTitle
sendLucidFragment $ basePage title (postIndex postNames)
posts :: FilePath -> ResponderM a
posts fp = do
posts postName = do
title <- liftIO Conf.appTitle
postMd <- liftIO $ mdFileToLucid
$ "./data/posts/" ++ fp ++ ".md"
sendLucidFragment
$ baseDoc title
$ baseNav
<> postMd
postMd <- liftIO $ mdFileToLucid ("./data/posts/" ++ postName ++ ".md")
sendLucidFragment $ basePage title (basePost postMd)
contact :: ResponderM a
contact = do
title <- liftIO Conf.appTitle
contactMd <- liftIO $ mdFileToLucid "./data/posts/contact.md"
sendLucidFragment $ basePage title (baseContact contactMd)
feed :: ResponderM a
feed = do
title <- liftIO Conf.appTitle
sendLucidFragment $ basePage title baseFeed
missing :: ResponderM a
missing = sendLucidFragment pageNotFound