Implement Config type and refactor

This commit is contained in:
Rene Vergara 2022-07-12 16:08:27 -05:00
parent b1ae5b51df
commit aa81880c65
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
5 changed files with 99 additions and 50 deletions

View file

@ -2,9 +2,8 @@
module Main where module Main where
import Config
import Control.Concurrent (forkIO) import Control.Concurrent (forkIO)
import Data.Configurator
import Data.SecureMem
import Database.MongoDB import Database.MongoDB
import Network.Wai.Handler.Warp (defaultSettings, setPort) import Network.Wai.Handler.Warp (defaultSettings, setPort)
import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings) import Network.Wai.Handler.WarpTLS (runTLS, tlsSettings)
@ -14,40 +13,31 @@ import ZGoBackend
main :: IO () main :: IO ()
main = do main = do
putStrLn "Reading config..." putStrLn "Reading config..."
config <- load ["zgo.cfg"] loadedConfig <- loadZGoConfig "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 = let myTlsSettings =
if useTls if c_useTls loadedConfig
then Just $ tlsSettings cert key then Just $
tlsSettings (c_certificate loadedConfig) (c_key loadedConfig)
else Nothing else Nothing
putStrLn "Starting Server..." putStrLn "Starting Server..."
pipe <- connect $ host dbHost pipe <- connect $ host (c_dbHost loadedConfig)
j <- access pipe master dbName (auth dbUser dbPassword) j <-
access
pipe
master
(c_dbName loadedConfig)
(auth (c_dbUser loadedConfig) (c_dbPassword loadedConfig))
if j if j
then putStrLn "Connected to MongoDB!" then putStrLn "Connected to MongoDB!"
else fail "MongoDB connection failed!" else fail "MongoDB connection failed!"
_ <- forkIO (setInterval 60 (checkZcashPrices pipe dbName)) _ <- forkIO (setInterval 60 (checkZcashPrices pipe (c_dbName loadedConfig)))
_ <- _ <- forkIO (setInterval 75 (scanZcash loadedConfig pipe))
forkIO (setInterval 75 (scanZcash nodeAddress pipe dbName nodeUser nodePwd)) _ <- forkIO (setInterval 60 (checkPayments pipe (c_dbName loadedConfig)))
_ <- forkIO (setInterval 60 (checkPayments pipe dbName)) _ <- forkIO (setInterval 60 (expireOwners pipe (c_dbName loadedConfig)))
_ <- forkIO (setInterval 60 (expireOwners pipe dbName)) _ <- forkIO (setInterval 60 (updateLogins pipe loadedConfig))
_ <- let appRoutes = routes pipe loadedConfig
forkIO
(setInterval 60 (updateLogins nodeUser nodePwd nodeAddress pipe dbName))
let appRoutes = routes pipe dbName passkey nodeAddress nodeUser nodePwd
case myTlsSettings of case myTlsSettings of
Nothing -> scotty port appRoutes Nothing -> scotty (c_port loadedConfig) appRoutes
Just tls -> do Just tls -> do
apiCore <- scottyApp appRoutes apiCore <- scottyApp appRoutes
runTLS tls (setPort port defaultSettings) apiCore runTLS tls (setPort (c_port loadedConfig) defaultSettings) apiCore

View file

@ -49,6 +49,7 @@ library:
- wai-cors - wai-cors
- warp-tls - warp-tls
- hexstring - hexstring
- configurator
executables: executables:
zgo-backend-exe: zgo-backend-exe:

55
src/Config.hs Normal file
View file

