|
| 1 | +{-# LANGUAGE BlockArguments #-} |
| 2 | +{-# LANGUAGE QuasiQuotes #-} |
| 3 | +{-# LANGUAGE TemplateHaskell #-} |
| 4 | +{-# LANGUAGE UndecidableInstances #-} |
| 5 | + |
| 6 | +{- | A very simple markdown website using Ema.Route.Lib.Extra.PandocRoute. |
| 7 | +
|
| 8 | + Also demostrates how to set up a custom server for following the currently open |
| 9 | + note, using a websocket for editor integration. |
| 10 | +-} |
| 11 | +module Ema.Example.Ex06_Markdown where |
| 12 | + |
| 13 | +import Control.Monad.Logger (LogLevel (..), MonadLoggerIO (..), defaultLoc, logInfoNS) |
| 14 | +import Control.Monad.Logger.Extras (runLoggerLoggingT) |
| 15 | +import Data.Default (Default (..)) |
| 16 | +import Data.Dependent.Sum (DSum (..)) |
| 17 | +import Data.Generics.Sum.Any |
| 18 | +import Data.Map (member) |
| 19 | +import Ema |
| 20 | +import Ema.CLI qualified as CLI |
| 21 | +import Ema.Route.Generic.TH |
| 22 | +import Ema.Route.Lib.Extra.PandocRoute qualified as Pandoc |
| 23 | +import Ema.Server (EmaServerOptions (..), EmaWsHandler (..), wsClientJS) |
| 24 | +import Network.WebSockets qualified as WS |
| 25 | +import Optics.Core ((%)) |
| 26 | +import System.Directory (makeAbsolute) |
| 27 | +import System.FilePath (isAbsolute, isRelative, makeRelative) |
| 28 | +import Text.Blaze.Html.Renderer.Utf8 qualified as RU |
| 29 | +import Text.Blaze.Html5 ((!)) |
| 30 | +import Text.Blaze.Html5 qualified as H |
| 31 | +import Text.Blaze.Html5.Attributes qualified as A |
| 32 | +import UnliftIO.Async (race) |
| 33 | +import UnliftIO.STM (TChan, dupTChan, newBroadcastTChanIO, readTChan, writeTChan) |
| 34 | + |
| 35 | +data Arg = Arg |
| 36 | + { pandocArg :: Pandoc.Arg |
| 37 | + , editorWsAddress :: String |
| 38 | + , editorWsPort :: Int |
| 39 | + } |
| 40 | + deriving stock (Generic) |
| 41 | + |
| 42 | +instance Default Arg where |
| 43 | + def = |
| 44 | + Arg |
| 45 | + { pandocArg = |
| 46 | + def |
| 47 | + { Pandoc.argBaseDir = "src/Ema/Example/Ex06_Markdown" |
| 48 | + } |
| 49 | + , editorWsAddress = "127.0.0.1" |
| 50 | + , editorWsPort = 9160 |
| 51 | + } |
| 52 | + |
| 53 | +data Model = Model |
| 54 | + { pandocModel :: Pandoc.Model |
| 55 | + , wsNextRoute :: TChan Route |
| 56 | + } |
| 57 | + deriving stock (Generic) |
| 58 | + |
| 59 | +newtype Route = Route Pandoc.PandocRoute |
| 60 | + deriving stock (Show, Eq, Ord, Generic) |
| 61 | + |
| 62 | +deriveGeneric ''Route |
| 63 | +deriveIsRoute |
| 64 | + ''Route |
| 65 | + [t| |
| 66 | + '[ WithModel Model |
| 67 | + , WithSubRoutes |
| 68 | + '[ Pandoc.PandocRoute |
| 69 | + ] |
| 70 | + ] |
| 71 | + |] |
| 72 | + |
| 73 | +instance EmaSite Route where |
| 74 | + type SiteArg Route = Arg |
| 75 | + |
| 76 | + siteInput act arg = do |
| 77 | + pandocDyn <- siteInput @Pandoc.PandocRoute act (pandocArg arg) |
| 78 | + editorWsDyn <- wsConnDyn arg |
| 79 | + return $ Model <$> pandocDyn <*> editorWsDyn |
| 80 | + |
| 81 | + siteOutput rp m (Route r) = do |
| 82 | + (pandoc, write) <- siteOutput (rp % _As @"Route") (pandocModel m) r |
| 83 | + let head' = H.title "Basic site" >> H.base ! A.href "/" |
| 84 | + body :: Text = coerce $ write pandoc |
| 85 | + html = RU.renderHtml do |
| 86 | + H.docType |
| 87 | + H.html ! A.lang "en" $ do |
| 88 | + H.head do |
| 89 | + H.meta ! A.charset "UTF-8" |
| 90 | + H.meta ! A.name "viewport" ! A.content "width=device-width, initial-scale=1" |
| 91 | + head' |
| 92 | + H.body $ H.preEscapedToHtml body |
| 93 | + return $ AssetGenerated Html html |
| 94 | + |
| 95 | +wsConnDyn :: forall m. (MonadLoggerIO m) => Arg -> m (Dynamic m (TChan Route)) |
| 96 | +wsConnDyn arg = do |
| 97 | + value <- newBroadcastTChanIO |
| 98 | + let manage :: m () |
| 99 | + manage = do |
| 100 | + logger <- askLoggerIO |
| 101 | + let log = logger defaultLoc "wsConnDyn" LevelInfo |
| 102 | + liftIO $ WS.runServer (editorWsAddress arg) (editorWsPort arg) \pendingConn -> do |
| 103 | + conn :: WS.Connection <- WS.acceptRequest pendingConn |
| 104 | + log "websocket connected" |
| 105 | + WS.withPingThread conn 30 pass $ |
| 106 | + void $ |
| 107 | + infinitely do |
| 108 | + msg <- liftIO $ toString @Text <$> WS.receiveData conn |
| 109 | + log $ "got message: " <> show msg |
| 110 | + baseDir <- makeAbsolute (Pandoc.argBaseDir $ pandocArg arg) |
| 111 | + let fp = makeRelative baseDir msg |
| 112 | + case Pandoc.mkPandocRoute fp of |
| 113 | + Just (_, route) |
| 114 | + -- We should have received an absolute file path inside the base dir |
| 115 | + | isAbsolute msg && isRelative fp -> |
| 116 | + atomically $ writeTChan value (Route route) |
| 117 | + _ -> pass |
| 118 | + return $ Dynamic (value, const manage) |
| 119 | + |
| 120 | +main :: IO () |
| 121 | +main = runWithFollow def |
| 122 | + |
| 123 | +runWithFollow :: |
| 124 | + SiteArg Route -> |
| 125 | + IO () |
| 126 | +runWithFollow input = do |
| 127 | + cli <- CLI.cliAction |
| 128 | + let cfg = SiteConfig cli followServerOptions |
| 129 | + result <- snd <$> runSiteWith @Route cfg input |
| 130 | + case result of |
| 131 | + CLI.Run _ :=> Identity () -> |
| 132 | + flip runLoggerLoggingT (CLI.getLogger cli) $ |
| 133 | + CLI.crash "ema" "Live server unexpectedly stopped" |
| 134 | + CLI.Generate _ :=> Identity _ -> pass |
| 135 | + |
| 136 | +followServerOptions :: EmaServerOptions Route |
| 137 | +followServerOptions = EmaServerOptions wsClientJS followServerHandler |
| 138 | + |
| 139 | +followServerHandler :: EmaWsHandler Route |
| 140 | +followServerHandler = EmaWsHandler handle |
| 141 | + where |
| 142 | + defaultHandler = unEmaWsHandler $ def @(EmaWsHandler ()) |
| 143 | + handle conn model = do |
| 144 | + either id id <$> race (defaultHandler conn ()) followHandler |
| 145 | + where |
| 146 | + rp = fromPrism_ $ routePrism model |
| 147 | + log = logInfoNS "followServerHandler" |
| 148 | + followHandler = do |
| 149 | + listenerChan <- atomically $ dupTChan $ wsNextRoute model |
| 150 | + route <- atomically $ readTChan listenerChan |
| 151 | + let Route pRoute = route |
| 152 | + path = routeUrl rp route |
| 153 | + if pRoute `member` Pandoc.modelPandocs (pandocModel model) |
| 154 | + then do |
| 155 | + log $ "switching to " <> show pRoute |
| 156 | + liftIO $ WS.sendTextData conn $ "SWITCH " <> path |
| 157 | + else log $ "invalid route " <> show pRoute |
| 158 | + followHandler |
0 commit comments