-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
164 lines (140 loc) · 6.88 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"context"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/nordicenergy/powerchain-maker-nodemanager/client"
"github.com/nordicenergy/powerchain-maker-nodemanager/contractclient"
"github.com/nordicenergy/powerchain-maker-nodemanager/service"
"net/http"
"os"
"os/signal"
"time"
)
var nodeUrl = "http://localhost:22000"
var listenPort = ":8000"
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
func main() {
if len(os.Args) > 1 {
nodeUrl = os.Args[1]
}
if len(os.Args) > 2 {
listenPort = ":" + os.Args[2]
}
router := mux.NewRouter()
nodeService := service.NodeServiceImpl{nodeUrl}
ticker := time.NewTicker(86400 * time.Second)
go func() {
for range ticker.C {
log.Debug("Rotating log for Geth and Constellation.")
nodeService.LogRotaterGeth()
nodeService.LogRotaterConst()
}
}()
go func() {
nodeService.CheckGethStatus(nodeUrl)
//log.Info("Deploying Network Manager Contract")
nodeService.NetworkManagerContractDeployer(nodeUrl)
nodeService.RegisterNodeDetails(nodeUrl)
nodeService.ContractCrawler(nodeUrl)
nodeService.ABICrawler(nodeUrl)
nodeService.IPWhitelister()
}()
networkMapService := contractclient.NetworkMapContractClient{EthClient: client.EthClient{nodeUrl}}
router.HandleFunc("/txn/{txn_hash}", nodeService.GetTransactionInfoHandler).Methods("GET")
router.HandleFunc("/txn", nodeService.GetLatestTransactionInfoHandler).Methods("GET")
router.HandleFunc("/block/{block_no}", nodeService.GetBlockInfoHandler).Methods("GET")
router.HandleFunc("/block", nodeService.GetLatestBlockInfoHandler).Methods("GET")
router.HandleFunc("/genesis", nodeService.GetGenesisHandler).Methods("POST", "OPTIONS")
router.HandleFunc("/peer/{peer_id}", nodeService.GetOtherPeerHandler).Methods("GET")
router.HandleFunc("/peer", nodeService.JoinNetworkHandler).Methods("POST", "OPTIONS")
router.HandleFunc("/peer", nodeService.GetCurrentNodeHandler).Methods("GET")
router.HandleFunc("/txnrcpt/{txn_hash}", nodeService.GetTransactionReceiptHandler).Methods("GET")
router.HandleFunc("/pendingJoinRequests", nodeService.PendingJoinRequestsHandler).Methods("GET")
router.HandleFunc("/joinRequestResponse", nodeService.JoinRequestResponseHandler).Methods("POST")
router.HandleFunc("/joinRequestResponse", nodeService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/createNetwork", nodeService.CreateNetworkScriptCallHandler).Methods("POST")
router.HandleFunc("/createNetwork", nodeService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/joinNetwork", nodeService.JoinNetworkScriptCallHandler).Methods("POST")
router.HandleFunc("/joinNetwork", nodeService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/deployContract", nodeService.DeployContractHandler).Methods("POST")
router.HandleFunc("/reset", nodeService.ResetHandler).Methods("GET")
router.HandleFunc("/restart", nodeService.RestartHandler).Methods("GET")
router.HandleFunc("/latestBlock", nodeService.LatestBlockHandler).Methods("GET")
router.HandleFunc("/latency", nodeService.LatencyHandler).Methods("GET")
//router.HandleFunc("/logs", nodeService.LogsHandler).Methods("GET")
router.HandleFunc("/txnsearch/{txn_hash}", nodeService.TransactionSearchHandler).Methods("GET")
router.HandleFunc("/mailserver", nodeService.MailServerConfigHandler).Methods("POST")
router.HandleFunc("/mailserver", nodeService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/registerNode", networkMapService.RegisterNodeRequestHandler).Methods("POST")
router.HandleFunc("/updateNode", networkMapService.UpdateNodeHandler).Methods("POST")
router.HandleFunc("/updateNode", networkMapService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/getNodeDetails/{index}", networkMapService.GetNodeDetailsResponseHandler).Methods("GET")
router.HandleFunc("/getNodeList", networkMapService.GetNodeListSelfResponseHandler).Methods("GET")
router.HandleFunc("/activeNodes", networkMapService.ActiveNodesHandler).Methods("GET")
router.HandleFunc("/chartData", nodeService.GetChartDataHandler).Methods("GET")
router.HandleFunc("/contractList", nodeService.GetContractListHandler).Methods("GET")
router.HandleFunc("/contractCount", nodeService.GetContractCountHandler).Methods("GET")
router.HandleFunc("/updateContractDetails", nodeService.ContractDetailsUpdateHandler).Methods("POST")
router.HandleFunc("/attachedNodeDetails", nodeService.AttachedNodeDetailsHandler).Methods("POST")
router.HandleFunc("/initialized", nodeService.InitializationHandler).Methods("GET")
router.HandleFunc("/createAccount", nodeService.CreateAccountHandler).Methods("POST")
router.HandleFunc("/createAccount", nodeService.OptionsHandler).Methods("OPTIONS")
router.HandleFunc("/getAccounts", nodeService.GetAccountsHandler).Methods("GET")
router.HandleFunc("/getWhitelist", nodeService.GetWhitelistedIPsHandler).Methods("GET")
router.HandleFunc("/updateWhitelist", nodeService.UpdateWhitelistHandler).Methods("POST")
router.HandleFunc("/updateWhitelist", nodeService.OptionsHandler).Methods("OPTIONS")
router.PathPrefix("/contracts").Handler(http.StripPrefix("/contracts", http.FileServer(http.Dir("/root/powerchain-maker/contracts"))))
router.PathPrefix("/geth").Handler(http.StripPrefix("/geth", http.FileServer(http.Dir("/home/node/qdata/gethLogs"))))
router.PathPrefix("/constellation").Handler(http.StripPrefix("/constellation", http.FileServer(http.Dir("/home/node/qdata/constellationLogs"))))
router.PathPrefix("/").Handler(http.StripPrefix("/", NewFileServer("NodeManagerUI")))
log.Info(fmt.Sprintf("Node Manager listening on %s...", listenPort))
srv := &http.Server{
Handler: router,
Addr: "0.0.0.0" + listenPort,
//WriteTimeout: 15 * time.Second,
//ReadTimeout: 15 * time.Second,
//IdleTimeout: time.Second * 60,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), 15)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Info("Node Manager Shutting down")
os.Exit(0)
}
type MyFileServer struct {
name string
handler http.Handler
}
func NewFileServer(file string) *MyFileServer {
return &MyFileServer{file, http.FileServer(http.Dir(file))}
}
func (mf *MyFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, err := os.Open(mf.name + "/" + r.URL.Path)
if err != nil {
r.URL.Path = "/"
}
mf.handler.ServeHTTP(w, r)
}