@ -0,0 +1,55 @@
{-# LANGUAGE OverloadedStrings #-}
module Config where
import qualified Data.ByteString as BS
import Data.Configurator
import Data.SecureMem
import qualified Data.Text as T
data Config =
Config
{ c_dbHost :: String
, c_dbName :: T.Text
, c_dbUser :: T.Text
, c_dbPassword :: T.Text
, c_passkey :: SecureMem
, c_nodeAddress :: T.Text
, c_nodeUser :: BS.ByteString
, c_nodePwd :: BS.ByteString
, c_port :: Int
, c_useTls :: Bool
, c_certificate :: String
, c_key :: String
}
deriving (Eq, Show)
loadZGoConfig :: Worth FilePath -> IO Config
loadZGoConfig path = do
config <- load [path]
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"
return $
Config
dbHost
dbName
dbUser
dbPassword
passkey
nodeAddress
nodeUser
nodePwd
port
useTls
cert
key

View file

@ -5,6 +5,7 @@
module ZGoBackend where module ZGoBackend where
import Config
import Control.Concurrent (forkIO, threadDelay) import Control.Concurrent (forkIO, threadDelay)
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
@ -319,16 +320,13 @@ upsertZGoTx coll t = do
upsert (select ["txid" =: txid t] coll) (encodeZGoTxBson t) upsert (select ["txid" =: txid t] coll) (encodeZGoTxBson t)
-- | Main API routes -- | Main API routes
routes :: routes :: Pipe -> Config -> ScottyM ()
Pipe routes pipe config = do
-> T.Text let run = access pipe master (c_dbName config)
-> SecureMem let passkey = c_passkey config
-> T.Text let nodeUser = c_nodeUser config
-> BS.ByteString let nodePwd = c_nodePwd config
-> BS.ByteString let nodeAddress = c_nodeAddress config
-> ScottyM ()
routes pipe db passkey nodeAddress nodeUser nodePwd = do
let run = access pipe master db
middleware $ middleware $
cors $ cors $
const $ const $
@ -568,14 +566,14 @@ checkZcashPrices p db = do
mapM_ (access p master db) (updatePrices (getResponseBody q)) mapM_ (access p master db) (updatePrices (getResponseBody q))
-- | Function to check the ZGo full node for new txs -- | Function to check the ZGo full node for new txs
scanZcash :: T.Text -> Pipe -> T.Text -> BS.ByteString -> BS.ByteString -> IO () scanZcash :: Config -> Pipe -> IO ()
scanZcash addr pipe db nodeUser nodePwd = do scanZcash config pipe = do
res <- res <-
makeZcashCall makeZcashCall
nodeUser (c_nodeUser config)
nodePwd (c_nodePwd config)
"z_listreceivedbyaddress" "z_listreceivedbyaddress"
[Data.Aeson.String addr] [Data.Aeson.String (c_nodeAddress config)]
let txs = let txs =
filter (not . zchange) $ filter (not . zchange) $
result (getResponseBody res :: RpcResponse [ZcashTx]) result (getResponseBody res :: RpcResponse [ZcashTx])
@ -586,14 +584,17 @@ scanZcash addr pipe db nodeUser nodePwd = do
mkRegex mkRegex
".*ZGOp::([0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}).*" ".*ZGOp::([0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}).*"
let k = map zToZGoTx (filter (matchTest r . T.unpack . zmemo) txs) let k = map zToZGoTx (filter (matchTest r . T.unpack . zmemo) txs)
mapM_ (access pipe master db . upsertZGoTx "txs") k mapM_ (access pipe master (c_dbName config) . upsertZGoTx "txs") k
let j = map zToZGoTx (filter (matchTest p . T.unpack . zmemo) txs) let j = map zToZGoTx (filter (matchTest p . T.unpack . zmemo) txs)
mapM_ (access pipe master db . upsertPayment) j mapM_ (access pipe master (c_dbName config) . upsertPayment) j
-- | Function to generate users from login txs -- | Function to generate users from login txs
updateLogins :: updateLogins :: Pipe -> Config -> IO ()
BS.ByteString -> BS.ByteString -> T.Text -> Pipe -> T.Text -> IO () updateLogins pipe config = do
updateLogins nodeUser nodePwd addr pipe db = do let db = c_dbName config
let nodeUser = c_nodeUser config
let nodePwd = c_nodePwd config
let addr = c_nodeAddress config
results <- results <-
access access
pipe pipe

View file

@ -26,6 +26,7 @@ source-repository head
library library
exposed-modules: exposed-modules:
Config
Item Item
Order Order
Owner Owner
@ -44,6 +45,7 @@ library
, base >=4.7 && <5 , base >=4.7 && <5
, bson , bson
, bytestring , bytestring
, configurator
, hexstring , hexstring
, http-conduit , http-conduit
, http-types , http-types