{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Yesod.Core.Dispatch
(
parseRoutes
, parseRoutesNoCheck
, parseRoutesFile
, parseRoutesFileNoCheck
, mkYesod
, mkYesodOpts
, mkYesodWith
, mkYesodData
, mkYesodDataOpts
, mkYesodSubData
, mkYesodSubDataOpts
, mkYesodDispatch
, mkYesodDispatchOpts
, mkYesodSubDispatch
, mkYesodSubDispatchInstance
, mkYesodSubDispatchInstanceOpts
, mkNestedSubDispatchInstance
, TyArgs (..)
, RouteOpts
, defaultOpts
, setEqDerived
, setShowDerived
, setReadDerived
, setCreateResources
, setFocusOnNestedRoute
, unsetFocusOnNestedRoute
, setParameterizedSubroute
, setNestedRouteFallthrough
, defaultGen
, getGetMaxExpires
, PathPiece (..)
, PathMultiPiece (..)
, Texts
, toWaiApp
, toWaiAppPlain
, UrlToDispatch(..)
, mkYesodRunnerEnv
, toWaiAppPlainNested
, toWaiAppYre
, toWaiAppYreNested
, warp
, warpDebug
, warpEnv
, mkDefaultMiddlewares
, defaultMiddlewaresNoLogging
, YesodSubDispatchNested (..)
, WaiSubsite (..)
, WaiSubsiteWithAuth (..)
) where
import Prelude hiding (exp)
import Yesod.Core.Internal.TH
import Language.Haskell.TH.Syntax (qLocation)
import Data.Proxy
import Yesod.Routes.Class
import Web.PathPieces
import qualified Network.Wai as W
import Data.ByteString.Lazy.Char8 ()
import Data.Bits ((.|.), finiteBitSize, shiftL)
import Data.Text (Text)
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
#if !MIN_VERSION_wai_extra(3,1,14)
import Data.Default (def)
#endif
import Yesod.Routes.Parse
import Yesod.Routes.TH (TyArgs (..))
import Yesod.Core.Types
import Yesod.Core.Class.Yesod
import Yesod.Core.Class.Dispatch
import Text.Read (readMaybe)
import System.Environment (getEnvironment)
import System.Entropy (getEntropy)
import Control.AutoUpdate (mkAutoUpdate, defaultUpdateSettings, updateAction, updateFreq)
import Yesod.Core.Internal.Util (getCurrentMaxExpiresRFC1123)
import Yesod.Core.Class.Dispatch.ToParentRoute
import Network.Wai.Middleware.Autohead
import Network.Wai.Middleware.AcceptOverride
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.Gzip (
gzip,
#if MIN_VERSION_wai_extra(3,1,14)
defaultGzipSettings,
#endif
)
import Network.Wai.Middleware.MethodOverride
import qualified Network.Wai.Handler.Warp
import System.Log.FastLogger
import Control.Monad.Logger
import Control.Monad (when)
import qualified Paths_yesod_core
import Data.Version (showVersion)
toWaiAppPlain :: YesodDispatch site => site -> IO W.Application
toWaiAppPlain :: forall site. YesodDispatch site => site -> IO Application
toWaiAppPlain site
site = do
yre <- site -> IO (YesodRunnerEnv site)
forall site. Yesod site => site -> IO (YesodRunnerEnv site)
mkYesodRunnerEnv site
site
return $ toWaiAppYre yre
mkYesodRunnerEnv :: Yesod site => site -> IO (YesodRunnerEnv site)
mkYesodRunnerEnv :: forall site. Yesod site => site -> IO (YesodRunnerEnv site)
mkYesodRunnerEnv site
site = do
logger <- site -> IO Logger
forall site. Yesod site => site -> IO Logger
makeLogger site
site
sb <- makeSessionBackend site
getMaxExpires <- getGetMaxExpires
pure YesodRunnerEnv
{ yreLogger = logger
, yreSite = site
, yreSessionBackend = sb
, yreGen = defaultGen
, yreGetMaxExpires = getMaxExpires
}
toWaiAppPlainNested
:: ( site ~ ParentSite route
, Yesod site
, YesodDispatchNested route
, ToParentRoute route
)
=> Proxy route
-> ParentArgs route
-> site
-> IO W.Application
toWaiAppPlainNested :: forall site route.
(site ~ ParentSite route, Yesod site, YesodDispatchNested route,
ToParentRoute route) =>
Proxy route -> ParentArgs route -> site -> IO Application
toWaiAppPlainNested Proxy route
proxy ParentArgs route
parentArgs site
site = do
yre <- site -> IO (YesodRunnerEnv site)
forall site. Yesod site => site -> IO (YesodRunnerEnv site)
mkYesodRunnerEnv site
site
return $ toWaiAppYreNested proxy parentArgs yre
defaultGen :: IO Int
defaultGen :: IO Int
defaultGen = ByteString -> Int
bsToInt (ByteString -> Int) -> IO ByteString -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO ByteString
getEntropy Int
bytes
where
bits :: Int
bits = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (Int
forall a. HasCallStack => a
undefined :: Int)
bytes :: Int
bytes = Int -> Int -> Int
forall a. Integral a => a -> a -> a
div (Int
bits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Int
8
bsToInt :: ByteString -> Int
bsToInt = (Int -> Word8 -> Int) -> Int -> ByteString -> Int
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
S.foldl' (\Int
v Word8
i -> Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL Int
v Int
8 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
i) Int
0
toWaiAppYre :: YesodDispatch site => YesodRunnerEnv site -> W.Application
toWaiAppYre :: forall site.
YesodDispatch site =>
YesodRunnerEnv site -> Application
toWaiAppYre = Proxy (Route site)
-> ParentArgs (Route site)
-> YesodRunnerEnv (ParentSite (Route site))
-> Application
forall a.
(Yesod (ParentSite a), YesodDispatchNested a, ToParentRoute a) =>
Proxy a
-> ParentArgs a -> YesodRunnerEnv (ParentSite a) -> Application
toWaiAppYreNested (Proxy (Route site)
forall {site}. Proxy (Route site)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (Route site)) ()
toWaiApp :: YesodDispatch site => site -> IO W.Application
toWaiApp :: forall site. YesodDispatch site => site -> IO Application
toWaiApp site
site = do
logger <- site -> IO Logger
forall site. Yesod site => site -> IO Logger
makeLogger site
site
toWaiAppLogger logger site
toWaiAppLogger :: YesodDispatch site => Logger -> site -> IO W.Application
toWaiAppLogger :: forall site. YesodDispatch site => Logger -> site -> IO Application
toWaiAppLogger Logger
logger site
site = do
sb <- site -> IO (Maybe SessionBackend)
forall site. Yesod site => site -> IO (Maybe SessionBackend)
makeSessionBackend site
site
getMaxExpires <- getGetMaxExpires
let yre = YesodRunnerEnv
{ yreLogger :: Logger
yreLogger = Logger
logger
, yreSite :: site
yreSite = site
site
, yreSessionBackend :: Maybe SessionBackend
yreSessionBackend = Maybe SessionBackend
sb
, yreGen :: IO Int
yreGen = IO Int
defaultGen
, yreGetMaxExpires :: IO Text
yreGetMaxExpires = IO Text
getMaxExpires
}
messageLoggerSource
site
logger
$(qLocation >>= liftLoc)
"yesod-core"
LevelInfo
(toLogStr ("Application launched" :: S.ByteString))
middleware <- mkDefaultMiddlewares logger
return $ middleware $ toWaiAppYre yre
warp :: YesodDispatch site => Int -> site -> IO ()
warp :: forall site. YesodDispatch site => Int -> site -> IO ()
warp Int
port site
site = do
logger <- site -> IO Logger
forall site. Yesod site => site -> IO Logger
makeLogger site
site
toWaiAppLogger logger site >>= Network.Wai.Handler.Warp.runSettings (
Network.Wai.Handler.Warp.setPort port $
Network.Wai.Handler.Warp.setServerName serverValue $
Network.Wai.Handler.Warp.setOnException (\Maybe Request
_ SomeException
e ->
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SomeException -> Bool
shouldLog' SomeException
e) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
site -> Logger -> Loc -> Text -> LogLevel -> LogStr -> IO ()
forall site.
Yesod site =>
site -> Logger -> Loc -> Text -> LogLevel -> LogStr -> IO ()
messageLoggerSource
site
site
Logger
logger
$(qLocation >>= liftLoc)
Text
"yesod-core"
LogLevel
LevelError
([Char] -> LogStr
forall msg. ToLogStr msg => msg -> LogStr
toLogStr ([Char] -> LogStr) -> [Char] -> LogStr
forall a b. (a -> b) -> a -> b
$ [Char]
"Exception from Warp: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ SomeException -> [Char]
forall a. Show a => a -> [Char]
show SomeException
e))
Network.Wai.Handler.Warp.defaultSettings)
where
shouldLog' :: SomeException -> Bool
shouldLog' = SomeException -> Bool
Network.Wai.Handler.Warp.defaultShouldDisplayException
serverValue :: S8.ByteString
serverValue :: ByteString
serverValue = [Char] -> ByteString
S8.pack ([Char] -> ByteString) -> [Char] -> ByteString
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Char]
"Warp/"
, [Char]
Network.Wai.Handler.Warp.warpVersion
, [Char]
" + Yesod/"
, Version -> [Char]
showVersion Version
Paths_yesod_core.version
, [Char]
" (core)"
]
mkDefaultMiddlewares :: Logger -> IO W.Middleware
mkDefaultMiddlewares :: Logger -> IO Middleware
mkDefaultMiddlewares Logger
logger = do
logWare <- RequestLoggerSettings -> IO Middleware
mkRequestLogger
#if MIN_VERSION_wai_extra(3,1,8)
RequestLoggerSettings
defaultRequestLoggerSettings
#else
def
#endif
{ destination = Network.Wai.Middleware.RequestLogger.Logger $ loggerSet logger
, outputFormat = Apache FromSocket
}
return $ logWare . defaultMiddlewaresNoLogging
defaultMiddlewaresNoLogging :: W.Middleware
defaultMiddlewaresNoLogging :: Middleware
defaultMiddlewaresNoLogging = Middleware
acceptOverride Middleware -> Middleware -> Middleware
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Middleware
autohead Middleware -> Middleware -> Middleware
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GzipSettings -> Middleware
gzip GzipSettings
gzipSettings Middleware -> Middleware -> Middleware
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Middleware
methodOverride
where
gzipSettings :: GzipSettings
gzipSettings =
#if MIN_VERSION_wai_extra(3,1,14)
GzipSettings
defaultGzipSettings
#else
def
#endif
warpDebug :: YesodDispatch site => Int -> site -> IO ()
warpDebug :: forall site. YesodDispatch site => Int -> site -> IO ()
warpDebug = Int -> site -> IO ()
forall site. YesodDispatch site => Int -> site -> IO ()
warp
{-# DEPRECATED warpDebug "Please use warp instead" #-}
warpEnv :: YesodDispatch site => site -> IO ()
warpEnv :: forall site. YesodDispatch site => site -> IO ()
warpEnv site
site = do
env <- IO [([Char], [Char])]
getEnvironment
case lookup "PORT" env of
Maybe [Char]
Nothing -> [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error [Char]
"warpEnv: no PORT environment variable found"
Just [Char]
portS ->
case [Char] -> Maybe Int
forall a. Read a => [Char] -> Maybe a
readMaybe [Char]
portS of
Maybe Int
Nothing -> [Char] -> IO ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"warpEnv: invalid PORT environment variable: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
portS
Just Int
port -> Int -> site -> IO ()
forall site. YesodDispatch site => Int -> site -> IO ()
warp Int
port site
site
getGetMaxExpires :: IO (IO Text)
getGetMaxExpires :: IO (IO Text)
getGetMaxExpires = UpdateSettings Text -> IO (IO Text)
forall a. UpdateSettings a -> IO (IO a)
mkAutoUpdate UpdateSettings ()
defaultUpdateSettings
{ updateAction = getCurrentMaxExpiresRFC1123
, updateFreq = 24 * 60 * 60 * 1000000
}