on master help sign in
obelisk-backend: Support head in cfg and begin using obelisk-snap
commitecd567b6751e63d6bb26e8dfbb8a05e49fcec7cf authorLuigy Leon <luigy@outlook.com> dateSat 19 May 2018, 19:09 (8 years ago) parent8929091
6 files changed+25−8
lib/backend/obelisk-backend.cabal+6−3 lib/backend/src/Obelisk/Backend.hs+12−4 lib/snap/src/Obelisk/Snap.hs+1−1 skeleton/backend/backend.cabal+2 skeleton/backend/frontendJs/frontend.jsexe+1 skeleton/backend/src/Backend.hs+3
lib/backend/obelisk-backend.cabal+6−3
@@ -6,9 +6,12 @@ build-type: Simple
66library
77 hs-source-dirs: src
88 build-depends: base,
9− snap,
10− snap-server,
9+ bytestring,
1110 data-default,
11+ lens,
1212 obelisk-asset-serve-snap,
13− bytestring
13+ obelisk-snap,
14+ reflex-dom,
15+ snap,
16+ snap-server
1417 exposed-modules: Obelisk.Backend
lib/backend/src/Obelisk/Backend.hs+12−4
@@ -1,14 +1,17 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Obelisk.Backend
33 ( backend
4− , BackendConfig
4+ , BackendConfig (..)
55 -- * Re-exports
66 , Default (def)
77 ) where
88
9+import Control.Lens
910import qualified Data.ByteString.Char8 as BSC8
1011import Data.Default (Default (..))
1112import Obelisk.Asset.Serve.Snap (serveAssets)
13+import Obelisk.Snap
14+import Reflex.Dom
1215import System.IO (hSetBuffering, stderr, BufferMode (..))
1316import Snap (httpServe, defaultConfig, commandLineConfig, route)
1417import Snap.Internal.Http.Server.Config (Config (accessLog, errorLog), ConfigLog (ConfigIoLog))
@@ -17,25 +20,30 @@ import Snap.Internal.Http.Server.Config (Config (accessLog, errorLog), ConfigLog
1720-- | Configure the operation of the Obelisk backend. For reasonable defaults,
1821-- use 'def'.
1922data BackendConfig = BackendConfig
23+ { _backendConfig_head :: StaticWidget () ()
24+ }
2025
2126instance Default BackendConfig where
22− def = BackendConfig
27+ def = BackendConfig (return ())
2328
2429-- | Start an Obelisk backend
2530backend :: BackendConfig -> IO ()
26−backend _ = do
31+backend cfg = do
2732 -- Make output more legible by decreasing the likelihood of output from
2833 -- multiple threads being interleaved
2934 hSetBuffering stderr LineBuffering
3035
3136 -- Get the web server configuration from the command line
3237 cmdLineConf <- commandLineConfig defaultConfig
38+ headHtml <- fmap snd $ renderStatic $ _backendConfig_head cfg
3339 let httpConf = cmdLineConf
3440 { accessLog = Just $ ConfigIoLog BSC8.putStrLn
3541 , errorLog = Just $ ConfigIoLog BSC8.putStrLn
3642 }
43+ appCfg = def & appConfig_initialHead .~ headHtml
3744 -- Start the web server
3845 httpServe httpConf $ route
39− [ ("", serveAssets "frontend.jsexe.assets" "frontend.jsexe") --TODO: Can we prevent naming conflicts between frontend.jsexe and static?
46+ [ ("", serveApp "" appCfg)
47+ , ("", serveAssets "frontend.jsexe.assets" "frontend.jsexe") --TODO: Can we prevent naming conflicts between frontend.jsexe and static?
4048 , ("", serveAssets "static.assets" "static")
4149 ]
lib/snap/src/Obelisk/Snap.hs+1−1
@@ -81,7 +81,7 @@ serveStaticIndex cfg = do
8181 toHtmlRaw $ _appConfig_initialHead cfg
8282 body_ $ do -- <body>
8383 toHtmlRaw initialBody -- HtmlT monad wrapper
84− script_ [type_ "text/javascript", src_ (maybe "/all.js" T.pack appJsPath), defer_ "defer"] ("" :: String)
84+ script_ [type_ "application/javascript", src_ (maybe "/all.js" T.pack appJsPath), defer_ "defer"] ("" :: String)
8585
8686{- | Takes an AppConfig monad and uses it to generate & write Lazy ByteString
8787 to the body of the https response. This one comes with hard coded values
skeleton/backend/backend.cabal+2
@@ -9,6 +9,7 @@ library
99 buildable: False
1010 build-depends: base
1111 , common
12+ , frontend
1213 , obelisk-backend
1314 exposed-modules:
1415 Backend
@@ -22,6 +23,7 @@ executable backend
2223 build-depends: base
2324 , backend
2425 , common
26+ , frontend
2527 , lens
2628 , modern-uri
2729 , obelisk-backend
skeleton/backend/frontendJs/frontend.jsexenew file+1
@@ -0,0 +1 @@
1+../../frontend-js/bin/frontend.jsexe
skeleton/backend/src/Backend.hs+3
@@ -1,7 +1,10 @@
11module Backend where
22
33import Common.Api
4+import Frontend
45import qualified Obelisk.Backend as Ob
56
67backend :: IO ()
78backend = Ob.backend Ob.def
9+ { Ob._backendConfig_head = fst frontend
10+ }