This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
/
bitmarkd.go
153 lines (136 loc) · 3.84 KB
/
bitmarkd.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
// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package api
import (
"encoding/json"
"github.com/bitmark-inc/bitmark-webgui/configuration"
"github.com/bitmark-inc/bitmark-webgui/structs"
bitmarkdConfig "github.com/bitmark-inc/bitmarkd/configuration"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/logger"
"net/http"
"net/rpc/jsonrpc"
"os"
"time"
)
type bitmarkdRequest struct {
Option string `json:"option"`
Network string `json:"network"`
// ConfigFile string `json:"config_file"`
}
// POST /api/bitmarkd
func Bitmarkd(w http.ResponseWriter, req *http.Request, webguiFilePath string, webguiConfig *configuration.Configuration, log *logger.L) {
log.Info("POST /api/bitmarkd")
response := &Response{
Ok: false,
Result: nil,
}
decoder := json.NewDecoder(req.Body)
var request bitmarkdRequest
if err := decoder.Decode(&request); nil != err {
log.Errorf("Error: %v", err)
response.Result = err
if err := writeApiResponseAndSetCookie(w, response); nil != err {
log.Errorf("Error: %v", err)
}
return
}
log.Infof("bitmarkd option: %s", request.Option)
apiErr := invalidValueErr
switch request.Option {
case `start`:
// Check if bitmarkd is running
if bitmarkService.IsRunning() {
response.Result = bitmarkdAlreadyStartErr
} else {
bitmarkService.ModeStart <- true
// wait one second to get correct result
time.Sleep(time.Second * 1)
if !bitmarkService.IsRunning() {
response.Result = bitmarkdStartErr
} else {
response.Ok = true
response.Result = bitmarkdStartSuccess
}
}
case `stop`:
if !bitmarkService.IsRunning() {
response.Result = bitmarkdAlreadyStopErr
} else {
bitmarkService.ModeStart <- false
time.Sleep(time.Second * 1)
if bitmarkService.IsRunning() {
response.Result = bitmarkdStopErr
} else {
response.Ok = true
response.Result = bitmarkdStopSuccess
}
}
case `status`:
response.Ok = true
if bitmarkService.IsRunning() {
response.Result = bitmarkdStarted
} else {
response.Result = bitmarkdStopped
}
case `info`:
if !bitmarkService.IsRunning() {
response.Result = bitmarkdAlreadyStopErr
} else {
if info, err := getBitmarkdInfo(webguiConfig.BitmarkConfigFile, log); "" != err {
response.Result = err
} else {
response.Ok = true
response.Result = info
}
}
case `setup`:
if err := bitmarkService.Setup(request.Network, webguiFilePath, webguiConfig); nil != err {
if os.IsNotExist(err) {
response.Result = "bitmarkd config not found"
} else {
response.Result = err.Error()
}
} else {
response.Ok = true
response.Result = nil
}
default:
response.Result = apiErr
if err := writeApiResponseAndSetCookie(w, response); nil != err {
log.Errorf("Error: %v", err)
}
return
}
if err := writeApiResponseAndSetCookie(w, response); nil != err {
log.Errorf("Error: %v", err)
}
}
func getBitmarkdInfo(bitmarkConfigFile string, log *logger.L) (*rpc.InfoReply, string) {
bitmarkConfig := &structs.BitmarkdConfiguration{}
err := bitmarkdConfig.ParseConfigurationFile(bitmarkConfigFile, bitmarkConfig)
if nil != err {
log.Errorf("Failed to get bitmarkd configuration: %v", err)
return nil, bitmarkdGetConfigErr
}
if len(bitmarkConfig.ClientRPC.Listen) == 0 {
log.Errorf("No listensing port in bitmarkd configuration.")
return nil, bitmarkdConnectErr
}
conn, err := bitmarkService.Connect(bitmarkConfig.ClientRPC.Listen[0])
if nil != err {
log.Errorf("Failed to connect to bitmarkd: %v", err)
return nil, bitmarkdConnectErr
}
defer conn.Close()
// create a client
client := jsonrpc.NewClient(conn)
defer client.Close()
info, err := bitmarkService.GetInfo(client)
if nil != err {
log.Errorf("Failed to get bitmark info: %v", err)
return nil, bitmarkdGetInfoErr
}
return info, ""
}