-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (153 loc) · 4.12 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
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"strings"
mydb "myDatabase"
_ "github.com/mattn/go-sqlite3"
)
type TextContent struct {
Content string `json:"content"`
}
type TextContent2 struct {
C1 string `json:"content1"`
C2 string `json:"content2"`
}
func writeJSONOKResonse(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}
func writeJSONErrResonse(w http.ResponseWriter, msg string, errcode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errcode)
json.NewEncoder(w).Encode(map[string]string{
"error": msg,
})
}
func writeJSONParseIncomplete(w http.ResponseWriter, msg string, errcode int, data string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errcode)
json.NewEncoder(w).Encode(map[string]string{
"error": msg,
"content": data,
})
}
func main() {
mydb.InitMyDB()
http.HandleFunc("/", statisticHandler)
http.HandleFunc("/parseTrans", parseTransHandler)
http.HandleFunc("/addref", addRefHandler)
http.HandleFunc("/parser", parserHandler)
http.HandleFunc("/scanner", scannerHandler)
fmt.Println("Server started at http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
mydb.CloseMyDB()
}
func parserHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "./web/account.html")
case "POST":
createTransaction(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func parseTransHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
parseTransaction(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func addRefHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var textContent TextContent2
if err := json.NewDecoder(r.Body).Decode(&textContent); err != nil {
writeJSONErrResonse(w, "Failed to parse request body", http.StatusBadRequest)
return
}
code := textContent.C1
name := textContent.C2
mydb.AddRef(code, name)
writeJSONOKResonse(w, textContent)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func statisticHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "./web/index.html")
case "POST":
doStatistic(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func scannerHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "./web/scanner.html")
case "POST":
doScan(w, r)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func createTransaction(w http.ResponseWriter, r *http.Request) {
var t mydb.Transaction
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
t.Total = int(math.Round(t.Price * float64(t.Quantity)))
if t.Direction {
t.Tax = 0
t.Net = t.Total + t.Fee
} else {
t.Tax = int(math.Round(float64(t.Total) * 0.003))
t.Net = t.Total - t.Fee - t.Tax
}
err := mydb.AddTransaction(t)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(t)
}
func parseTransaction(w http.ResponseWriter, r *http.Request) {
var textContent TextContent
if err := json.NewDecoder(r.Body).Decode(&textContent); err != nil {
writeJSONErrResonse(w, "Failed to parse request body", http.StatusBadRequest)
return
}
var curr_idx int
data := textContent.Content
data = strings.TrimPrefix(data, "\n")
entries := strings.Split(data, "\n")
for i, ent := range entries {
rc, msg := parseEntry(ent)
curr_idx = i
if rc != http.StatusOK {
found := true
for n := 0; n < curr_idx && found; n = n + 1 {
idx := strings.Index(data, "\n")
if idx > 0 {
data = data[idx+1:]
}
// fmt.Println("cut", n, idx, data)
}
writeJSONParseIncomplete(w, msg, rc, data)
return
}
}
writeJSONOKResonse(w, map[string]int{"count": curr_idx})
}