diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-14 01:19:36 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-14 01:19:36 +0100 |
commit | 53fcf55c02f9335518c28d26429913258fc28f87 (patch) | |
tree | bc6aa880814da485343ae595c916de5ae52fcfa0 | |
parent | d84b462a711ce95593ff05a7581e722562c3835a (diff) | |
download | bar-53fcf55c02f9335518c28d26429913258fc28f87.tar bar-53fcf55c02f9335518c28d26429913258fc28f87.tar.gz bar-53fcf55c02f9335518c28d26429913258fc28f87.tar.bz2 bar-53fcf55c02f9335518c28d26429913258fc28f87.tar.xz bar-53fcf55c02f9335518c28d26429913258fc28f87.zip |
Extract appRoot from http headers
-rw-r--r-- | Foundation.hs | 17 | ||||
-rw-r--r-- | Settings.hs | 4 | ||||
-rw-r--r-- | config/settings.yml | 11 |
3 files changed, 12 insertions, 20 deletions
diff --git a/Foundation.hs b/Foundation.hs index 85512a3..d192c08 100644 --- a/Foundation.hs +++ b/Foundation.hs | |||
@@ -1,12 +1,20 @@ | |||
1 | module Foundation where | 1 | module Foundation where |
2 | 2 | ||
3 | import Import.NoFoundation | 3 | import Import.NoFoundation hiding (requestHeaders) |
4 | import Database.Persist.Sql (ConnectionPool, runSqlPool) | 4 | import Database.Persist.Sql (ConnectionPool, runSqlPool) |
5 | import Text.Hamlet (hamletFile) | 5 | import Text.Hamlet (hamletFile) |
6 | 6 | ||
7 | import Yesod.Core.Types (Logger) | 7 | import Yesod.Core.Types (Logger) |
8 | import qualified Yesod.Core.Unsafe as Unsafe | 8 | import qualified Yesod.Core.Unsafe as Unsafe |
9 | 9 | ||
10 | import qualified Data.Text.Encoding as TE | ||
11 | import qualified Data.Text.Encoding.Error as TEE | ||
12 | |||
13 | import Data.Map.Lazy (Map) | ||
14 | import qualified Data.Map.Lazy as Map | ||
15 | |||
16 | import Network.Wai (requestHeaders) | ||
17 | |||
10 | -- | The foundation datatype for your application. This can be a good place to | 18 | -- | The foundation datatype for your application. This can be a good place to |
11 | -- keep settings and values requiring initialization before your application | 19 | -- keep settings and values requiring initialization before your application |
12 | -- starts running, such as database connections. Every handler will have | 20 | -- starts running, such as database connections. Every handler will have |
@@ -44,12 +52,7 @@ type Form x = Html -> MForm (HandlerT App IO) (FormResult x, Widget) | |||
44 | -- Please see the documentation for the Yesod typeclass. There are a number | 52 | -- Please see the documentation for the Yesod typeclass. There are a number |
45 | -- of settings which can be configured by overriding methods here. | 53 | -- of settings which can be configured by overriding methods here. |
46 | instance Yesod App where | 54 | instance Yesod App where |
47 | -- Controls the base of generated URLs. For more information on modifying, | 55 | approot = ApprootRequest $ \_ req -> maybe "" (TE.decodeUtf8With TEE.lenientDecode) $ Map.lookup "AppRoot" (Map.fromList $ requestHeaders req) |
48 | -- see: https://github.com/yesodweb/yesod/wiki/Overriding-approot | ||
49 | approot = ApprootRequest $ \app req -> | ||
50 | case appRoot $ appSettings app of | ||
51 | Nothing -> getApprootText guessApproot app req | ||
52 | Just root -> root | ||
53 | 56 | ||
54 | -- Store session data on the client in encrypted cookies, | 57 | -- Store session data on the client in encrypted cookies, |
55 | -- default session idle timeout is 120 minutes | 58 | -- default session idle timeout is 120 minutes |
diff --git a/Settings.hs b/Settings.hs index 76aa2f3..63cbd15 100644 --- a/Settings.hs +++ b/Settings.hs | |||
@@ -33,9 +33,6 @@ data AppSettings = AppSettings | |||
33 | -- ^ Directory from which to serve static files. | 33 | -- ^ Directory from which to serve static files. |
34 | , appDatabaseConf :: PostgresConf | 34 | , appDatabaseConf :: PostgresConf |
35 | -- ^ Configuration settings for accessing the database. | 35 | -- ^ Configuration settings for accessing the database. |
36 | , appRoot :: Maybe Text | ||
37 | -- ^ Base for all generated URLs. If @Nothing@, determined | ||
38 | -- from the request headers. | ||
39 | , appHost :: HostPreference | 36 | , appHost :: HostPreference |
40 | -- ^ Host/interface the server should bind to. | 37 | -- ^ Host/interface the server should bind to. |
41 | , appPort :: Int | 38 | , appPort :: Int |
@@ -57,7 +54,6 @@ instance FromJSON AppSettings where | |||
57 | let defaultDev = DEV_BOOL | 54 | let defaultDev = DEV_BOOL |
58 | appStaticDir <- o .: "static-dir" | 55 | appStaticDir <- o .: "static-dir" |
59 | appDatabaseConf <- o .: "database" | 56 | appDatabaseConf <- o .: "database" |
60 | appRoot <- o .:? "approot" | ||
61 | appHost <- fromString <$> o .: "host" | 57 | appHost <- fromString <$> o .: "host" |
62 | appPort <- o .: "port" | 58 | appPort <- o .: "port" |
63 | appIpFromHeader <- o .: "ip-from-header" | 59 | appIpFromHeader <- o .: "ip-from-header" |
diff --git a/config/settings.yml b/config/settings.yml index fcae60c..83d3bfc 100644 --- a/config/settings.yml +++ b/config/settings.yml | |||
@@ -1,15 +1,8 @@ | |||
1 | # Values formatted like "_env:ENV_VAR_NAME:default_value" can be overridden by the specified environment variable. | ||
2 | # See https://github.com/yesodweb/yesod/wiki/Configuration#overriding-configuration-values-with-environment-variables | ||
3 | |||
4 | static-dir: "_env:STATIC_DIR:static" | 1 | static-dir: "_env:STATIC_DIR:static" |
5 | host: "_env:HOST:*4" # any IPv4 host | 2 | host: "_env:HOST:*4" # any IPv4 host |
6 | port: "_env:PORT:3000" # NB: The port `yesod devel` uses is distinct from this value. Set the `yesod devel` port from the command line. | 3 | port: "_env:PORT:3000" |
7 | ip-from-header: "_env:IP_FROM_HEADER:false" | 4 | ip-from-header: "_env:IP_FROM_HEADER:false" |
8 | 5 | ||
9 | # Default behavior: determine the application root from the request headers. | ||
10 | # Uncomment to set an explicit approot | ||
11 | approot: "_env:APPROOT:" | ||
12 | |||
13 | # Optional values with the following production defaults. | 6 | # Optional values with the following production defaults. |
14 | # In development, they default to the inverse. | 7 | # In development, they default to the inverse. |
15 | # | 8 | # |
@@ -21,6 +14,6 @@ database: | |||
21 | user: "_env:PGUSER:bar" | 14 | user: "_env:PGUSER:bar" |
22 | password: "_env:PGPASS:" | 15 | password: "_env:PGPASS:" |
23 | host: "_env:PGHOST:" | 16 | host: "_env:PGHOST:" |
24 | port: "_env:PGPORT:" | 17 | port: "_env:PGPORT:5432" |
25 | database: "_env:PGDATABASE:bar" | 18 | database: "_env:PGDATABASE:bar" |
26 | poolsize: "_env:PGPOOLSIZE:10" | 19 | poolsize: "_env:PGPOOLSIZE:10" |