-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtradeserver.go
More file actions
163 lines (148 loc) · 3.99 KB
/
tradeserver.go
File metadata and controls
163 lines (148 loc) · 3.99 KB
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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"tradingServer/entity"
"tradingServer/server"
"tradingServer/serviceMarket"
"tradingServer/servicePriceVariation"
"tradingServer/serviceTrade"
"tradingServer/serviceUser"
"tradingServer/storage"
)
func initPriceMakers(ev chan entity.MarketAsset) {
db := storage.GetDatabase()
assets, err := db.GetAssets()
if err != nil {
log.Fatalf("initPriceMakers() failed to fetch assets: %v", err)
}
for _, ass := range assets {
pm := servicePriceVariation.NewPriceMaker(ass.Name, ass.Price, ev)
go pm.Run()
}
}
func runServer() {
s := server.NewServer()
f, err := os.OpenFile("tradingServer.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Printf("failed to open or create tradingServer.log: %v", err)
} else {
log.SetOutput(f)
}
initPriceMakers(s.GetEventInputChannel())
s.Run()
}
func usage() {
fmt.Println(`usage: ./tradingServer <command> <options...>
commands:
adduser <login> [<password>] [<email>]
Create new user account. Password will be generated and printed to
console unless specified.
log [dump]
Show last 10 combined log messages from access and transaction log.
Dump will output the entire log.
resetdb
Reset database. User accounts, user assets and logs will be deleted.
Not really needed as database will be initialised automatically upon
first operation in case it does not exist.
setpw <login> [<password>]
Reset user's password. If no password given, it will be autogenerated
and printed to console.
setemail <login> <email>
Update user's email address.
Without any sub command given the server will start up and wait for incoming requests.
Database will be created and initialised if it does not exist.`)
}
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "adduser":
if len(os.Args) < 3 {
fmt.Printf("missing arguments: %v adduser <login> [<password>] [<email>]\n", os.Args[0])
os.Exit(1)
}
login := os.Args[2]
password := ""
if len(os.Args) > 3 {
password = os.Args[3]
}
email := ""
if len(os.Args) > 4 {
email = os.Args[4]
}
serviceUser.AddUser(login, password, email)
case "addasset":
if len(os.Args) < 3 {
fmt.Printf("missing arguments: %v addasset <name> <price>\n", os.Args[0])
os.Exit(1)
}
name, priceStr := os.Args[2], os.Args[3]
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
fmt.Printf("price is not a number: %v", priceStr)
os.Exit(1)
}
err = serviceMarket.AddAsset(name, price)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
case "log":
if len(os.Args) < 3 {
// show last 10 messages of access log and transaction log
serviceTrade.ShowLastLog(10)
} else if os.Args[2] == "dump" {
// export combined access and transaction log
serviceTrade.ShowLastLog(-1)
} else if os.Args[2] == "tail" {
// live tracking of access log
fmt.Println("no yet implemented")
os.Exit(1)
} else {
fmt.Printf("invalid sub command '%v'\n", os.Args[2])
os.Exit(1)
}
case "resetdb":
if len(os.Args) > 2 {
fmt.Printf("invalid arguments: %v (initdb takes no parameters)\n", strings.Join(os.Args[2:], " "))
}
serviceUser.RemoveUsers()
serviceMarket.ResetPrices()
serviceTrade.ResetLog()
case "setpw":
if len(os.Args) < 2 {
fmt.Printf("missing arguments: %v setpw <login> [<password>]\n", os.Args[0])
os.Exit(1)
}
login := os.Args[2]
password := ""
if len(os.Args) > 3 {
password = os.Args[3]
}
serviceUser.ChangeUser(login, password, "")
case "setemail":
if len(os.Args) < 3 {
fmt.Printf("missing arguments: %v setemail <login> <email>\n", os.Args[0])
os.Exit(1)
}
login, email := os.Args[2], os.Args[3]
serviceUser.ChangeUser(login, "", email)
case "help":
fallthrough
case "-help":
fallthrough
case "--help":
fallthrough
case "-h":
usage()
return
default:
log.Fatalf("unknown subcommand '%v'\n", os.Args[1])
}
} else {
runServer()
}
}