{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedLabels #-}
{-# OPTIONS_GHC -Wall #-}

-- | Serve pages via 'ScottyM'
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

-- | serve a Page via a ScottyM
servePageWith :: RoutePattern -> PageConfig -> Page -> ScottyM ()
servePageWith :: RoutePattern -> PageConfig -> Page -> ScottyM ()
servePageWith RoutePattern
rp PageConfig
pc Page
p =
  [ScottyM ()] -> ScottyM ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ ([ScottyM ()] -> ScottyM ()) -> [ScottyM ()] -> ScottyM ()
forall a b. (a -> b) -> a -> b
$ [ScottyM ()]
servedir [ScottyM ()] -> [ScottyM ()] -> [ScottyM ()]
forall a. Semigroup a => a -> a -> a
<> [ScottyM ()
getpage]
  where
    getpage :: ScottyM ()
getpage = case PageConfig
pc PageConfig
-> Optic' A_Lens NoIx PageConfig PageConcerns -> PageConcerns
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. IsLabel "concerns" (Optic' A_Lens NoIx PageConfig PageConcerns)
Optic' A_Lens NoIx PageConfig PageConcerns
#concerns of
      PageConcerns
Inline ->
        RoutePattern -> ActionM () -> ScottyM ()
get RoutePattern
rp (Text -> ActionM ()
html (Text -> ActionM ()) -> Text -> ActionM ()
forall a b. (a -> b) -> a -> b
$ Html () -> Text
forall a. Html a -> Text
renderText (Html () -> Text) -> Html () -> Text
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 -> ScottyM ()
middleware (Middleware -> ScottyM ()) -> Middleware -> ScottyM ()
forall a b. (a -> b) -> a -> b
$ Policy -> Middleware
staticPolicy (Policy -> Middleware) -> Policy -> Middleware
forall a b. (a -> b) -> a -> b
$ [(String, String)] -> Policy
only [(String
cssfp, String
cssfp), (String
jsfp, String
jsfp)]
              RoutePattern -> ActionM () -> ScottyM ()
get
                RoutePattern
rp
                ( do
                    IO () -> ActionM ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO () -> ActionM ()) -> IO () -> ActionM ()
forall a b. (a -> b) -> a -> b
$ String -> String -> IO ()
writeFile' String
cssfp (Text -> String
unpack Text
css)
                    IO () -> ActionM ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO () -> ActionM ()) -> IO () -> ActionM ()
forall a b. (a -> b) -> a -> b
$ String -> String -> IO ()
writeFile' String
jsfp (Text -> String
unpack Text
js)
                    Text -> ActionM ()
html (Text -> ActionM ()) -> Text -> ActionM ()
forall a b. (a -> b) -> a -> b
$ Html () -> Text
forall a. Html a -> Text
renderText Html ()
h
                )
    cssfp :: String
cssfp = PageConfig
pc PageConfig -> Optic' A_Lens NoIx PageConfig String -> String
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. IsLabel
  "filenames"
  (Optic
     A_Lens
     NoIx
     PageConfig
     PageConfig
     (Concerns String)
     (Concerns String))
Optic
  A_Lens
  NoIx
  PageConfig
  PageConfig
  (Concerns String)
  (Concerns String)
#filenames Optic
  A_Lens
  NoIx
  PageConfig
  PageConfig
  (Concerns String)
  (Concerns String)
-> Optic
     A_Lens NoIx (Concerns String) (Concerns String) String String
-> Optic' A_Lens NoIx PageConfig String
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
% IsLabel
  "cssConcern"
  (Optic
     A_Lens NoIx (Concerns String) (Concerns String) String String)
Optic A_Lens NoIx (Concerns String) (Concerns String) String String
#cssConcern
    jsfp :: String
jsfp = PageConfig
pc PageConfig -> Optic' A_Lens NoIx PageConfig String -> String
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. IsLabel
  "filenames"
  (Optic
     A_Lens
     NoIx
     PageConfig
     PageConfig
     (Concerns String)
     (Concerns String))
Optic
  A_Lens
  NoIx
  PageConfig
  PageConfig
  (Concerns String)
  (Concerns String)
#filenames Optic
  A_Lens
  NoIx
  PageConfig
  PageConfig
  (Concerns String)
  (Concerns String)
-> Optic
     A_Lens NoIx (Concerns String) (Concerns String) String String
-> Optic' A_Lens NoIx PageConfig String
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
% IsLabel
  "jsConcern"
  (Optic
     A_Lens NoIx (Concerns String) (Concerns String) String String)
Optic A_Lens NoIx (Concerns String) (Concerns String) String String
#jsConcern
    writeFile' :: String -> String -> IO ()
writeFile' String
fp String
s = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (String
s String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
forall a. Monoid a => a
mempty) (String -> String -> IO ()
writeFile String
fp String
s)
    servedir :: [ScottyM ()]
servedir = (\String
x -> Middleware -> ScottyM ()
middleware (Middleware -> ScottyM ()) -> Middleware -> ScottyM ()
forall a b. (a -> b) -> a -> b
$ Policy -> Middleware
staticPolicy (Policy
noDots Policy -> Policy -> Policy
forall a. Semigroup a => a -> a -> a
<> String -> Policy
addBase String
x)) (String -> ScottyM ()) -> [String] -> [ScottyM ()]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PageConfig
pc PageConfig -> Optic' A_Lens NoIx PageConfig [String] -> [String]
forall k s (is :: IxList) a.
Is k A_Getter =>
s -> Optic' k is s a -> a
^. IsLabel "localdirs" (Optic' A_Lens NoIx PageConfig [String])
Optic' A_Lens NoIx PageConfig [String]
#localdirs