{-# 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 (State(..), ZenithRPC(..), authenticate, zenithServer) import Zenith.Scanner (rescanZebra) import Zenith.Types (Config(..)) main :: IO () main = do config <- load ["$(HOME)/Zenith/zenith.cfg"] dbFileName <- require config "dbFileName" nodeUser <- require config "nodeUser" nodePwd <- require config "nodePwd" zebraPort <- require config "zebraPort" zebraHost <- require config "zebraHost" nodePort <- require config "nodePort" let myConfig = Config dbFileName 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 dbFileName case x of Left e2 -> throwIO $ userError e2 Right x' -> do when x' $ rescanZebra zebraHost zebraPort dbFileName let myState = State (zgb_net chainInfo) zebraHost zebraPort dbFileName (zgi_build zebra) (zgb_blocks chainInfo) run nodePort $ serveWithContext (Proxy :: Proxy ZenithRPC) ctx (zenithServer myState)