zenith/app/Server.hs

55 lines
1.8 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-08-10 12:04:40 +00:00
import Control.Exception (throwIO, try)
import Control.Monad (when)
2024-07-24 21:03:49 +00:00
import Data.Configurator
2024-07-23 18:46:37 +00:00
import Network.Wai.Handler.Warp (run)
import Servant
2024-08-10 12:04:40 +00:00
import ZcashHaskell.Types (ZebraGetBlockChainInfo(..), ZebraGetInfo(..))
import Zenith.Core (checkBlockChain, checkZebra)
import Zenith.DB (initDb)
2024-08-26 20:25:31 +00:00
import Zenith.RPC (State(..), ZenithRPC(..), authenticate, 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-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"]
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"
let myConfig = Config dbFileName zebraHost zebraPort nodeUser nodePwd nodePort
2024-07-24 21:03:49 +00:00
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 dbFileName
2024-08-10 12:04:40 +00:00
case x of
Left e2 -> throwIO $ userError e2
Right x' -> do
when x' $ rescanZebra zebraHost zebraPort dbFileName
2024-08-26 20:25:31 +00:00
let myState =
State
(zgb_net chainInfo)
zebraHost
zebraPort
dbFileName
2024-08-26 20:25:31 +00:00
(zgi_build zebra)
(zgb_blocks chainInfo)
2024-08-10 12:04:40 +00:00
run nodePort $
serveWithContext
(Proxy :: Proxy ZenithRPC)
ctx
2024-08-26 20:25:31 +00:00
(zenithServer myState)