forked from NebulousLabs/Sia
/
server.go
56 lines (45 loc) · 1.32 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package api
import (
"github.com/stretchr/graceful"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/modules/consensus"
"github.com/NebulousLabs/Sia/sync"
"github.com/NebulousLabs/Sia/types"
)
// A Server is essentially a collection of modules and an API server to talk
// to them all.
type Server struct {
cs modules.ConsensusSet
gateway modules.Gateway
host modules.Host
hostdb modules.HostDB
miner modules.Miner
renter modules.Renter
tpool modules.TransactionPool
wallet modules.Wallet
// Consensus set variables.
blockchainHeight types.BlockHeight
currentBlock types.Block
apiServer *graceful.Server
mu *sync.RWMutex
}
// NewServer creates a new API server from the provided modules.
func NewServer(APIaddr string, s *consensus.State, g modules.Gateway, h modules.Host, hdb modules.HostDB, m modules.Miner, r modules.Renter, tp modules.TransactionPool, w modules.Wallet) (*Server, error) {
srv := &Server{
cs: s,
gateway: g,
host: h,
hostdb: hdb,
miner: m,
renter: r,
tpool: tp,
wallet: w,
mu: sync.New(modules.SafeMutexDelay, 1),
}
// Set the genesis block and start listening to the consensus package.
srv.currentBlock = srv.cs.GenesisBlock()
srv.cs.ConsensusSetSubscribe(srv)
// Register API handlers
srv.initAPI(APIaddr)
return srv, nil
}