Skip to content

Instantly share code, notes, and snippets.

@dalaing
Created August 8, 2017 23:07
Show Gist options
  • Save dalaing/73b24b9faea0f51c8e4cbc6d1311516e to your computer and use it in GitHub Desktop.
Save dalaing/73b24b9faea0f51c8e4cbc6d1311516e to your computer and use it in GitHub Desktop.
build-depends: base >=4.9 && <4.10
, mtl >=2.2 && <2.3
, filepath >=1.4 && <1.5
, directory >=1.3 && <1.4
, text >=1.2 && <1.3
, containers >=0.5 && <0.6
, reflex-dom-core
, jsaddle
, jsaddle-warp
, warp
, wai-middleware-static
, websockets
-- with the appropriate ghci file to bring everything together
ghcid -T "runner testMe" --reload=css
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module Run (
runner
) where
import Control.Monad.Reader (ReaderT, runReaderT)
import Data.Foldable (traverse_)
import Reflex.Dom.Core
import Network.Wai.Handler.Warp (defaultSettings,
runSettings, setPort,
setTimeout)
import Network.WebSockets (defaultConnectionOptions)
import Language.Javascript.JSaddle.Run (syncPoint)
import Language.Javascript.JSaddle.WebSockets
import Network.Wai.Middleware.Static
import System.FilePath ((</>))
import System.Directory (listDirectory)
import qualified Data.Text as Text
import qualified Data.Map as Map
runner' ::
FilePath ->
Int ->
(forall x. Widget x ()) ->
IO ()
runner' cssPath port w =
do
cssFiles <- listDirectory $ "." </> cssPath
let
f = do
let
stylesheet s =
elAttr "link" (Map.fromList [("rel", "stylesheet"), ("href", s)]) $
return ()
mainWidgetWithHead
(traverse_ (\f -> stylesheet . Text.pack $ cssPath </> f) cssFiles)
w
serveFiles = staticPolicy $ hasPrefix cssPath
debugWrapper $ \refreshMiddleware registerContext -> do
app <- jsaddleOr
defaultConnectionOptions
(registerContext >> f >> syncPoint)
(refreshMiddleware jsaddleApp)
runSettings (setPort port (setTimeout 3600 defaultSettings)) $
serveFiles app
runner ::
(forall x. Widget x ())
-> IO ()
runner =
runner' "css" 8080
@nh2
Copy link

nh2 commented May 23, 2018

I had to replace jsaddleApp by jsaddleAppWithJs $ jsaddleJs True to make auto reload work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment