@ -27,16 +27,18 @@ type Service struct {
router * mux . Router
router * mux . Router
IP string
IP string
Port string
Port string
GetNumPeers func ( ) int
storage * Storage
storage * Storage
server * http . Server
server * http . Server
messageChan chan * msg_pb . Message
messageChan chan * msg_pb . Message
}
}
// New returns explorer service.
// New returns explorer service.
func New ( selfPeer * p2p . Peer ) * Service {
func New ( selfPeer * p2p . Peer , GetNumPeers func ( ) int ) * Service {
return & Service {
return & Service {
IP : selfPeer . IP ,
IP : selfPeer . IP ,
Port : selfPeer . Port ,
Port : selfPeer . Port ,
GetNumPeers : GetNumPeers ,
}
}
}
}
@ -89,6 +91,9 @@ func (s *Service) Run() *http.Server {
s . router . Path ( "/address" ) . Queries ( "id" , "{[0-9A-Fa-fx]*?}" ) . HandlerFunc ( s . GetExplorerAddress ) . Methods ( "GET" )
s . router . Path ( "/address" ) . Queries ( "id" , "{[0-9A-Fa-fx]*?}" ) . HandlerFunc ( s . GetExplorerAddress ) . Methods ( "GET" )
s . router . Path ( "/address" ) . HandlerFunc ( s . GetExplorerAddress )
s . router . Path ( "/address" ) . HandlerFunc ( s . GetExplorerAddress )
// Set up router for nodes.
s . router . Path ( "/nodes" ) . HandlerFunc ( s . GetExplorerNodes )
// Do serving now.
// Do serving now.
utils . GetLogInstance ( ) . Info ( "Listening on " , "port: " , GetExplorerPort ( s . Port ) )
utils . GetLogInstance ( ) . Info ( "Listening on " , "port: " , GetExplorerPort ( s . Port ) )
server := & http . Server { Addr : addr , Handler : s . router }
server := & http . Server { Addr : addr , Handler : s . router }
@ -247,6 +252,12 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
json . NewEncoder ( w ) . Encode ( data . Address )
json . NewEncoder ( w ) . Encode ( data . Address )
}
}
// GetExplorerNodes serves /nodes end-point.
func ( s * Service ) GetExplorerNodes ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , "application/json" )
json . NewEncoder ( w ) . Encode ( s . GetNumPeers ( ) )
}
// NotifyService notify service
// NotifyService notify service
func ( s * Service ) NotifyService ( params map [ string ] interface { } ) {
func ( s * Service ) NotifyService ( params map [ string ] interface { } ) {
return
return