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 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

View File

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