Merge pull request #15 from Start9Labs/tor-settings

Tor settings
This commit is contained in:
Lucy C
2020-08-19 16:10:31 -06:00
committed by GitHub
4 changed files with 26 additions and 18 deletions

View File

@@ -32,6 +32,7 @@ app-compatibility-path: "_env:APP_COMPATIBILITY_CONFIG:/etc/start9/registry/comp
resources-path: "_env:RESOURCES_PATH:/var/www/html/resources" resources-path: "_env:RESOURCES_PATH:/var/www/html/resources"
ssl-path: "_env:SSL_PATH:/var/ssl" ssl-path: "_env:SSL_PATH:/var/ssl"
registry-hostname: "_env:REGISTRY_HOSTNAME:registry.start9labs.com" registry-hostname: "_env:REGISTRY_HOSTNAME:registry.start9labs.com"
tor-port: "_env:TOR_PORT:447"
database: database:
database: "_env:PG_DATABASE:start9_registry" database: "_env:PG_DATABASE:start9_registry"

View File

@@ -34,7 +34,7 @@ import Database.Persist.Postgresql (createPostgresqlPool, pg
import Language.Haskell.TH.Syntax (qLocation) import Language.Haskell.TH.Syntax (qLocation)
import Network.Wai import Network.Wai
import Network.Wai.Handler.Warp (Settings, defaultSettings, defaultShouldDisplayException, import Network.Wai.Handler.Warp (Settings, defaultSettings, defaultShouldDisplayException,
getPort, setHost, setOnException, setPort) getPort, setHost, setOnException, setPort, runSettings)
import Network.Wai.Handler.WarpTLS import Network.Wai.Handler.WarpTLS
import Network.Wai.Middleware.AcceptOverride import Network.Wai.Middleware.AcceptOverride
import Network.Wai.Middleware.Autohead import Network.Wai.Middleware.Autohead
@@ -59,6 +59,8 @@ import Settings
import System.Posix.Process import System.Posix.Process
import System.Time.Extra import System.Time.Extra
import Model import Model
import Control.Lens
import Control.Arrow ((***))
-- This line actually creates our YesodDispatch instance. It is the second half -- This line actually creates our YesodDispatch instance. It is the second half
-- of the call to mkYesodData which occurs in Foundation.hs. Please see the -- of the call to mkYesodData which occurs in Foundation.hs. Please see the
@@ -143,9 +145,9 @@ makeAuthWare _ app req res = next
next = app req res next = app req res
-- | Warp settings for the given foundation value. -- | Warp settings for the given foundation value.
warpSettings :: RegistryCtx -> Settings warpSettings :: AppPort -> RegistryCtx -> Settings
warpSettings foundation = warpSettings port foundation =
setPort (fromIntegral . appPort $ appSettings foundation) setPort (fromIntegral port)
$ setHost (appHost $ appSettings foundation) $ setHost (appHost $ appSettings foundation)
$ setOnException (\_req e -> $ setOnException (\_req e ->
when (defaultShouldDisplayException e) $ messageLoggerSource when (defaultShouldDisplayException e) $ messageLoggerSource
@@ -201,14 +203,16 @@ startWeb foundation = do
where where
startWeb' app = do startWeb' app = do
let AppSettings{..} = appSettings foundation let AppSettings{..} = appSettings foundation
putStrLn @Text $ "Launching Tor Web Server on port " <> show torPort
torAction <- async $ runSettings (warpSettings torPort foundation) app
putStrLn @Text $ "Launching Web Server on port " <> show appPort putStrLn @Text $ "Launching Web Server on port " <> show appPort
action <- async $ runTLS action <- async $ runTLS (tlsSettings sslCertLocation sslKeyLocation)
(tlsSettings sslCertLocation sslKeyLocation) (warpSettings appPort foundation)
(warpSettings foundation) app
app let actions = (action, torAction)
setWebProcessThreadId (asyncThreadId action) foundation setWebProcessThreadId (join (***) asyncThreadId actions) foundation
void $ waitCatch action void $ both waitCatch actions
shouldRestart <- takeMVar (appShouldRestartWeb foundation) shouldRestart <- takeMVar (appShouldRestartWeb foundation)
when shouldRestart $ do when shouldRestart $ do
putMVar (appShouldRestartWeb foundation) False putMVar (appShouldRestartWeb foundation) False
@@ -228,17 +232,17 @@ shutdownAll threadIds = do
-- Careful, you should always spawn this within forkIO so as to avoid accidentally killing the running process -- Careful, you should always spawn this within forkIO so as to avoid accidentally killing the running process
shutdownWeb :: RegistryCtx -> IO () shutdownWeb :: RegistryCtx -> IO ()
shutdownWeb RegistryCtx{..} = do shutdownWeb RegistryCtx{..} = do
threadId <- takeMVar appWebServerThreadId threadIds <- takeMVar appWebServerThreadId
killThread threadId void $ both killThread threadIds
-------------------------------------------------------------- --------------------------------------------------------------
-- Functions for DevelMain.hs (a way to run the RegistryCtx from GHCi) -- Functions for DevelMain.hs (a way to run the RegistryCtx from GHCi)
-------------------------------------------------------------- --------------------------------------------------------------
getApplicationRepl :: IO (Int, RegistryCtx, Application) getApplicationRepl :: AppPort -> IO (Int, RegistryCtx, Application)
getApplicationRepl = do getApplicationRepl port = do
foundation <- getAppSettings >>= makeFoundation foundation <- getAppSettings >>= makeFoundation
wsettings <- getDevSettings $ warpSettings foundation wsettings <- getDevSettings $ warpSettings port foundation
app1 <- makeApplication foundation app1 <- makeApplication foundation
return (getPort wsettings, foundation, app1) return (getPort wsettings, foundation, app1)

View File

@@ -29,13 +29,13 @@ import Yesod.Persist.Core
data RegistryCtx = RegistryCtx data RegistryCtx = RegistryCtx
{ appSettings :: AppSettings { appSettings :: AppSettings
, appLogger :: Logger , appLogger :: Logger
, appWebServerThreadId :: MVar ThreadId , appWebServerThreadId :: MVar (ThreadId, ThreadId)
, appShouldRestartWeb :: MVar Bool , appShouldRestartWeb :: MVar Bool
, appCompatibilityMap :: HM.HashMap AppVersion AppVersion , appCompatibilityMap :: HM.HashMap AppVersion AppVersion
, appConnPool :: ConnectionPool , appConnPool :: ConnectionPool
} }
setWebProcessThreadId :: ThreadId -> RegistryCtx -> IO () setWebProcessThreadId :: (ThreadId, ThreadId) -> RegistryCtx -> IO ()
setWebProcessThreadId tid a = putMVar (appWebServerThreadId a) $ tid setWebProcessThreadId tid a = putMVar (appWebServerThreadId a) $ tid
-- This is where we define all of the routes in our application. For a full -- This is where we define all of the routes in our application. For a full

View File

@@ -31,11 +31,12 @@ import Data.Yaml.Config
-- | Runtime settings to configure this application. These settings can be -- | Runtime settings to configure this application. These settings can be
-- loaded from various sources: defaults, environment variables, config files, -- loaded from various sources: defaults, environment variables, config files,
-- theoretically even a database. -- theoretically even a database.
type AppPort = Word16
data AppSettings = AppSettings data AppSettings = AppSettings
{ appDatabaseConf :: PostgresConf { appDatabaseConf :: PostgresConf
, appHost :: HostPreference , appHost :: HostPreference
-- ^ Host/interface the server should bind to. -- ^ Host/interface the server should bind to.
, appPort :: Word16 , appPort :: AppPort
-- ^ Port to listen on -- ^ Port to listen on
, appIpFromHeader :: Bool , appIpFromHeader :: Bool
-- ^ Get the IP address from the header when logging. Useful when sitting -- ^ Get the IP address from the header when logging. Useful when sitting
@@ -53,6 +54,7 @@ data AppSettings = AppSettings
, sslKeyLocation :: FilePath , sslKeyLocation :: FilePath
, sslCsrLocation :: FilePath , sslCsrLocation :: FilePath
, sslCertLocation :: FilePath , sslCertLocation :: FilePath
, torPort :: AppPort
} }
instance FromJSON AppSettings where instance FromJSON AppSettings where
@@ -67,6 +69,7 @@ instance FromJSON AppSettings where
resourcesDir <- o .: "resources-path" resourcesDir <- o .: "resources-path"
sslPath <- o .: "ssl-path" sslPath <- o .: "ssl-path"
registryHostname <- o .: "registry-hostname" registryHostname <- o .: "registry-hostname"
torPort <- o .: "tor-port"
let sslKeyLocation = sslPath </> "key.pem" let sslKeyLocation = sslPath </> "key.pem"
let sslCsrLocation = sslPath </> "certificate.csr" let sslCsrLocation = sslPath </> "certificate.csr"