zgo-backend/app/Server.hs

46 lines
1.5 KiB
Haskell
Raw Permalink Normal View History

2022-04-22 16:15:23 +00:00
{-# LANGUAGE OverloadedStrings #-}
2022-10-26 20:34:29 +00:00
module Server where
2022-04-22 16:15:23 +00:00
2022-07-12 21:08:27 +00:00
import Config
2023-03-14 15:17:31 +00:00
--import Control.Concurrent (forkIO)
2022-04-22 16:15:23 +00:00
import Database.MongoDB
2022-05-19 14:52:17 +00:00
import Network.Wai.Handler.Warp (defaultSettings, setPort)
import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings)
import Web.Scotty
2022-04-22 16:15:23 +00:00
import ZGoBackend
main :: IO ()
main = do
2022-05-19 13:24:52 +00:00
putStrLn "Reading config..."
2022-07-12 21:08:27 +00:00
loadedConfig <- loadZGoConfig "zgo.cfg"
2022-05-19 13:24:52 +00:00
let myTlsSettings =
2022-07-12 21:08:27 +00:00
if c_useTls loadedConfig
then Just $
tlsSettings (c_certificate loadedConfig) (c_key loadedConfig)
2022-05-19 13:24:52 +00:00
else Nothing
2022-04-22 16:15:23 +00:00
putStrLn "Starting Server..."
2022-07-12 21:08:27 +00:00
pipe <- connect $ host (c_dbHost loadedConfig)
j <-
access
pipe
master
(c_dbName loadedConfig)
(auth (c_dbUser loadedConfig) (c_dbPassword loadedConfig))
2022-04-22 16:15:23 +00:00
if j
then putStrLn "Connected to MongoDB!"
else fail "MongoDB connection failed!"
2023-03-14 15:17:31 +00:00
{-_ <- forkIO (setInterval 60 (checkZcashPrices pipe (c_dbName loadedConfig)))-}
{-_ <- forkIO (setInterval 75 (scanZcash loadedConfig pipe))-}
{-_ <- forkIO (setInterval 90 (scanPayments loadedConfig pipe))-}
{-_ <- forkIO (setInterval 60 (checkPayments pipe (c_dbName loadedConfig)))-}
{-_ <- forkIO (setInterval 60 (expireOwners pipe (c_dbName loadedConfig)))-}
{-_ <- forkIO (setInterval 60 (updateLogins pipe loadedConfig))-}
2022-07-12 21:08:27 +00:00
let appRoutes = routes pipe loadedConfig
2022-05-19 14:52:17 +00:00
case myTlsSettings of
2022-07-12 21:08:27 +00:00
Nothing -> scotty (c_port loadedConfig) appRoutes
2022-05-19 14:52:17 +00:00
Just tls -> do
apiCore <- scottyApp appRoutes
2022-07-12 21:08:27 +00:00
runTLS tls (setPort (c_port loadedConfig) defaultSettings) apiCore