Merge pull request #150 from xiaoruoruo/staticroot

web: add a --static-root option to customize static files root
This commit is contained in:
Simon Michael 2013-12-15 18:23:23 -08:00
commit 014838bb67
4 changed files with 22 additions and 11 deletions

View File

@ -19,7 +19,7 @@ import Network.HTTP.Conduit (Manager)
-- import qualified Settings -- import qualified Settings
import Settings.Development (development) import Settings.Development (development)
import Settings.StaticFiles import Settings.StaticFiles
import Settings (widgetFile, Extra (..)) import Settings (staticRoot, widgetFile, Extra (..))
#ifndef DEVELOPMENT #ifndef DEVELOPMENT
import Settings (staticDir) import Settings (staticDir)
import Text.Jasmine (minifym) import Text.Jasmine (minifym)
@ -117,11 +117,11 @@ instance Yesod App where
hamletToRepHtml $(hamletFile "templates/default-layout-wrapper.hamlet") hamletToRepHtml $(hamletFile "templates/default-layout-wrapper.hamlet")
-- -- This is done to provide an optimization for serving static files from -- This is done to provide an optimization for serving static files from
-- -- a separate domain. Please see the staticRoot setting in Settings.hs -- a separate domain. Please see the staticRoot setting in Settings.hs
-- urlRenderOverride y (StaticR s) = urlRenderOverride y (StaticR s) =
-- Just $ uncurry (joinPath y (Settings.staticRoot $ settings y)) $ renderRoute s Just $ uncurry (joinPath y (Settings.staticRoot $ settings y)) $ renderRoute s
-- urlRenderOverride _ _ = Nothing urlRenderOverride _ _ = Nothing
#ifndef DEVELOPMENT #ifndef DEVELOPMENT
-- This function creates static content files in the static folder -- This function creates static content files in the static folder

View File

@ -20,6 +20,7 @@ import Network.Wai.Handler.Warp (runSettings, defaultSettings, settingsPort)
import Network.Wai.Handler.Launch (runUrlPort) import Network.Wai.Handler.Launch (runUrlPort)
-- --
import Prelude hiding (putStrLn) import Prelude hiding (putStrLn)
import Control.Applicative ((<$>))
import Control.Monad (when) import Control.Monad (when)
import Data.Text (pack) import Data.Text (pack)
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
@ -59,12 +60,13 @@ web opts j = do
let j' = filterJournalTransactions (queryFromOpts d $ reportopts_ $ cliopts_ opts) j let j' = filterJournalTransactions (queryFromOpts d $ reportopts_ $ cliopts_ opts) j
p = port_ opts p = port_ opts
u = base_url_ opts u = base_url_ opts
staticRoot = pack <$> static_root_ opts
_ <- printf "Starting web app on port %d with base url %s\n" p u _ <- printf "Starting web app on port %d with base url %s\n" p u
app <- makeApplication opts j' AppConfig{appEnv = Development app <- makeApplication opts j' AppConfig{appEnv = Development
,appPort = p ,appPort = p
,appRoot = pack u ,appRoot = pack u
,appHost = HostIPv4 ,appHost = HostIPv4
,appExtra = Extra "" Nothing ,appExtra = Extra "" Nothing staticRoot
} }
if server_ opts if server_ opts
then do then do

View File

@ -1,6 +1,7 @@
module Hledger.Web.Options module Hledger.Web.Options
where where
import Prelude import Prelude
import Control.Applicative ((<$>))
import Data.Maybe import Data.Maybe
import System.Console.CmdArgs import System.Console.CmdArgs
import System.Console.CmdArgs.Explicit import System.Console.CmdArgs.Explicit
@ -26,6 +27,7 @@ webflags = [
flagNone ["server"] (setboolopt "server") ("log requests, don't auto-exit") flagNone ["server"] (setboolopt "server") ("log requests, don't auto-exit")
,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "URL" ("set the base url (default: "++defbaseurlexample++")") ,flagReq ["base-url"] (\s opts -> Right $ setopt "base-url" s opts) "URL" ("set the base url (default: "++defbaseurlexample++")")
,flagReq ["port"] (\s opts -> Right $ setopt "port" s opts) "PORT" ("listen on this tcp port (default: "++show defport++")") ,flagReq ["port"] (\s opts -> Right $ setopt "port" s opts) "PORT" ("listen on this tcp port (default: "++show defport++")")
,flagReq ["static-root"] (\s opts -> Right $ setopt "static-root" s opts) "Static Root" ("The root url from which the static files will be loaded (default: BASE_URL/static)")
] ]
webmode :: Mode [([Char], [Char])] webmode :: Mode [([Char], [Char])]
@ -47,6 +49,7 @@ data WebOpts = WebOpts {
server_ :: Bool server_ :: Bool
,base_url_ :: String ,base_url_ :: String
,port_ :: Int ,port_ :: Int
,static_root_ :: Maybe String
,cliopts_ :: CliOpts ,cliopts_ :: CliOpts
} deriving (Show) } deriving (Show)
@ -56,6 +59,7 @@ defwebopts = WebOpts
def def
def def
def def
def
-- instance Default WebOpts where def = defwebopts -- instance Default WebOpts where def = defwebopts
@ -67,6 +71,7 @@ toWebOpts rawopts = do
port_ = p port_ = p
,server_ = boolopt "server" rawopts ,server_ = boolopt "server" rawopts
,base_url_ = maybe (defbaseurl p) stripTrailingSlash $ maybestringopt "base-url" rawopts ,base_url_ = maybe (defbaseurl p) stripTrailingSlash $ maybestringopt "base-url" rawopts
,static_root_ = stripTrailingSlash <$> maybestringopt "static-root" rawopts
,cliopts_ = cliopts ,cliopts_ = cliopts
} }
where where

View File

@ -54,8 +54,10 @@ staticDir = "static"
-- have to make a corresponding change here. -- have to make a corresponding change here.
-- --
-- To see how this value is used, see urlRenderOverride in Foundation.hs -- To see how this value is used, see urlRenderOverride in Foundation.hs
staticRoot :: AppConfig DefaultEnv x -> Text staticRoot :: AppConfig DefaultEnv Extra -> Text
staticRoot conf = [st|#{appRoot conf}/static|] staticRoot conf = case extraStaticRoot $ appExtra conf of
Just root -> root
Nothing -> [st|#{appRoot conf}/static|]
-- | Settings for 'widgetFile', such as which template languages to support and -- | Settings for 'widgetFile', such as which template languages to support and
-- default Hamlet settings. -- default Hamlet settings.
@ -77,9 +79,11 @@ widgetFile = (if development then widgetFileReload
data Extra = Extra data Extra = Extra
{ extraCopyright :: Text { extraCopyright :: Text
, extraAnalytics :: Maybe Text -- ^ Google Analytics , extraAnalytics :: Maybe Text -- ^ Google Analytics
, extraStaticRoot :: Maybe Text
} deriving Show } deriving Show
parseExtra :: DefaultEnv -> Object -> Parser Extra parseExtra :: DefaultEnv -> Object -> Parser Extra
parseExtra _ o = Extra parseExtra _ o = Extra
<$> o .: "copyright" <$> o .: "copyright"
<*> o .:? "analytics" <*> o .:? "analytics"
<*> o .:? "staticRoot"