This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
148 lines (128 loc) · 4.61 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/bitmark-inc/bitmark-node-docker/config"
"github.com/bitmark-inc/bitmark-node-docker/server"
"github.com/bitmark-inc/bitmark-node-docker/services"
luaconf "github.com/bitmark-inc/bitmarkd/configuration"
"github.com/bitmark-inc/exitwithstatus"
"github.com/bitmark-inc/logger"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
)
var version string = "v0.1" // do not change this value
var log *logger.L
// MasterConfiguration is a bitmark-node-docker Configuration file
type MasterConfiguration struct {
Port int `gluamapper:"port" json:"port"`
DataDir string `gluamapper:"datadir" json:"datadir"`
Logging logger.Configuration `gluamapper:"logging" json:"logging"`
VersionURL string `gluamapper:"versionURL" json:"versionURL"`
}
// Parse take the filepath and parse the configure
func (c *MasterConfiguration) Parse(filepath string) error {
if err := luaconf.ParseConfigurationFile(filepath, c); err != nil {
panic(fmt.Sprintf("config file read failed: %s", err))
}
return nil
}
func init() {
if len(os.Getenv("VERSION")) == 0 {
log.Warn("Version is set to default")
os.Setenv("VERSION", version)
}
}
func main() {
defer exitwithstatus.Handler()
var confFile string
var containerIP string
var uiPath string
flag.StringVar(&confFile, "config-file", "bitmark-node-docker.conf", "configuration for bitmark-node-docker")
flag.StringVar(&containerIP, "container-ip", "", "ip address for container")
flag.StringVar(&uiPath, "ui", "ui/public", "path of ui interface")
flag.Parse()
var masterConfig MasterConfiguration
err := masterConfig.Parse(confFile)
if err != nil {
exitwithstatus.Message(err.Error())
}
err = logger.Initialise(masterConfig.Logging)
if err != nil {
exitwithstatus.Message(err.Error())
}
log = logger.New("bitmark-node-docker")
log.Info(fmt.Sprintf("DataDirectory:%s", masterConfig.DataDir))
log.Info(fmt.Sprintf("Port:%d", masterConfig.Port))
log.Info(fmt.Sprintf("VersionURL:%s", masterConfig.VersionURL))
defer logger.Finalise()
var rootPath string
if filepath.IsAbs(masterConfig.DataDir) {
rootPath = masterConfig.DataDir
} else {
rootPath, err = filepath.Abs(filepath.Join(filepath.Dir(confFile), masterConfig.DataDir))
if err != nil {
exitwithstatus.Message(err.Error())
}
}
bitmarkdPath := filepath.Join(rootPath, "bitmarkd")
recorderdPath := filepath.Join(rootPath, "recorderd")
dbPath := filepath.Join(rootPath, "db")
err = os.MkdirAll(bitmarkdPath, 0755)
err = os.MkdirAll(recorderdPath, 0755)
err = os.MkdirAll(dbPath, 0755)
bitmarkdService := services.NewBitmarkd(containerIP)
recorderdService := services.NewRecorderd()
bitmarkdService.Initialise(bitmarkdPath)
defer bitmarkdService.Finalise()
recorderdService.Initialise(recorderdPath)
defer recorderdService.Finalise()
nodeConfig := config.New()
err = nodeConfig.Initialise(dbPath)
if err != nil {
exitwithstatus.Message(err.Error())
}
if network := nodeConfig.GetNetwork(); network != "" {
bitmarkdService.SetNetwork(network)
recorderdService.SetNetwork(network)
}
webserver := server.NewWebServer(
nodeConfig,
rootPath,
bitmarkdService,
recorderdService,
masterConfig.VersionURL,
)
peerport := os.Getenv("PeerPort")
if len(peerport) == 0 {
peerport = "2136"
}
go webserver.CheckPortReachableRoutine(os.Getenv("PUBLIC_IP"), peerport)
go webserver.ClearCmdErrorRoutine(bitmarkdService)
r := gin.New()
r.Use(static.Serve("/", static.LocalFile(uiPath, true)))
r.GET("/", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
})
apiRouter := r.Group("/api")
apiRouter.GET("/info", webserver.NodeInfo)
apiRouter.GET("/config", webserver.GetConfig)
apiRouter.POST("/config", webserver.UpdateConfig)
apiRouter.GET("/chain", webserver.GetChain)
apiRouter.POST("/account/", webserver.NewAccount)
apiRouter.GET("/account/", webserver.GetAccount)
apiRouter.GET("/account/save", webserver.SaveAccount)
apiRouter.GET("/account/delete", webserver.DeleteSavedAccount)
apiRouter.POST("/account/phrase", webserver.SetRecoveryPhrase)
apiRouter.GET("/account/phrase", webserver.GetRecoveryPhrase)
apiRouter.GET("/bitmarkd/conn_stat", webserver.ConnectionStatus)
apiRouter.POST("/bitmarkd", webserver.BitmarkdStartStop)
apiRouter.GET("/latestVersion", webserver.LatestVersion)
apiRouter.POST("/recorderd", webserver.RecorderdStartStop)
apiRouter.GET("/log/:serviceName", webserver.GetLog)
apiRouter.POST("/snapshot", webserver.DownloadSnapshot)
apiRouter.GET("/snapshot-info", webserver.GetSnapshotInfo)
r.Run(fmt.Sprintf(":%d", masterConfig.Port))
}