-
Notifications
You must be signed in to change notification settings - Fork 148
/
app.js
executable file
·178 lines (160 loc) · 4.61 KB
/
app.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const program = require('commander')
const path = require('path')
const fs = require('fs')
const ip = require('ip')
const daemon = require('daemon')
const tracer = require('tracer')
const asch = require('asch-core')
const Application = asch.Application
function main() {
process.stdin.resume()
const version = '1.5.2'
program
.version(version)
.option('-c, --config <path>', 'Config file path')
.option('-p, --port <port>', 'Listening port number')
.option('-a, --address <ip>', 'Listening host name or ip')
.option('-g, --genesisblock <path>', 'Genesisblock path')
.option('-x, --peers [peers...]', 'Peers list')
.option('-l, --log <level>', 'Log level')
.option('-d, --daemon', 'Run asch node as daemon')
.option('--app <dir>', 'App directory')
.option('--base <dir>', 'Base directory')
.option('--data <dir>', 'Data directory')
.parse(process.argv)
const baseDir = program.base || './'
let appConfigFile = path.join(baseDir, 'config.json')
if (program.config) {
appConfigFile = path.resolve(process.cwd(), program.config)
}
const appConfig = JSON.parse(fs.readFileSync(appConfigFile, 'utf8'))
const pidFile = appConfig.pidFile || path.join(baseDir, 'asch.pid')
if (fs.existsSync(pidFile)) {
console.log('Failed: asch server already started')
return
}
appConfig.version = version
appConfig.baseDir = baseDir
appConfig.dataDir = program.data || path.resolve(baseDir, 'data')
appConfig.appDir = program.app || path.resolve(baseDir, 'src')
appConfig.buildVersion = 'DEFAULT_BUILD_TIME'
appConfig.netVersion = process.env.NET_VERSION || 'testnet'
appConfig.publicDir = path.join(baseDir, 'public', 'dist')
global.Config = appConfig
let genesisblockFile = path.join(baseDir, 'genesisBlock.json')
if (program.genesisblock) {
genesisblockFile = path.resolve(process.cwd(), program.genesisblock)
}
const genesisblock = JSON.parse(fs.readFileSync(genesisblockFile, 'utf8'))
if (program.port) {
appConfig.port = program.port
}
if (!appConfig.peerPort) {
appConfig.peerPort = appConfig.port + 1
}
if (program.address) {
appConfig.address = program.address
}
if (program.peers) {
if (typeof program.peers === 'string') {
appConfig.peers.list = program.peers.split(',').map((peer) => {
const parts = peer.split(':')
return {
ip: parts.shift(),
port: parts.shift() || appConfig.port,
}
})
} else {
appConfig.peers.list = []
}
}
if (appConfig.netVersion === 'mainnet') {
const seeds = [
{
ip: '47.52.169.154',
port: 8193,
id: 'PAxFQA98nFUYbUceLpXviadKaCZvHcKC6P'
},
{
ip: '47.244.132.54',
port: 8193,
id: 'PKRX44RfMbxAu2txUAMPtRCEjEJfDr8FVu'
},
{
ip: '45.32.254.236',
port: 81,
id: 'PGb927r7CTmrGH1RWMh1Vf15ACDHB2v6mM'
},
{
ip: '108.61.181.76',
port: 81,
id: 'PECZ4C9oxr4473ZP42nRfgi995QX9v9KnG'
},
{
ip: '104.207.135.39',
port: 81,
id: 'PHnHEhUPMAgHL5eKyT6TkAKB1arKHUUvEu'
},
{
ip: '45.76.99.134',
port: 81,
id: 'PDTNRtKNJkncb17gQiVHxdzVw7vwKgmpr7'
},
{
ip: '104.238.181.114',
port: 81,
id: 'PMD3LUNZH8KVr5ZjXpXu7uCyTX4PaxMM86'
},
{
ip: '107.191.41.208',
port: 81,
id: 'PtYpACnBydF9ee8U4C3NXpHVKEK4p2Rdn'
},
{
ip: '104.238.180.37',
port: 81,
id: 'P5KiQADBEz4NiDSEt5o8xnwLjwDXw2qLn7'
},
{
ip: '45.76.98.139',
port: 81,
id: 'PCbSyzhU7qLbJh2nBrdFB9wHk2rGJsg8WV'
}
]
for (let i = 0; i < seeds.length; ++i) {
appConfig.peers.list.push(seeds[i])
}
}
if (program.log) {
appConfig.logLevel = program.log
} else if (process.env.LOG_LEVEL) {
appConfig.logLevel = program.log
}
appConfig.logLevel = program.log || process.env.LOG_LEVEL || appConfig.logLevel
if (program.daemon) {
console.log('Asch server started as daemon ...')
daemon({ cwd: process.cwd() })
fs.writeFileSync(pidFile, process.pid, 'utf8')
}
let logger
if (program.daemon) {
logger = tracer.dailyfile({
root: path.join(baseDir, 'logs'),
maxLogFiles: 10,
allLogsFileName: 'debug',
dateformat: 'yyyy-mm-dd HH:MM:ss.L'
})
} else {
logger = tracer.colorConsole({ dateformat: 'yyyy-mm-dd HH:MM:ss.L' })
}
tracer.setLevel(appConfig.logLevel)
const options = {
appConfig,
genesisblock,
logger,
pidFile,
}
const app = new Application(options)
app.run()
}
main()