Dynamic route and handler creation for Markdown posts
This commit is contained in:
@ -1,20 +1,30 @@
|
||||
module Core.HTTP where
|
||||
|
||||
import qualified Core.Configuration as Conf
|
||||
import qualified Core.Handlers as Handle
|
||||
import qualified Core.Configuration as Conf
|
||||
import qualified Core.Handlers as Handle
|
||||
|
||||
import Network.Wai.Handler.Warp (Port, run)
|
||||
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
|
||||
import Network.Wai.Middleware.Static
|
||||
import Control.Monad ( mapM_ )
|
||||
import Data.String ( fromString )
|
||||
import Network.Wai.Handler.Warp ( Port, run )
|
||||
import Network.Wai.Middleware.RequestLogger ( logStdoutDev )
|
||||
import Network.Wai.Middleware.Static
|
||||
import System.FilePath ( takeFileName )
|
||||
|
||||
import Web.Twain
|
||||
|
||||
import Web.Twain
|
||||
|
||||
-- Get the port to listen on from the ENV and start the webserver
|
||||
main :: IO ()
|
||||
main = do
|
||||
main :: [FilePath] -> IO ()
|
||||
main mdFiles = do
|
||||
port <- Conf.appPort
|
||||
run (read port :: Int) $
|
||||
foldr ($) (notFound Handle.missing) app
|
||||
foldr ($) (notFound Handle.missing) (app mdFiles)
|
||||
where
|
||||
app mdFiles = preProcessors
|
||||
++ routes
|
||||
++ (map mdFileToRoute mdFiles)
|
||||
++ postProcessors
|
||||
|
||||
|
||||
-- These Middlewares are executed before any routes are reached
|
||||
preProcessors :: [Middleware]
|
||||
@ -22,11 +32,14 @@ preProcessors = [ logStdoutDev
|
||||
, staticPolicy $ noDots >-> addBase "data/assets/public"
|
||||
]
|
||||
|
||||
-- The application's routes expressed as a list of WAI Middlewares
|
||||
-- These Middlewares are executed after all other routes are exhausted
|
||||
postProcessors :: [Middleware]
|
||||
postProcessors = []
|
||||
|
||||
-- The application's core routes expressed as a list of WAI Middlewares
|
||||
routes :: [Middleware]
|
||||
routes =
|
||||
[ get "/" Handle.index ]
|
||||
|
||||
-- Combine our Preprocessor Middlewares and Routes to create an App to run
|
||||
app :: [Middleware]
|
||||
app = preProcessors ++ routes
|
||||
mdFileToRoute :: FilePath -> Middleware
|
||||
mdFileToRoute fp = get (fromString $ "/posts/" ++ fp) (Handle.posts fp)
|
||||
|
@ -20,6 +20,16 @@ index = do
|
||||
$ baseNav
|
||||
<> baseHome homeMd
|
||||
|
||||
posts :: FilePath -> ResponderM a
|
||||
posts fp = do
|
||||
title <- liftIO Conf.appTitle
|
||||
postMd <- liftIO $ mdFileToLucid
|
||||
$ "./data/posts/" ++ fp ++ ".md"
|
||||
sendLucidFragment
|
||||
$ baseDoc title
|
||||
$ baseNav
|
||||
<> postMd
|
||||
|
||||
missing :: ResponderM a
|
||||
missing = sendLucidFragment pageNotFound
|
||||
|
||||
|
Reference in New Issue
Block a user