Compositional HTML fragments

This commit is contained in:
James Eversole 2024-02-18 14:26:11 -06:00
parent aea0e6dfc8
commit d8f3bc3c70
5 changed files with 34 additions and 26 deletions

View File

@ -13,19 +13,20 @@ executable sampu
import: warnings import: warnings
main-is: Main.hs main-is: Main.hs
default-extensions: OverloadedStrings default-extensions: OverloadedStrings
ghc-options: -threaded -rtsopts -with-rtsopts=-N -optl-pthread -fPIC
build-depends: base build-depends: base
, bytestring >= 0.11.5.0 , bytestring >= 0.11.5.0
, commonmark >= 0.2.4 , commonmark >= 0.2.4
, dotenv >= 0.11.0.0 , dotenv >= 0.11.0.0
, lucid >= 2.11.0 , lucid >= 2.11.0
, text >= 2.0
, twain >= 2.1.0.0 , twain >= 2.1.0.0
, wai-extra >= 3.0 && < 3.2 , wai-extra >= 3.0 && < 3.2
, warp == 3.3.25 , warp == 3.3.25
hs-source-dirs: src hs-source-dirs: src
other-modules: other-modules: Core.Configuration
Core.Configuration Core.Handlers
Core.Handlers Core.HTTP
Core.HTTP Core.Rendering
Core.Rendering Templates.Base
Templates.Base
default-language: GHC2021 default-language: GHC2021

View File

@ -1,7 +1,7 @@
module Core.Configuration where module Core.Configuration where
import Configuration.Dotenv import Configuration.Dotenv
import System.Environment (getEnv, lookupEnv) import System.Environment (getEnv, lookupEnv)
main :: IO () main :: IO ()
main = do main = do

View File

@ -1,29 +1,30 @@
module Core.HTTP where module Core.HTTP where
import qualified Core.Configuration as Conf import qualified Core.Configuration as Conf
import qualified Core.Handlers as Handle import qualified Core.Handlers as Handle
import Network.Wai.Middleware.RequestLogger (logStdoutDev) import Network.Wai.Middleware.RequestLogger (logStdoutDev)
import Network.Wai.Handler.Warp (Port, run) import Network.Wai.Handler.Warp (Port, run)
import Web.Twain import Web.Twain
-- Get the port to listen on from the ENV and start the webserver
main :: IO () main :: IO ()
main = do main = do
port <- Conf.appPort port <- Conf.appPort
run (read port :: Int) $ run (read port :: Int) $
foldr ($) (notFound Handle.missing) app foldr ($) (notFound Handle.missing) app
-- Combine our Preprocessor Middlewares and Routes to create an App -- These Middlewares are always executed before any routes are reached
app :: [Middleware]
app = preProcessors ++ routes
-- These Middleware are always executed before any routes are reached
preProcessors :: [Middleware] preProcessors :: [Middleware]
preProcessors = [logStdoutDev] preProcessors = [logStdoutDev]
-- The application's core routes expressed as a list of WAI middlewares -- The application's routes expressed as a list of WAI Middlewares
routes :: [Middleware] routes :: [Middleware]
routes = routes =
[ get "/" Handle.index [ get "/" Handle.index
, get "/echo/:testParam" Handle.testRoute , get "/echo/:testParam" Handle.testRoute
] ]
-- Combine our Preprocessor Middlewares and Routes to create an App to run
app :: [Middleware]
app = preProcessors ++ routes

View File

@ -1,12 +1,16 @@
module Core.Handlers where module Core.Handlers where
import qualified Templates.Base as T import qualified Core.Configuration as Conf
import Core.Rendering as R import qualified Core.Rendering as R
import qualified Templates.Base as T
import Control.Monad.IO.Class (liftIO)
import Web.Twain import Web.Twain
index :: ResponderM a index :: ResponderM a
index = send $ html $ R.twain T.baseNav index = liftIO Conf.appTitle >>= (\title ->
send $ html $ R.twain $
T.baseDoc title T.baseNav)
testRoute :: ResponderM a testRoute :: ResponderM a
testRoute = do testRoute = do

View File

@ -1,13 +1,15 @@
module Templates.Base where module Templates.Base where
import Data.Text
import Data.String (fromString)
import Lucid import Lucid
baseNav :: Html () baseDoc :: String -> Html () -> Html ()
baseNav = doctypehtml_ $ do baseDoc title nav = doctypehtml_ $ do
head_ $ do head_ $ do
title_ getBlogTitle title_ (fromString title)
link_ [rel_ "stylesheet", type_ "text/css", href_ "style.css"] link_ [rel_ "stylesheet", type_ "text/css", href_ "style.css"]
body_ $ do body_ nav
h1_ "This will be a nav eventually."
where baseNav :: Html ()
getBlogTitle = "unimplemented getEnv" baseNav = h1_ "This will be a nav eventually."