Last active
July 12, 2017 06:30
-
-
Save nkpart/13b6749b82524ca48978b15165bcf6ea to your computer and use it in GitHub Desktop.
JSaddle plus static file serving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
Hacked Language.Javascript.JSaddle.Warp that also serves a static directory of files. a 404 on the static application | |
is handled by the hosted jsaddle app. | |
-} | |
{-# LANGUAGE CPP #-} | |
module Warpy ( | |
-- * Running JSM over WebSockets | |
runAndServe | |
#ifndef ghcjs_HOST_OS | |
, module Language.Javascript.JSaddle.WebSockets | |
#endif | |
) where | |
#ifndef ghcjs_HOST_OS | |
import Control.Applicative | |
import Network.Wai.Handler.Warp (defaultSettings, | |
runSettings, setPort, | |
setTimeout) | |
import Network.WebSockets (defaultConnectionOptions) | |
import Data.Maybe (fromMaybe) | |
import Language.Javascript.JSaddle.Run (syncPoint) | |
import Language.Javascript.JSaddle.Types (JSM) | |
import Language.Javascript.JSaddle.WebSockets | |
import Network.Wai.Application.Static | |
#endif | |
-- | Run the given 'JSM' action as the main entry point. Either directly | |
-- in GHCJS or as a Warp server on the given port on GHC. | |
#ifdef ghcjs_HOST_OS | |
runAndServe :: Int -> FilePath -> IO () -> IO () | |
runAndServe _port _thingo = id | |
#else | |
runAndServe :: Int -> FilePath -> JSM () -> IO () | |
runAndServe port staticDir f = | |
do reflexApp <- jsaddleOr defaultConnectionOptions (f >> syncPoint) (jsaddleElseStatic staticDir) | |
runSettings (setPort port (setTimeout 3600 defaultSettings)) reflexApp | |
jsaddleElseStatic staticDir req sendResponse = | |
fromMaybe | |
(staticApp (defaultFileServerSettings staticDir) req sendResponse) | |
(jsaddleAppPartial req sendResponse) | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment