zenith/app/Server.hs

68 lines
2.1 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-08-10 12:04:40 +00:00
import Control.Exception (throwIO, try)
2024-10-08 13:20:52 +00:00
import Control.Monad (forever, 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-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-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"]
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
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-10-08 13:20:52 +00:00
_ <-
forkIO $
forever $ do
_ <-
scanZebra dbFilePath zebraHost zebraPort (zgb_net chainInfo)
threadDelay 90000000
2024-08-26 20:25:31 +00:00
let myState =
State
(zgb_net chainInfo)
zebraHost
zebraPort
dbFilePath
(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)