web: production build now has all web content embedded, dev build uses ./static (#63)
This commit is contained in:
		
							parent
							
								
									c7c67efc1c
								
							
						
					
					
						commit
						4c52dd5b76
					
				| @ -1 +0,0 @@ | |||||||
| 0.15.3 |  | ||||||
| @ -5,7 +5,6 @@ Re-export the modules of the hledger-web program. | |||||||
| module Hledger.Web ( | module Hledger.Web ( | ||||||
|                      module Hledger.Web.App, |                      module Hledger.Web.App, | ||||||
|                      module Hledger.Web.AppRun, |                      module Hledger.Web.AppRun, | ||||||
|                      module Hledger.Web.EmbeddedFiles, |  | ||||||
|                      module Hledger.Web.Handlers, |                      module Hledger.Web.Handlers, | ||||||
|                      module Hledger.Web.Options, |                      module Hledger.Web.Options, | ||||||
|                      module Hledger.Web.Settings, |                      module Hledger.Web.Settings, | ||||||
| @ -17,7 +16,6 @@ import Test.HUnit | |||||||
| 
 | 
 | ||||||
| import Hledger.Web.App | import Hledger.Web.App | ||||||
| import Hledger.Web.AppRun | import Hledger.Web.AppRun | ||||||
| import Hledger.Web.EmbeddedFiles |  | ||||||
| import Hledger.Web.Handlers | import Hledger.Web.Handlers | ||||||
| import Hledger.Web.Options | import Hledger.Web.Options | ||||||
| import Hledger.Web.Settings | import Hledger.Web.Settings | ||||||
|  | |||||||
| @ -12,12 +12,10 @@ where | |||||||
| import Data.Dynamic (Dynamic, toDyn) | import Data.Dynamic (Dynamic, toDyn) | ||||||
| import Network.Wai (Application) | import Network.Wai (Application) | ||||||
| import Network.Wai.Middleware.Debug (debugHandle) | import Network.Wai.Middleware.Debug (debugHandle) | ||||||
| import System.IO.Storage (withStore, putValue) |  | ||||||
| import Yesod.Logger (makeLogger, flushLogger, Logger, logLazyText, logString) | import Yesod.Logger (makeLogger, flushLogger, Logger, logLazyText, logString) | ||||||
| import Yesod.Static | import Yesod.Static | ||||||
| 
 | 
 | ||||||
| import Hledger | import Hledger | ||||||
| import Hledger.Cli |  | ||||||
| import Hledger.Web.App | import Hledger.Web.App | ||||||
| import Hledger.Web.Handlers | import Hledger.Web.Handlers | ||||||
| import Hledger.Web.Options | import Hledger.Web.Options | ||||||
| @ -28,9 +26,6 @@ import Hledger.Web.Settings | |||||||
| -- the comments there for more details. | -- the comments there for more details. | ||||||
| mkYesodDispatch "App" resourcesApp | mkYesodDispatch "App" resourcesApp | ||||||
| 
 | 
 | ||||||
| -- withApp :: App -> (Application -> IO a) -> IO a |  | ||||||
| -- withApp a f = toWaiApp a >>= f |  | ||||||
| 
 |  | ||||||
| -- This function allocates resources (such as a database connection pool), | -- This function allocates resources (such as a database connection pool), | ||||||
| -- performs initialization and creates a WAI application. This is also the | -- performs initialization and creates a WAI application. This is also the | ||||||
| -- place to put your migrate statements to have automatic database | -- place to put your migrate statements to have automatic database | ||||||
| @ -38,30 +33,20 @@ mkYesodDispatch "App" resourcesApp | |||||||
| withApp :: AppConfig -> Logger -> (Application -> IO a) -> IO a | withApp :: AppConfig -> Logger -> (Application -> IO a) -> IO a | ||||||
| withApp conf logger f = do | withApp conf logger f = do | ||||||
| #ifdef PRODUCTION | #ifdef PRODUCTION | ||||||
|     s <- static Hledger.Web.Settings.staticDir |     putStrLn $ "Production mode, using embedded web files" | ||||||
|  |     let s = $(embed Hledger.Web.Settings.staticDir) | ||||||
| #else | #else | ||||||
|  |     putStrLn $ "Not in production mode, using web files from " ++ Hledger.Web.Settings.staticDir ++ "/" | ||||||
|     s <- staticDevel Hledger.Web.Settings.staticDir |     s <- staticDevel Hledger.Web.Settings.staticDir | ||||||
| #endif | #endif | ||||||
|     let h = App {settings=conf |     let a = App {settings=conf | ||||||
|                 ,getLogger=logger |                 ,getLogger=logger | ||||||
|                 ,getStatic=s |                 ,getStatic=s | ||||||
|                 ,appOpts=defwebopts |                 ,appOpts=defwebopts | ||||||
|                 ,appArgs=[] |                 ,appArgs=[] | ||||||
|                 ,appJournal=nulljournal |                 ,appJournal=nulljournal | ||||||
|               } |                 } | ||||||
|     toWaiApp h >>= f |     toWaiApp a >>= f | ||||||
| 
 |  | ||||||
| -- withDevelApp :: Dynamic |  | ||||||
| -- withDevelApp = do |  | ||||||
| --   s <- static Hledger.Web.Settings.staticdir |  | ||||||
| --   let a = App{ |  | ||||||
| --               getStatic=s |  | ||||||
| --              ,appRoot=Hledger.Web.Settings.defapproot |  | ||||||
| --              ,appOpts=defwebopts |  | ||||||
| --              ,appArgs=[] |  | ||||||
| --              ,appJournal=nulljournal |  | ||||||
| --              } |  | ||||||
| --   return $ toDyn (withApp a :: (Application -> IO ()) -> IO ()) |  | ||||||
| 
 | 
 | ||||||
| -- for yesod devel | -- for yesod devel | ||||||
| withDevelAppPort :: Dynamic | withDevelAppPort :: Dynamic | ||||||
| @ -78,21 +63,3 @@ withDevelAppPort = | |||||||
|         flushLogger logger |         flushLogger logger | ||||||
|       where |       where | ||||||
|         logHandle logger msg = logLazyText logger msg >> flushLogger logger |         logHandle logger msg = logLazyText logger msg >> flushLogger logger | ||||||
| 
 |  | ||||||
| -- -- Called by wai-handler-devel. |  | ||||||
| -- -- Eg: cabal-dev/bin/wai-handler-devel 5001 AppRun withWaiHandlerDevelApp |  | ||||||
| -- withWaiHandlerDevelApp :: (Application -> IO ()) -> IO () |  | ||||||
| -- withWaiHandlerDevelApp func = do |  | ||||||
| --   let f = "./test.journal" |  | ||||||
| --   ej <- readJournalFile Nothing f |  | ||||||
| --   let Right j = ej |  | ||||||
| --   let a = App{ |  | ||||||
| --               getStatic=static Hledger.Web.Settings.staticdir |  | ||||||
| --              ,appRoot="http://localhost:5002" |  | ||||||
| --              ,appOpts=defwebopts{cliopts_=defcliopts{file_=Just f}} |  | ||||||
| --              ,appArgs=[] |  | ||||||
| --              ,appJournal=j |  | ||||||
| --              } |  | ||||||
| --   withStore "hledger" $ do |  | ||||||
| --     putValue "hledger" "journal" j |  | ||||||
| --     withApp a func |  | ||||||
|  | |||||||
| @ -1,46 +0,0 @@ | |||||||
| {-# LANGUAGE TemplateHaskell #-} |  | ||||||
| {-|  |  | ||||||
| 
 |  | ||||||
| Support files (static files and templates) used by the web app are |  | ||||||
| embedded in this module at compile time. Since hamlet can not easily use |  | ||||||
| these directly, we provide a way to write them out to the filesystem at |  | ||||||
| startup, when needed. This simplifies installation for end-users, and |  | ||||||
| customisation too. |  | ||||||
| 
 |  | ||||||
| -} |  | ||||||
| module Hledger.Web.EmbeddedFiles |  | ||||||
|     ( |  | ||||||
|      files |  | ||||||
|     ,createFilesIfMissing |  | ||||||
|     ) |  | ||||||
| where |  | ||||||
| import Control.Monad |  | ||||||
| import qualified Data.ByteString as B |  | ||||||
| import Data.FileEmbed (embedDir) |  | ||||||
| import System.Directory |  | ||||||
| import System.FilePath |  | ||||||
| 
 |  | ||||||
| import Hledger.Web.Settings (datadir) |  | ||||||
| 
 |  | ||||||
| -- | An embedded copy of all files below the the hledger-web data |  | ||||||
| -- directory (@.hledger\/web\/@) at compile time, as (FilePath,ByteString) |  | ||||||
| -- pairs. |  | ||||||
| files :: [(FilePath, B.ByteString)] |  | ||||||
| files = $(embedDir datadir) |  | ||||||
| 
 |  | ||||||
| -- | If the hledger-web data directory (@.hledger\/web\/@) does not exist in |  | ||||||
| -- the current directory, create and fill it with the web app support |  | ||||||
| -- files (templates, stylesheets, images etc.) Returns True if the |  | ||||||
| -- directory was missing. |  | ||||||
| createFilesIfMissing :: IO Bool |  | ||||||
| createFilesIfMissing = do |  | ||||||
|   exists <- doesDirectoryExist datadir |  | ||||||
|   if exists |  | ||||||
|    then return False |  | ||||||
|    else do |  | ||||||
|      createDirectoryIfMissing True datadir   |  | ||||||
|      setCurrentDirectory datadir |  | ||||||
|      forM_ files $ \(f,d) -> do |  | ||||||
|                               createDirectoryIfMissing True $ takeDirectory f |  | ||||||
|                               B.writeFile f d |  | ||||||
|      return True |  | ||||||
| @ -22,7 +22,7 @@ module Hledger.Web.Settings | |||||||
|     , defbaseurl |     , defbaseurl | ||||||
|     , hledgerorgurl |     , hledgerorgurl | ||||||
|     , manualurl |     , manualurl | ||||||
|     , datadir |     -- , datadir | ||||||
| 
 | 
 | ||||||
|     ) where |     ) where | ||||||
| 
 | 
 | ||||||
| @ -109,7 +109,8 @@ staticDir :: FilePath | |||||||
| staticDir = datadir++"static" | staticDir = datadir++"static" | ||||||
| 
 | 
 | ||||||
| datadir :: FilePath | datadir :: FilePath | ||||||
| datadir = "./.hledger/web/" | datadir = "./" | ||||||
|  | -- datadir = "./.hledger/web/" | ||||||
| 
 | 
 | ||||||
| -- | The base URL for your static files. As you can see by the default | -- | The base URL for your static files. As you can see by the default | ||||||
| -- value, this can simply be "static" appended to your application root. | -- value, this can simply be "static" appended to your application root. | ||||||
|  | |||||||
| @ -22,7 +22,6 @@ cabal-version:  >= 1.6 | |||||||
| build-type:     Simple | build-type:     Simple | ||||||
| extra-tmp-files: | extra-tmp-files: | ||||||
| extra-source-files: | extra-source-files: | ||||||
| -- included via hamletFile/EmbeddedFiles.hs since yesod needs the paths at compile-time |  | ||||||
| -- data-dir:     | -- data-dir:     | ||||||
| -- data-files:   | -- data-files:   | ||||||
| 
 | 
 | ||||||
| @ -51,7 +50,6 @@ executable hledger-web | |||||||
|                      Hledger.Web |                      Hledger.Web | ||||||
|                      Hledger.Web.App |                      Hledger.Web.App | ||||||
|                      Hledger.Web.AppRun |                      Hledger.Web.AppRun | ||||||
|                      Hledger.Web.EmbeddedFiles |  | ||||||
|                      Hledger.Web.Options |                      Hledger.Web.Options | ||||||
|                      Hledger.Web.Settings |                      Hledger.Web.Settings | ||||||
|                      Hledger.Web.StaticFiles |                      Hledger.Web.StaticFiles | ||||||
| @ -105,7 +103,6 @@ library | |||||||
|     other-modules: |     other-modules: | ||||||
|                      Hledger.Web |                      Hledger.Web | ||||||
|                      Hledger.Web.App |                      Hledger.Web.App | ||||||
|                      Hledger.Web.EmbeddedFiles |  | ||||||
|                      Hledger.Web.Options |                      Hledger.Web.Options | ||||||
|                      Hledger.Web.Settings |                      Hledger.Web.Settings | ||||||
|                      Hledger.Web.StaticFiles |                      Hledger.Web.StaticFiles | ||||||
|  | |||||||
| @ -54,17 +54,10 @@ withJournalDo' opts cmd = do | |||||||
| -- | The web command. | -- | The web command. | ||||||
| web :: WebOpts -> Journal -> IO () | web :: WebOpts -> Journal -> IO () | ||||||
| web opts j = do | web opts j = do | ||||||
|   created <- createFilesIfMissing |   putStrLn $ "Running self-tests..." | ||||||
|   if created |   runTestsOrExit $ cliopts_ opts | ||||||
|    then do |   -- unless (debug_ $ cliopts_ opts) $ forkIO (browser baseurl) >> return () | ||||||
|      putStrLn $ "Installing support files in "++datadir++" - done, please run again." |   server (base_url_ opts) (port_ opts) opts j | ||||||
|      exitFailure |  | ||||||
|    else do |  | ||||||
|      putStrLn $ "Running self-tests..." |  | ||||||
|      runTestsOrExit $ cliopts_ opts |  | ||||||
|      putStrLn $ "Using support files in "++datadir |  | ||||||
|      -- unless (debug_ $ cliopts_ opts) $ forkIO (browser baseurl) >> return () |  | ||||||
|      server (base_url_ opts) (port_ opts) opts j |  | ||||||
| 
 | 
 | ||||||
| -- browser :: String -> IO () | -- browser :: String -> IO () | ||||||
| -- browser baseurl = do | -- browser baseurl = do | ||||||
|  | |||||||
| Before Width: | Height: | Size: 309 B After Width: | Height: | Size: 309 B | 
| Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB | 
		Loading…
	
		Reference in New Issue
	
	Block a user