zgo-backend/app/Main.hs

54 lines
1.8 KiB
Haskell
Raw Normal View History

2022-04-22 16:15:23 +00:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
2022-05-19 13:24:52 +00:00
import Control.Concurrent (forkIO)
import Data.Configurator
2022-04-22 16:15:23 +00:00
import Data.SecureMem
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..."
config <- load ["zgo.cfg"]
2022-05-19 17:56:56 +00:00
dbHost <- require config "dbHost"
2022-05-19 13:24:52 +00:00
dbName <- require config "dbName"
dbUser <- require config "dbUser"
dbPassword <- require config "dbPassword"
nodeAddress <- require config "nodeAddress"
2022-05-19 17:56:56 +00:00
nodeUser <- require config "nodeUser"
nodePwd <- require config "nodePassword"
2022-05-19 13:24:52 +00:00
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
2022-04-22 16:15:23 +00:00
putStrLn "Starting Server..."
2022-05-19 17:56:56 +00:00
pipe <- connect $ host dbHost
2022-05-19 13:24:52 +00:00
j <- access pipe master dbName (auth dbUser dbPassword)
2022-04-22 16:15:23 +00:00
if j
then putStrLn "Connected to MongoDB!"
else fail "MongoDB connection failed!"
2022-05-19 13:24:52 +00:00
_ <- forkIO (setInterval 60 (checkZcashPrices pipe dbName))
2022-05-19 17:56:56 +00:00
_ <-
forkIO (setInterval 75 (scanZcash nodeAddress pipe dbName nodeUser nodePwd))
2022-05-19 13:24:52 +00:00
_ <- forkIO (setInterval 60 (checkPayments pipe dbName))
_ <- forkIO (setInterval 60 (expireOwners pipe dbName))
2022-05-20 14:16:51 +00:00
_ <-
forkIO
(setInterval 60 (updateLogins nodeUser nodePwd nodeAddress pipe dbName))
2022-05-19 17:56:56 +00:00
let appRoutes = routes pipe dbName passkey nodeAddress nodeUser nodePwd
2022-05-19 14:52:17 +00:00
case myTlsSettings of
Nothing -> scotty port appRoutes
Just tls -> do
apiCore <- scottyApp appRoutes
runTLS tls (setPort port defaultSettings) apiCore