Created
September 19, 2019 10:55
-
-
Save ubourdon/7fec18f7f5875e6c865a9c9aa5c02c32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE DuplicateRecordFields #-} | |
{-# LANGUAGE OverloadedLabels #-} | |
{-# LANGUAGE DisambiguateRecordFields #-} | |
module Main where | |
import Data.Text | |
import Text.Read (readMaybe) | |
--import Network.Wai (responseLBS, Application) | |
import Network.Wai.Handler.Warp (run) | |
import Network.Wai (Middleware) | |
--import Network.HTTP.Types (status200) | |
--import Network.HTTP.Types.Header (hContentType) | |
import Network.Wai.Application.Static (staticApp, defaultFileServerSettings) | |
import Network.Wai.Middleware.Gzip (GzipSettings, GzipFiles(..), defaultCheckMime, def, gzipFiles, gzip) | |
import System.Environment (lookupEnv) | |
--(|>) :: a -> a -> a | |
(|>) x y = y x | |
main :: IO () | |
main = do | |
envPort <- lookupEnv "PORT" | |
envDocumentRootPath <- lookupEnv "DOCUMENT_ROOT" | |
serverPort <- (envPort >>= mPort) |> pure | |
startServer $ initConfig serverPort (fmap Path envDocumentRootPath) | |
startServer :: Maybe ServerConfig -> IO () | |
startServer (Just config) = run config.port.value $ staticApp (defaultFileServerSettings config.path.value) |> myGzip | |
startServer _ = error "need config" | |
myGzip :: Middleware | |
myGzip = gzip def { gzipFiles = GzipCompress } | |
newtype Port = Port { value :: Int } deriving Show | |
newtype Path = Path { value :: String } deriving Show | |
mPort :: String -> Maybe Port | |
mPort s = fmap Port $ readMaybe s | |
data ServerConfig = ServerConfig { | |
port :: Port, | |
documentRootPath :: Path | |
} | |
initConfig :: Maybe Port -> Maybe Path -> Maybe ServerConfig | |
initConfig (Just port) (Just path) = Just $ ServerConfig port path | |
initConfig _ _ = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment