on master help sign in
Merge pull request #50 from tomsmalley/master
a5785ceHamish Mackenzie
jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+11−3
@@ -40,10 +40,11 @@ import Network.WebSockets
40 receiveDataMessage, acceptRequest, ServerApp, sendPing)
40 receiveDataMessage, acceptRequest, ServerApp, sendPing)
41import qualified Network.WebSockets as WS (DataMessage(..))
41import qualified Network.WebSockets as WS (DataMessage(..))
42import Network.Wai.Handler.WebSockets (websocketsOr)
42import Network.Wai.Handler.WebSockets (websocketsOr)
43+import Network.HTTP.Types (Status(..))
43
44
44import Language.Javascript.JSaddle.Types (JSM(..), JSContextRef(..))
45import Language.Javascript.JSaddle.Types (JSM(..), JSContextRef(..))
45import qualified Network.Wai as W
46import qualified Network.Wai as W
46− (responseLBS, requestMethod, pathInfo)
47+ (responseLBS, requestMethod, pathInfo, modifyResponse, responseStatus)
47import qualified Data.Text as T (pack)
48import qualified Data.Text as T (pack)
48import qualified Network.HTTP.Types as H
49import qualified Network.HTTP.Types as H
49 (status403, status200)
50 (status403, status200)
@@ -111,7 +112,11 @@ jsaddleOr opts entryPoint otherApp = do
111 Just handler -> do
112 Just handler -> do
112 next <- encode <$> handler result
113 next <- encode <$> handler result
113 sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/json")] next
114 sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/json")] next
114− _ -> otherApp req sendResponse
115+ (method, _) -> (catch404 otherApp) req sendResponse
116+ where catch404 = W.modifyResponse $ \resp ->
117+ case (method, W.responseStatus resp) of
118+ ("GET", Status 404 _) -> indexResponse
119+ _ -> resp
115 return $ websocketsOr opts wsApp syncHandler
120 return $ websocketsOr opts wsApp syncHandler
116
121
117
122
@@ -132,9 +137,12 @@ jsaddleWithAppOr opts entryPoint otherApp = jsaddleOr opts entryPoint $ \req sen
132jsaddleAppPartial :: Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
137jsaddleAppPartial :: Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
133jsaddleAppPartial = jsaddleAppPartialWithJs $ jsaddleJs False
138jsaddleAppPartial = jsaddleAppPartialWithJs $ jsaddleJs False
134
139
140+indexResponse :: Response
141+indexResponse = W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
142+
135jsaddleAppPartialWithJs :: ByteString -> Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
143jsaddleAppPartialWithJs :: ByteString -> Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
136jsaddleAppPartialWithJs js req sendResponse = case (W.requestMethod req, W.pathInfo req) of
144jsaddleAppPartialWithJs js req sendResponse = case (W.requestMethod req, W.pathInfo req) of
137− ("GET", []) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
145+ ("GET", []) -> Just $ sendResponse indexResponse
138 ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] js
146 ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] js
139 _ -> Nothing
147 _ -> Nothing
140
148
jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs+1−1
@@ -31,7 +31,7 @@ indexHtml =
31 \</head>\n\
31 \</head>\n\
32 \<body>\n\
32 \<body>\n\
33 \</body>\n\
33 \</body>\n\
34− \<script src=\"jsaddle.js\"></script>\n\
34+ \<script src=\"/jsaddle.js\"></script>\n\
35 \</html>"
35 \</html>"
36
36
37initState :: ByteString
37initState :: ByteString