zenith/app/Server.hs

92 lines
3.2 KiB
Haskell
Raw Normal View History

2024-07-24 21:03:49 +00:00
{-# LANGUAGE OverloadedStrings #-}
2024-07-23 18:46:37 +00:00
module Server where
2024-10-08 13:20:52 +00:00
import Control.Concurrent (forkIO, threadDelay)
2024-11-20 22:12:27 +00:00
import Control.Exception (throwIO, throwTo, try)
2024-10-08 13:20:52 +00:00
import Control.Monad (forever, when)
2024-11-20 22:12:27 +00:00
import Control.Monad.Logger (runNoLoggingT)
2024-07-24 21:03:49 +00:00
import Data.Configurator
2024-11-20 22:12:27 +00:00
import qualified Data.Text as T
2024-07-23 18:46:37 +00:00
import Network.Wai.Handler.Warp (run)
import Servant
2024-11-20 22:12:27 +00:00
import System.Exit
import System.Posix.Signals
2024-08-10 12:04:40 +00:00
import ZcashHaskell.Types (ZebraGetBlockChainInfo(..), ZebraGetInfo(..))
import Zenith.Core (checkBlockChain, checkZebra)
2024-11-20 22:12:27 +00:00
import Zenith.DB (getWallets, initDb, initPool)
2024-10-08 13:20:52 +00:00
import Zenith.RPC
( State(..)
, ZenithRPC(..)
, authenticate
, scanZebra
, zenithServer
)
2024-08-10 12:04:40 +00:00
import Zenith.Scanner (rescanZebra)
2024-07-24 21:03:49 +00:00
import Zenith.Types (Config(..))
2024-11-21 13:25:36 +00:00
import Zenith.Utils (getZenithPath)
2024-07-23 18:46:37 +00:00
main :: IO ()
2024-07-24 21:03:49 +00:00
main = do
config <- load ["$(HOME)/Zenith/zenith.cfg"]
2024-11-21 13:25:36 +00:00
dbFileName <- require config "dbFileName"
2024-07-24 21:03:49 +00:00
nodeUser <- require config "nodeUser"
nodePwd <- require config "nodePwd"
zebraPort <- require config "zebraPort"
zebraHost <- require config "zebraHost"
nodePort <- require config "nodePort"
2024-11-21 13:25:36 +00:00
dbFP <- getZenithPath
let dbFilePath = T.pack $ dbFP ++ dbFileName
2024-07-24 21:03:49 +00:00
let myConfig = Config dbFilePath zebraHost zebraPort nodeUser nodePwd nodePort
let ctx = authenticate myConfig :. EmptyContext
2024-08-10 12:04:40 +00:00
w <- try $ checkZebra zebraHost zebraPort :: IO (Either IOError ZebraGetInfo)
case w of
Right zebra -> do
bc <-
try $ checkBlockChain zebraHost zebraPort :: IO
(Either IOError ZebraGetBlockChainInfo)
case bc of
Left e1 -> throwIO e1
Right chainInfo -> do
x <- initDb dbFilePath
case x of
Left e2 -> throwIO $ userError e2
Right x' -> do
when x' $ rescanZebra zebraHost zebraPort dbFilePath
2024-11-20 22:12:27 +00:00
pool <- runNoLoggingT $ initPool dbFilePath
walList <- getWallets pool $ zgb_net chainInfo
if not (null walList)
then do
scanThread <-
forkIO $
forever $ do
_ <-
scanZebra
dbFilePath
zebraHost
zebraPort
(zgb_net chainInfo)
threadDelay 90000000
putStrLn "Zenith RPC Server 0.8.0.0-beta"
2024-11-20 22:12:27 +00:00
putStrLn "------------------------------"
putStrLn $
"Connected to " ++
show (zgb_net chainInfo) ++
" Zebra " ++
T.unpack (zgi_build zebra) ++ " on port " ++ show zebraPort
let myState =
State
(zgb_net chainInfo)
zebraHost
zebraPort
dbFilePath
(zgi_build zebra)
(zgb_blocks chainInfo)
run nodePort $
serveWithContext
(Proxy :: Proxy ZenithRPC)
ctx
(zenithServer myState)
else putStrLn
"No wallets available. Please start Zenith interactively to create a wallet"