Search code examples
haskellscottyhaskell-warp

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


I need to start my scotty application with warp-tls instead of plain warp server but is seems running warp is hardwired in scotty's source code. Am I missing something obvious?


Solution

  • You can use the scottyApp function instead of scotty, to get a WAI Application which you can pass to warp's 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)