Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No using data-default #1011

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stack-lts-19.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ packages:
- ./wai-http2-extra
- ./wai-websockets
- ./warp
# - ./warp-quic
- ./warp-quic
- ./warp-tls
flags:
wai-extra:
Expand All @@ -28,12 +28,18 @@ extra-deps:
- crypton-1.0.1
- crypton-x509-1.7.7
- crypton-x509-store-1.6.9
- crypton-x509-system-1.6.7
- crypton-x509-validation-1.6.12
- fast-logger-3.2.5
- http-semantics-0.2.1
- http2-5.3.4
- http3-0.0.18
- memory-0.18.0
- network-3.1.4.0
- network-byte-order-0.1.7
- network-control-0.1.3
- quic-0.2.4
- sockaddr-0.0.1
- tls-2.1.3
- tls-session-manager-0.0.7
- unix-time-0.4.16
7 changes: 6 additions & 1 deletion stack-lts-20.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ packages:
- ./wai-http2-extra
- ./wai-websockets
- ./warp
# - ./warp-quic
- ./warp-quic
- ./warp-tls
flags:
wai-extra:
Expand All @@ -28,13 +28,18 @@ extra-deps:
- crypton-1.0.1
- crypton-x509-1.7.7
- crypton-x509-store-1.6.9
- crypton-x509-system-1.6.7
- crypton-x509-validation-1.6.12
- fast-logger-3.2.5
- http-semantics-0.2.1
- http2-5.3.4
- http3-0.0.18
- memory-0.18.0
- multipart-0.2.1
- network-byte-order-0.1.7
- network-control-0.1.3
- quic-0.2.4
- sockaddr-0.0.1
- tls-2.1.3
- tls-session-manager-0.0.7
- unix-time-0.4.16
7 changes: 6 additions & 1 deletion stack-lts-21.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ packages:
- ./wai-http2-extra
- ./wai-websockets
- ./warp
# - ./warp-quic
- ./warp-quic
- ./warp-tls
flags:
wai-extra:
Expand All @@ -27,9 +27,14 @@ extra-deps:
- crypton-1.0.1
- crypton-x509-1.7.7
- crypton-x509-store-1.6.9
- crypton-x509-system-1.6.7
- crypton-x509-validation-1.6.12
- http-semantics-0.2.1
- http2-5.3.4
- http3-0.0.18
- network-control-0.1.3
- quic-0.2.4
- sockaddr-0.0.1
- tls-2.1.3
- tls-session-manager-0.0.7
- unix-time-0.4.16
7 changes: 5 additions & 2 deletions stack-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ nix:
- fcgi
- zlib
extra-deps:
- crypto-token-0.1.2
- http-semantics-0.2.1
- http3-0.0.18
- network-udp-0.0.0
- quic-0.2.3
- sockaddr-0.0.1
- quic-0.2.4
- tls-2.1.3
- tls-session-manager-0.0.7
- sockaddr-0.0.1
4 changes: 2 additions & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-22.39
resolver: lts-22.40
packages:
- ./auto-update
- ./mime-types
Expand Down Expand Up @@ -29,7 +29,7 @@ extra-deps:
- http3-0.0.16
- network-control-0.1.0
- network-udp-0.0.0
- quic-0.2.3
- quic-0.2.4
- sockaddr-0.0.1
- tls-2.1.3
- tls-session-manager-0.0.7
2 changes: 1 addition & 1 deletion wai-app-static/WaiAppStatic/CmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ runCommandLine middleware = do
port
(if noindex then "no" else show index)
let middle =
gzip def{gzipFiles = GzipCompress}
gzip defaultGzipSettings{gzipFiles = GzipCompress}
. (if verbose then logStdout else id)
. middleware clArgs
runSettings
Expand Down
8 changes: 8 additions & 0 deletions wai-extra/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog for wai-extra

## 3.1.17

