{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLabels #-}
{-# OPTIONS_GHC -Wall #-}
module Web.Rep.Server
( servePageWith,
)
where
import Control.Monad
import Control.Monad.Trans.Class
import Data.Text (unpack)
import Lucid
import Network.Wai.Middleware.Static (addBase, noDots, only, staticPolicy)
import Optics.Core hiding (only)
import Web.Rep.Page
import Web.Rep.Render
import Web.Scotty
servePageWith :: RoutePattern -> PageConfig -> Page -> ScottyM ()
servePageWith :: RoutePattern -> PageConfig -> Page -> ScottyT Text IO ()
servePageWith RoutePattern
rp PageConfig
pc Page
p =
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ forall a b. (a -> b) -> a -> b
$ [ScottyT Text IO ()]
servedir forall a. Semigroup a => a -> a -> a
<> [ScottyT Text IO ()
getpage]
where
getpage :: ScottyT Text IO ()
getpage = case PageConfig
pc forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "concerns" a => a
#concerns of
PageConcerns
Inline ->
RoutePattern -> ActionM () -> ScottyT Text IO ()
get RoutePattern
rp (Text -> ActionM ()
html forall a b. (a -> b) -> a -> b
$ forall a. Html a -> Text
renderText forall a b. (a -> b) -> a -> b
$ PageConfig -> Page -> Html ()
renderPageHtmlWith PageConfig
pc Page
p)
PageConcerns
Separated ->
let (Text
css, Text
js, Html ()
h) = PageConfig -> Page -> (Text, Text, Html ())
renderPageWith PageConfig
pc Page
p
in do
Middleware -> ScottyT Text IO ()
middleware forall a b. (a -> b) -> a -> b
$ Policy -> Middleware
staticPolicy forall a b. (a -> b) -> a -> b
$ [(FilePath, FilePath)] -> Policy
only [(FilePath
cssfp, FilePath
cssfp), (FilePath
jsfp, FilePath
jsfp)]
RoutePattern -> ActionM () -> ScottyT Text IO ()
get
RoutePattern
rp
( do
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
writeFile' FilePath
cssfp (Text -> FilePath
unpack Text
css)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
writeFile' FilePath
jsfp (Text -> FilePath
unpack Text
js)
Text -> ActionM ()
html forall a b. (a -> b) -> a -> b
$ forall a. Html a -> Text
renderText Html ()
h
)
cssfp :: FilePath
cssfp = PageConfig
pc forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "filenames" a => a
#filenames forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall a. IsLabel "cssConcern" a => a
#cssConcern
jsfp :: FilePath
jsfp = PageConfig
pc forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "filenames" a => a
#filenames forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall a. IsLabel "jsConcern" a => a
#jsConcern
writeFile' :: FilePath -> FilePath -> IO ()
writeFile' FilePath
fp FilePath
s = forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FilePath
s forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty) (FilePath -> FilePath -> IO ()
writeFile FilePath
fp FilePath
s)
servedir :: [ScottyT Text IO ()]
servedir = (\FilePath
x -> Middleware -> ScottyT Text IO ()
middleware forall a b. (a -> b) -> a -> b
$ Policy -> Middleware
staticPolicy (Policy
noDots forall a. Semigroup a => a -> a -> a
<> FilePath -> Policy
addBase FilePath
x)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PageConfig
pc forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall a. IsLabel "localdirs" a => a
#localdirs