Remove internal TLS, add option for HTTPS redirection
This commit is contained in:
+11
-9
@@ -4,14 +4,14 @@
|
||||
|
||||
module Main where
|
||||
import Dbapi
|
||||
import Middleware (reportPgErrors)
|
||||
import Middleware (reportPgErrors, redirectInsecure)
|
||||
import Network.Wai.Handler.Warp hiding (Connection)
|
||||
import Database.HDBC.PostgreSQL (connectPostgreSQL')
|
||||
import Data.String.Conversions (cs)
|
||||
|
||||
import Control.Monad (unless)
|
||||
import Control.Applicative
|
||||
import Options.Applicative hiding (columns)
|
||||
import Network.Wai.Handler.WarpTLS (tlsSettings, runTLS)
|
||||
import Network.Wai.Middleware.Gzip (gzip, def)
|
||||
import Network.Wai.Middleware.Cors (cors)
|
||||
|
||||
@@ -23,12 +23,10 @@ argParser = AppConfig
|
||||
<> help "database uri to expose, e.g. postgres://user:pass@host:port/database")
|
||||
<*> option (long "port" <> short 'p' <> metavar "NUMBER" <> value 3000
|
||||
<> help "port number on which to run HTTP server")
|
||||
<*> strOption (long "sslcert" <> short 'c' <> metavar "PATH" <> value "test/test.crt"
|
||||
<> help "path to SSL cert file")
|
||||
<*> strOption (long "sslkey" <> short 'k' <> metavar "PATH" <> value "test/test.key"
|
||||
<> help "path to SSL key file")
|
||||
<*> strOption (long "anonymous" <> short 'a' <> metavar "ROLE"
|
||||
<> help "postgres role to use for non-authenticated requests")
|
||||
<*> switch (long "secure" <> short 's'
|
||||
<> help "Redirect all requests to HTTPS" )
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
@@ -36,12 +34,16 @@ main = do
|
||||
let port = configPort conf
|
||||
let dburi = configDbUri conf
|
||||
|
||||
let tls = tlsSettings (configSslCert conf) (configSslKey conf)
|
||||
let settings = setPort port defaultSettings
|
||||
unless (configSecure conf) $
|
||||
putStrLn "WARNING, running in insecure mode, auth will be in plaintext"
|
||||
|
||||
Prelude.putStrLn $ "Listening on port " ++ (show $ configPort conf :: String)
|
||||
conn <- connectPostgreSQL' dburi
|
||||
runTLS tls settings $ gzip def $ cors corsPolicy $ reportPgErrors $ app conn (cs $ configAnonRole conf)
|
||||
run port $ (if configSecure conf then redirectInsecure else id)
|
||||
$ gzip def
|
||||
$ cors corsPolicy
|
||||
$ reportPgErrors
|
||||
$ app conn (cs $ configAnonRole conf)
|
||||
|
||||
where
|
||||
describe = progDesc "create a REST API to an existing Postgres database"
|
||||
|
||||
Reference in New Issue
Block a user