22{-# LANGUAGE MultiParamTypeClasses #-}
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE RecordWildCards #-}
5- {-# LANGUAGE TypeSynonymInstances #-}
65
76module Web.Template.Server
87 ( restartOnError
98 , restartOnError1
109 , runWebServer
10+ , runWebServerWith
1111 , defaultHandleLog
1212 , defaultHeaderCORS
1313 , toApplication
@@ -26,7 +26,7 @@ import Network.HTTP.Types.Header (Header)
2626import Network.HTTP.Types.Status (status401 )
2727import Network.Wai (Application , Middleware , Request ,
2828 mapResponseHeaders , modifyResponse )
29- import Network.Wai.Handler.Warp (defaultSettings ,
29+ import Network.Wai.Handler.Warp (Settings , defaultSettings ,
3030 exceptionResponseForDebug ,
3131 setOnException ,
3232 setOnExceptionResponse , setPort )
@@ -59,9 +59,21 @@ restartOnError f delayUs = f `catch` handle
5959 threadDelay delayUs
6060 restartOnError f delayUs
6161
62- -- | For given port and server settings run the server.
62+ -- | For given port and server settings run the server with default timeout (30 seconds) .
6363runWebServer :: (Monoid w , Show w ) => Port -> CustomWebServer r w s -> IO ()
64- runWebServer port s = scottyOptsT (scottyOpts port) (evalCustomWebServer s) (toScottyT s)
64+ runWebServer port s = scottyOptsT (scottyOpts port id ) (evalCustomWebServer s) (toScottyT s)
65+
66+ -- | For given user settings, port and server settings run the server.
67+ -- Setting port and exception handler via @userSettings@ will have no effect.
68+ -- Use @port@ to set up port instead.
69+ --
70+ runWebServerWith
71+ :: (Monoid w , Show w )
72+ => (Settings -> Settings )
73+ -> Port
74+ -> CustomWebServer r w s
75+ -> IO ()
76+ runWebServerWith userSettings port s = scottyOptsT (scottyOpts port userSettings) (evalCustomWebServer s) (toScottyT s)
6577
6678toApplication :: (Monoid w , Show w ) => CustomWebServer r w s -> IO Application
6779toApplication s = scottyAppT (evalCustomWebServer s) (toScottyT s)
@@ -89,13 +101,17 @@ defaultHeaderCORS = modifyResponse (mapResponseHeaders addHeaderCORS)
89101runRoute :: Monoid w => Route r w s -> ScottyM r w s ()
90102runRoute Route {.. } = method (fromString $ " /:version" <> path) (checkVersion version . auth $ process)
91103
92- scottyOpts :: Port -> Options
93- scottyOpts port = Options 1 warpSettings
104+ -- | Create @Options@ with given port and timeout.
105+ -- If no timeout is given, it will be set to Warp's default (30 seconds).
106+ --
107+ scottyOpts :: Port -> (Settings -> Settings ) -> Options
108+ scottyOpts port userSettings = Options 1 warpSettings
94109 where
95110 warpSettings =
96111 setOnException onException
97112 . setOnExceptionResponse exceptionResponseForDebug
98113 . setPort port
114+ . userSettings
99115 $ defaultSettings
100116
101117onException :: Maybe Request -> SomeException -> IO ()
0 commit comments