如何使用 warp-tls 而不是 scotty 的 warp?

How to use warp-tls instead of warp with scotty?

我需要使用 warp-tls 而不是普通的 warp 服务器来启动我的 scotty 应用程序,但似乎 运行 warp 是硬连接在 scotty 的源代码中的。我是否漏掉了一些明显的东西?

您可以使用 scottyApp 函数而不是 scotty 来获取 WAI Application,您可以将其传递给 warp 的 runTLS:

{-# LANGUAGE OverloadedStrings #-}

import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings)
import Network.Wai.Handler.Warp (defaultSettings, setPort)
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
import Web.Scotty

main :: IO ()
main = do
    let tlsConfig = tlsSettings "your.crt" "your.key"
        config    = setPort 3443 defaultSettings
    waiApp <- scottyApp $ do
        get "/"      (text "hello")
        get "/hello" (text "hello again")
    runTLS tlsConfig config (logStdoutDev waiApp)