{-# LANGUAGE OverloadedStrings #-} module Server where import Control.Exception (throwIO, try) import Control.Monad (when) import Data.Configurator import Network.Wai.Handler.Warp (run) import Servant import ZcashHaskell.Types (ZebraGetBlockChainInfo(..), ZebraGetInfo(..)) import Zenith.Core (checkBlockChain, checkZebra) import Zenith.DB (initDb) import Zenith.RPC (ZenithRPC(..), authenticate, zenithServer) import Zenith.Scanner (rescanZebra) import Zenith.Types (Config(..)) main :: IO () main = do config <- load ["$(HOME)/Zenith/zenith.cfg"] dbFilePath <- require config "dbFilePath" nodeUser <- require config "nodeUser" nodePwd <- require config "nodePwd" zebraPort <- require config "zebraPort" zebraHost <- require config "zebraHost" nodePort <- require config "nodePort" let myConfig = Config dbFilePath zebraHost zebraPort nodeUser nodePwd nodePort let ctx = authenticate myConfig :. EmptyContext 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 run nodePort $ serveWithContext (Proxy :: Proxy ZenithRPC) ctx (zenithServer myConfig)