From 1794e63bb0b494e9ae012f59b989f2d8bf7fd47d Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Mon, 2 Oct 2017 20:31:49 +1100 Subject: [PATCH] Restore the staticRoot option from older versions of Yesod's scaffolding, so I can host static files on a subdomain easily --- config/settings.yml | 5 ++--- src/Foundation.hs | 10 ++++++++++ src/Settings.hs | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config/settings.yml b/config/settings.yml index 226e363..87604c6 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -6,9 +6,8 @@ host: "_env:HOST:127.0.0.1" port: "_env:PORT:3000" # NB: The port `yesod devel` uses is distinct from this value. Set the `yesod devel` port from the command line. ip-from-header: "_env:IP_FROM_HEADER:true" -# Default behavior: determine the application root from the request headers. -# Uncomment to set an explicit approot -#approot: "_env:APPROOT:http://localhost:3000" +approot: "_env:APPROOT:https://00dani.dev" +static-root: "_env:STATIC_ROOT:https://static.00dani.dev" # Optional values with the following production defaults. # In development, they default to the inverse. diff --git a/src/Foundation.hs b/src/Foundation.hs index df01d55..4d767c1 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -77,6 +77,16 @@ instance Yesod App where 120 -- timeout in minutes "config/client_session_key.aes" + -- Redirect static requests to a subdomain - this is recommended for best + -- performance, since serving static files does not need your session + -- cookies and they can be served from the frontend HTTP server without + -- hitting the app server at all. + -- https://github.com/yesodweb/yesod-cookbook/blob/master/cookbook/Serve-static-files-from-a-separate-domain.md + urlParamRenderOverride app (StaticR s) _ = do + staticRoot <- appStaticRoot . appSettings $ app + return . uncurry (joinPath app staticRoot) . renderRoute $ s + urlParamRenderOverride _ _ _ = Nothing + -- Yesod Middleware allows you to run code before and after each handler function. -- The defaultYesodMiddleware adds the response header "Vary: Accept, Accept-Language" and performs authorization checks. -- Some users may also want to add the defaultCsrfMiddleware, which: diff --git a/src/Settings.hs b/src/Settings.hs index 901a8b1..110a8be 100644 --- a/src/Settings.hs +++ b/src/Settings.hs @@ -43,6 +43,10 @@ data AppSettings = AppSettings -- ^ Get the IP address from the header when logging. Useful when sitting -- behind a reverse proxy. + , appStaticRoot :: Maybe Text + -- ^ Base for static generated URLs. Useful for serving static assets from + -- a separate domain. If @Nothing@, use @appRoot@ and @appStaticDir@ instead. + , appDetailedRequestLogging :: Bool -- ^ Use detailed request logging system , appShouldLogAll :: Bool @@ -79,6 +83,8 @@ instance FromJSON AppSettings where appPort <- o .: "port" appIpFromHeader <- o .: "ip-from-header" + appStaticRoot <- o .:? "static-root" + appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev appShouldLogAll <- o .:? "should-log-all" .!= defaultDev appReloadTemplates <- o .:? "reload-templates" .!= defaultDev