@@ -9,6 +9,7 @@
99{-# OPTIONS_GHC -fno-warn-orphans #-}
1010module Obelisk.Backend
1111 ( Backend (..)
12+ , StaticAssets (..)
1213 -- * Re-exports
1314 , Default (def)
1415 , getPageName
@@ -17,6 +18,7 @@ module Obelisk.Backend
1718 , serveDefaultObeliskApp
1819 , prettifyOutput
1920 , runBackend
21+ , runBackend'
2022 , staticRenderContentType
2123 , mkRouteToUrl
2224 , getPublicConfigs
@@ -45,7 +47,7 @@ import Obelisk.Snap.Extras (doNotCache, serveFileIfExistsAs)
4547import Reflex.Dom
4648import Snap (MonadSnap, Snap, commandLineConfig, defaultConfig, getsRequest, httpServe, modifyResponse
4749 , rqPathInfo, rqQueryString, setContentType, writeBS, writeText
48− , rqCookies, Cookie(..) , setHeader)
50+ , rqCookies, Cookie(..) , setHeader, setPort)
4951import Snap.Internal.Http.Server.Config (Config (accessLog, errorLog), ConfigLog (ConfigIoLog))
5052import System.IO (BufferMode (..), hSetBuffering, stderr, stdout)
5153
@@ -97,10 +99,10 @@ defaultFrontendGhcjsAssets = StaticAssets
9799 , _staticAssets_unprocessed = "frontend.jsexe"
98100 }
99101
100−runSnapWithCommandLineArgs :: MonadIO m => Snap () -> m ()
101−runSnapWithCommandLineArgs a = do
102+runSnapWithCommandLineArgs :: MonadIO m => Maybe Int -> Snap () -> m ()
103+runSnapWithCommandLineArgs mPort a = do
102104 -- Get the web server configuration from the command line
103− cmdLineConf <- liftIO $ commandLineConfig defaultConfig
105+ cmdLineConf <- liftIO $ commandLineConfig $ maybe id setPort mPort defaultConfig
104106 let httpConf = cmdLineConf
105107 { accessLog = Just $ ConfigIoLog BSC8.putStrLn
106108 , errorLog = Just $ ConfigIoLog BSC8.putStrLn
@@ -170,17 +172,25 @@ serveGhcjsApp urlEnc app config = \case
170172 GhcjsAppRoute_Resource :=> Identity pathSegments -> serveStaticAssets (_ghcjsApp_compiled app) pathSegments
171173
172174runBackend :: Backend backendRoute frontendRoute -> Frontend (R frontendRoute) -> IO ()
173−runBackend backend frontend = case checkEncoder $ _backend_routeEncoder backend of
175+runBackend = runBackend' Nothing defaultStaticAssets
176+
177+runBackend'
178+ :: Maybe Int
179+ -> StaticAssets
180+ -> Backend fullRoute frontendRoute
181+ -> Frontend (R frontendRoute)
182+ -> IO ()
183+runBackend' mPort staticAssets backend frontend = case checkEncoder $ _backend_routeEncoder backend of
174184 Left e -> fail $ "backend error:\n" <> T.unpack e
175185 Right validFullEncoder -> do
176186 publicConfigs <- getPublicConfigs
177187 _backend_run backend $ \serveRoute -> do
178− runSnapWithCommandLineArgs $ do
188+ runSnapWithCommandLineArgs mPort $ do
179189 getRouteWith validFullEncoder >>= \case
180190 Identity r -> case r of
181191 FullRoute_Backend backendRoute :/ a -> serveRoute $ backendRoute :/ a
182192 FullRoute_Frontend obeliskRoute :/ a ->
183− serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) (serveStaticAssets defaultStaticAssets) frontend publicConfigs $
193+ serveDefaultObeliskApp (mkRouteToUrl validFullEncoder) (serveStaticAssets staticAssets) frontend publicConfigs $
184194 obeliskRoute :/ a
185195
186196mkRouteToUrl :: Encoder Identity parse (R (FullRoute br fr)) PageName -> R fr -> Text