zgo-backend/app/Main.hs

51 lines
1.7 KiB
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Concurrent (forkIO)
import Data.Configurator
import Data.SecureMem
import Database.MongoDB
import Network.Wai.Handler.Warp (defaultSettings, setPort)
import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings)
import Web.Scotty
import ZGoBackend
main :: IO ()
main = do
putStrLn "Reading config..."
config <- load ["zgo.cfg"]
dbHost <- require config "dbHost"
dbName <- require config "dbName"
dbUser <- require config "dbUser"
dbPassword <- require config "dbPassword"
nodeAddress <- require config "nodeAddress"
nodeUser <- require config "nodeUser"
nodePwd <- require config "nodePassword"
passkey <- secureMemFromByteString <$> require config "passkey"
port <- require config "port"
useTls <- require config "tls"
cert <- require config "certificate"
key <- require config "key"
let myTlsSettings =
if useTls
then Just $ tlsSettings cert key
else Nothing
putStrLn "Starting Server..."
pipe <- connect $ host dbHost
j <- access pipe master dbName (auth dbUser dbPassword)
if j
then putStrLn "Connected to MongoDB!"
else fail "MongoDB connection failed!"
_ <- forkIO (setInterval 60 (checkZcashPrices pipe dbName))
_ <-
forkIO (setInterval 75 (scanZcash nodeAddress pipe dbName nodeUser nodePwd))
_ <- forkIO (setInterval 60 (checkPayments pipe dbName))
_ <- forkIO (setInterval 60 (expireOwners pipe dbName))
let appRoutes = routes pipe dbName passkey nodeAddress nodeUser nodePwd
case myTlsSettings of
Nothing -> scotty port appRoutes
Just tls -> do
apiCore <- scottyApp appRoutes
runTLS tls (setPort port defaultSettings) apiCore