Last active
October 4, 2020 19:38
-
-
Save konn/3a94165772886f8ee2fd78fd5af835b7 to your computer and use it in GitHub Desktop.
JSaddle (Miso) and custom Backend Server, running on the same host & port
This file contains hidden or 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
{-# LANGUAGE DuplicateRecordFields, OverloadedStrings, RecordWildCards #-} | |
module JSaddleCompat (runApp, App(..)) where | |
import qualified Language.Javascript.JSaddle.Warp as JSaddle | |
import Miso (startApp, syncPoint) | |
import qualified Miso as Miso | |
import Network.HTTP.Client.Conduit (Manager, newManager) | |
import Network.HTTP.ReverseProxy (WaiProxyResponse (..), | |
defaultOnExc, waiProxyTo) | |
import Network.Wai (Application, pathInfo) | |
import qualified Network.Wai.Handler.Warp as Warp | |
import Network.Wai.Handler.WebSockets (isWebSocketsReq) | |
import Network.Wai.Middleware.Rewrite (rewritePureWithQueries) | |
import Network.WebSockets (defaultConnectionOptions) | |
runApp :: Eq state => Application -> App state action -> IO () | |
runApp backend misoApp = do | |
jSaddle <- JSaddle.jsaddleOr defaultConnectionOptions | |
(startApp misoApp >> syncPoint) | |
$ JSaddle.jsaddleApp | |
man <- newManager | |
Warp.runSettings | |
(Warp.setPort 9292 (Warp.setTimeout 3600 Warp.defaultSettings)) | |
$ router WaiApps | |
{ jsaddle = jSaddle | |
, backend = backend | |
, manager = man | |
} | |
router :: WaiApps -> Application | |
router WaiApps{..} = waiProxyTo route defaultOnExc manager | |
where | |
route req = pure $ | |
case (pathInfo req) of | |
[] | isWebSocketsReq req -> WPRApplication jsaddle | |
-- jsaddle maintains dedicated websock on / | |
| otherwise -> WPRApplication backend | |
-- otherwise we can just serve index.html from the backend. | |
-- we can serve genuine HTML containing CDN links etc from the backend! | |
("ws" : _) -> WPRApplication backend -- if backend server runs some WebSock on /ws | |
["static", "all.js"] -> WPRApplication | |
$ rewritePureWithQueries (\(_, qry) _ -> (["jsaddle.js"], qry)) jsaddle | |
-- jsaddle-warp always serves js on /jsaddle.js | |
-- we have to redirect ghcjs script link(s) to it. | |
("static": _) -> WPRApplication backend | |
-- serves static files from the backend (or wherever) | |
_ -> WPRApplication jsaddle | |
-- delegate default behaviour to jsaddle (as you like) | |
data WaiApps = | |
WaiApps | |
{ jsaddle :: Application | |
, backend :: Application | |
, manager :: Manager | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Advantages of this approach:
<script>
tags.