fix the https redirection

master
Michele Guerini Rocco 2019-11-06 19:36:31 +01:00
parent 101f5c06af
commit 7fdfb25ce0
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450
2 changed files with 23 additions and 13 deletions

View File

@ -100,6 +100,13 @@ type API =
breve :: FilePath -> Url -> UrlTable -> Application breve :: FilePath -> Url -> UrlTable -> Application
breve static url table = serve (Proxy :: Proxy Breve) (breveServer static url table) breve static url table = serve (Proxy :: Proxy Breve) (breveServer static url table)
-- | Empty application
--
-- This app does *nothing* but it's useful nonetheless:
-- it will be used as a basis to run the 'forceSSL'
-- middleware.
emptyApp :: Application
emptyApp = serve (Proxy :: Proxy EmptyAPI) emptyServer
-- * Handlers -- * Handlers

View File

@ -19,24 +19,26 @@ import Data.String (IsString(..))
import Data.Maybe (listToMaybe) import Data.Maybe (listToMaybe)
-- IO -- IO
import Control.Monad (when, void)
import Control.Concurrent (forkIO)
import System.Environment (getArgs) import System.Environment (getArgs)
import Data.Text.IO as T import Data.Text.IO as T
-- Web server -- Web server
import Servant (Application) import Servant (Application)
import Network.Wai.Handler.Warp (defaultSettings, setPort, setHost) import Network.Wai.Handler.Warp (run, defaultSettings, setPort, setHost)
import Network.Wai.Handler.WarpTLS (runTLS, TLSSettings) import Network.Wai.Handler.WarpTLS (runTLS, TLSSettings)
-- Middlewares -- Middlewares
import Network.Wai.Middleware.ForceSSL (forceSSL)
import Network.Wai.Middleware.RequestLogger (logStdout) import Network.Wai.Middleware.RequestLogger (logStdout)
import Network.Wai.Middleware.ForceSSL (forceSSL)
-- * Helpers -- * Helpers
-- | Runs Breve on the Warp webserver -- | Runs Breve on the Warp webserver
run :: AppSettings -> Application -> IO () runApp :: AppSettings -> Application -> IO ()
run (AppSettings{..}) = runApp (AppSettings{..}) =
runTLS tlsSettings warpSettings runTLS tlsSettings warpSettings
where where
host = unpack bindHost host = unpack bindHost
@ -54,13 +56,14 @@ main = do
table <- load urlTable table <- load urlTable
static <- getDataFileName "static/" static <- getDataFileName "static/"
-- Middlewares are just functions of type -- Redirect from HTTP to HTTPS when listening
-- (Application -> Application). We use a couple here -- on the standard port
-- to add requests logging and HTTPS redirection. when (bindPort == 443) $ void $
let forkIO (run 80 $ forceSSL emptyApp)
middlewares =
logStdout . -- Middlewares are functions (Application -> Application).
(if bindPort == 433 then forceSSL else id) -- We use one here to add requests
let middlewares = logStdout
T.putStrLn ("Serving on " <> bindUrl) T.putStrLn ("Serving on " <> bindUrl)
run config (middlewares $ breve static bindUrl table) runApp config (middlewares $ breve static bindUrl table)