* Started deprecation of `data-default` [#1011](https://github.com/yesodweb/wai/pull/1011)
* All `Default` instances have comments that these will be removed in a future major version.
* `def` exported from `Network.Wai.Middleware.Gzip` now has a deprecation warning
* All uses of `def` have been replaced with explicit `default{TYPE_NAME}` values.
* Some additional documentation

## 3.1.16

* Substituted `data-default-class` for `data-default` [#1010](https://github.com/yesodweb/wai/pull/1010)
Expand Down
26 changes: 17 additions & 9 deletions wai-extra/Network/Wai/Middleware/Gzip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import qualified Data.ByteString.Builder.Extra as Blaze (flush)
import qualified Data.ByteString.Char8 as S8
import Data.ByteString.Lazy.Internal (defaultChunkSize)
import Data.Char (isAsciiLower, isAsciiUpper, isDigit)
import Data.Default (Default (..))
import qualified Data.Default as Default (Default (..))
import Data.Function (fix)
import Data.Maybe (isJust)
import qualified Data.Set as Set
Expand Down Expand Up @@ -119,12 +119,12 @@ import Network.Wai.Util (splitCommas, trimWS)

-- $settings
--
-- If you would like to use the default settings, using just 'def' is enough.
-- If you would like to use the default settings, use 'defaultGzipSettings'.
-- The default settings don't compress file responses, only builder and stream
-- responses, and only if the response passes the MIME and length checks. (cf.
-- 'defaultCheckMime' and 'gzipSizeThreshold')
--
-- To customize your own settings, use the 'def' method and set the
-- To customize your own settings, use 'defaultGzipSettings' and set the
-- fields you would like to change as follows:
--
-- @
Expand Down Expand Up @@ -196,15 +196,23 @@ data GzipFiles

-- $miscellaneous
--
-- 'def' is re-exported for convenience sake, and 'defaultCheckMime'
-- is exported in case anyone wants to use it in defining their own
-- 'gzipCheckMime' function.
-- 'defaultCheckMime' is exported in case anyone wants to use it in
-- defining their own 'gzipCheckMime' function.
-- 'def' has been re-exported for convenience sake, but its use is now
-- heavily discouraged. Please use the explicit 'defaultGzipSettings'.

-- | Use default MIME settings; /do not/ compress files; skip
-- compression on data smaller than 860 bytes.
instance Default GzipSettings where
-- | DO NOT USE THIS INSTANCE!
-- Please use 'defaultGzipSettings'.
--
-- This instance will be removed in a future major version.
instance Default.Default GzipSettings where
def = defaultGzipSettings

-- | Deprecated synonym for the 'defaultGzipSettings'.
def :: GzipSettings
def = defaultGzipSettings
{-# Deprecated def "Please use 'defaultGzipSettings'. 'def' and the Default instance will be removed in a future major update." #-}

-- | Default settings for the 'gzip' middleware.
--
-- * Does not compress files.
Expand Down
79 changes: 65 additions & 14 deletions wai-extra/Network/Wai/Middleware/RequestLogger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ module Network.Wai.Middleware.RequestLogger (

-- * Create more versions
mkRequestLogger,
-- ** Settings type
RequestLoggerSettings,
defaultRequestLoggerSettings,
-- *** Settings fields
outputFormat,
autoFlush,
destination,
-- ** More settings
OutputFormat (..),
ApacheSettings,
defaultApacheSettings,
setApacheIPAddrSource,
setApacheRequestFilter,
setApacheUserGetter,
DetailedSettings (..),
defaultDetailedSettings,
OutputFormatter,
OutputFormatterWithDetails,
OutputFormatterWithDetailsAndHeaders,
Expand Down Expand Up @@ -77,6 +81,8 @@ import Network.Wai.Parse (
)

-- | The logging format.
--
-- @since 1.3.0
data OutputFormat
= Apache IPAddrSource
| -- | @since 3.1.8
Expand Down Expand Up @@ -159,14 +165,27 @@ data DetailedSettings = DetailedSettings
-- ^ @since 3.1.7
}

-- | DO NOT USE THIS INSTANCE!
-- Please use 'defaultDetailedSettings'
--
-- This instance will be removed in a future major version.
instance Default DetailedSettings where
def =
DetailedSettings
{ useColors = True
, mModifyParams = Nothing
, mFilterRequests = Nothing
, mPrelogRequests = False
}
def = defaultDetailedSettings

-- | Default 'DetailedSettings'
--
-- Uses colors, but doesn't modify nor filter anything.
-- Also doesn't prelog requests.
--
-- @since 3.1.16
defaultDetailedSettings :: DetailedSettings
defaultDetailedSettings =
DetailedSettings
{ useColors = True
, mModifyParams = Nothing
, mFilterRequests = Nothing
, mPrelogRequests = False
}

type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr

Expand Down Expand Up @@ -206,29 +225,52 @@ type OutputFormatterWithDetailsAndHeaders =
-- ^ The response headers
-> LogStr

-- | Where to send the logs to.
--
-- @since 1.3.0
data Destination
= Handle Handle
| Logger LoggerSet
| Callback Callback


-- | When using a callback as a destination.
--
-- @since 1.3.0
type Callback = LogStr -> IO ()

-- | @RequestLoggerSettings@ is an instance of Default. See <https://hackage.haskell.org/package/data-default Data.Default> for more information.
-- | Settings for the request logger.
--
-- Sets what which format,
--
-- @outputFormat@, @autoFlush@, and @destination@ are record fields
-- for the record type @RequestLoggerSettings@, so they can be used to
-- modify settings values using record syntax.
--
-- @since 1.3.0
data RequestLoggerSettings = RequestLoggerSettings
{ outputFormat :: OutputFormat
-- ^ Default value: @Detailed@ @True@.
-- ^ Default value: @Detailed True@.
--
-- @since 1.3.0
, autoFlush :: Bool
-- ^ Only applies when using the @Handle@ constructor for @destination@.
-- ^ Only applies when using the 'Handle' constructor for 'destination'.
--
-- Default value: @True@.
--
-- @since 1.3.0
, destination :: Destination
-- ^ Default: @Handle@ @stdout@.
-- ^ Default: @Handle stdout@.
--
-- @since 1.3.0
}

-- | Default 'RequestLoggerSettings'.
--
-- Use this to create 'RequestLoggerSettings', and use the
-- accompanying fields to edit these settings.
--
-- @since 3.1.8
defaultRequestLoggerSettings :: RequestLoggerSettings
defaultRequestLoggerSettings =
RequestLoggerSettings
Expand All @@ -237,9 +279,16 @@ defaultRequestLoggerSettings =
, destination = Handle stdout
}

-- | DO NOT USE THIS INSTANCE!
-- Please use 'defaultRequestLoggerSettings' instead.
--
-- This instance will be removed in a future major release.
instance Default RequestLoggerSettings where
def = defaultRequestLoggerSettings

-- | Create the 'Middleware' using the given 'RequestLoggerSettings'
--
-- @since 1.3.0
mkRequestLogger :: RequestLoggerSettings -> IO Middleware
mkRequestLogger RequestLoggerSettings{..} = do
let (callback, flusher) =
Expand All @@ -263,7 +312,7 @@ mkRequestLogger RequestLoggerSettings{..} = do
getdate
return $ apacheMiddleware apacheRequestFilter apache
Detailed useColors ->
let settings = def{useColors = useColors}
let settings = defaultDetailedSettings{useColors = useColors}
in detailedMiddleware callbackAndFlush settings
DetailedWithSettings settings ->
detailedMiddleware callbackAndFlush settings
Expand Down Expand Up @@ -339,14 +388,16 @@ customMiddlewareWithDetailsAndHeaders cb getdate formatter app req sendResponse
-- the socket (see 'IPAddrSource' for more information). It logs to 'stdout'.
{-# NOINLINE logStdout #-}
logStdout :: Middleware
logStdout = unsafePerformIO $ mkRequestLogger def{outputFormat = Apache FromSocket}
logStdout =
unsafePerformIO $
mkRequestLogger defaultRequestLoggerSettings{outputFormat = Apache FromSocket}

-- | Development request logger middleware.
--
-- This uses the 'Detailed' 'True' logging format and logs to 'stdout'.
{-# NOINLINE logStdoutDev #-}
logStdoutDev :: Middleware
logStdoutDev = unsafePerformIO $ mkRequestLogger def
logStdoutDev = unsafePerformIO $ mkRequestLogger defaultRequestLoggerSettings

-- | Prints a message using the given callback function for each request.
-- This is not for serious production use- it is inefficient.
Expand Down
4 changes: 2 additions & 2 deletions wai-extra/example/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Network.Wai.EventSource (
)
import Network.Wai.Handler.Warp (run)
import Network.Wai.Middleware.AddHeaders (addHeaders)
import Network.Wai.Middleware.Gzip (def, gzip)
import Network.Wai.Middleware.Gzip (defaultGzipSettings, gzip)

app :: Chan ServerEvent -> Application
app chan req respond =
Expand Down Expand Up @@ -67,7 +67,7 @@ main :: IO ()
main = do
chan <- newChan
_ <- forkIO . eventChan $ chan
run 8080 (gzip def $ headers $ app chan)
run 8080 (gzip defaultGzipSettings $ headers $ app chan)
where
-- headers required for SSE to work through nginx
-- not required if using warp directly
Expand Down
Loading