fix the https redirection
This commit is contained in:
parent
101f5c06af
commit
7fdfb25ce0
@ -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
|
||||
|
||||
|
29
src/Main.hs
29
src/Main.hs
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user