Skip to content

Commit 4dd73fb

Browse files
authored
Version 0.1.2.1. runWebServerWith function. (#14)
1 parent 9466663 commit 4dd73fb

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.1.2.1] - 2020-04-15
10+
### Added
11+
- `runWebServerWith` function that runs a web server with custom Warp settings.
12+
913
## [0.1.2.0] - 2020-04-09
1014
### Changed
1115
- `warp` is configured to print exceptions to `stdout` in json format;

src/Web/Template/Server.hs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
{-# LANGUAGE MultiParamTypeClasses #-}
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE RecordWildCards #-}
5-
{-# LANGUAGE TypeSynonymInstances #-}
65

76
module 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)
2626
import Network.HTTP.Types.Status (status401)
2727
import 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).
6363
runWebServer :: (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

6678
toApplication :: (Monoid w, Show w) => CustomWebServer r w s -> IO Application
6779
toApplication s = scottyAppT (evalCustomWebServer s) (toScottyT s)
@@ -89,13 +101,17 @@ defaultHeaderCORS = modifyResponse (mapResponseHeaders addHeaderCORS)
89101
runRoute :: Monoid w => Route r w s -> ScottyM r w s ()
90102
runRoute 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

101117
onException :: Maybe Request -> SomeException -> IO ()

web-template.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: web-template
2-
version: 0.1.2.0
2+
version: 0.1.2.1
33
synopsis: Web template
44
description:
55
Web template includes:

0 commit comments

Comments
 (0)