-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (224 loc) · 7.02 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"database/sql"
"flag"
"fmt"
"strings"
"log"
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)
var set Settings
var queries []Query
var zvits []Zvit
func main() {
initSettings()
zvits = initZvits()
flag.Parse()
//db, err := sql.Open("mysql", "root:toortoortoor@tcp(localhost:3306)/rtb")
db, err := sql.Open("mysql", "root@tcp(192.168.2.84:3306)/rtb")
//db, err := sql.Open("mysql", "root@tcp(localhost:3306)/rtb")
if err != nil {
panic(err.Error())
}
defer db.Close()
queries = initQueries(db)
stmtIns, err := db.Prepare("INSERT INTO companies VALUES(?,?,?,?)")
if err != nil {
panic(err.Error())
}
defer stmtIns.Close()
getTables, err := db.Prepare("show tables;")
if err != nil {
panic(err.Error())
}
defer getTables.Close()
updateTable := func(postArgs *fasthttp.Args, table string) error {
var err error
var sqlUpdate []string
var primaryKeys []string
var primaryValues []string
//var sqlArgs []string
postArgs.VisitAll(func(key, value []byte) {
val := string(value)
switch string(key) {
case "primary_keys":
primaryKeys = strings.Split(val, ",")
case "primary_values":
primaryValues = strings.Split(val, ",")
default:
if string(value) != "" {
sqlUpdate = append(sqlUpdate, "`"+string(key)+"` = '"+string(value)+"'")
} else {
sqlUpdate = append(sqlUpdate, "`"+string(key)+"` = null")
}
}
})
sql := ""
insert := true
for _, v := range primaryValues {
if v != "" {
insert = false
}
}
if insert {
sql += "INSERT INTO `" + table + "` SET " + strings.Join(sqlUpdate, ", ")
} else {
sql += "UPDATE `" + table + "` SET " + strings.Join(sqlUpdate, ", ") + " WHERE "
var whereRule []string
for i := range primaryKeys {
whereRule = append(whereRule, " `"+primaryKeys[i]+"`='"+primaryValues[i]+"'")
}
sql += strings.Join(whereRule, " AND ")
}
if len(sqlUpdate) > 0 {
fmt.Println(sql)
_, err = db.Query(sql)
}
return err
}
h := func(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, `<html><head><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><style>body{text-align:center} form{margin: 0 auto; width:600px}</style></head><body>`)
path := strings.Split(string(ctx.Path()), "/")
if path[1] == "insert" {
_, err = stmtIns.Exec("http://pupkin.com/", nil, "Pupkin SSP", "Pupkin DSP")
if err != nil {
panic(err.Error())
}
} else if path[1] == "table" {
err := updateTable(ctx.PostArgs(), path[2])
fmt.Fprintf(ctx, "<h3>Form `"+path[2]+"`</h3>")
if err != nil {
fmt.Fprintf(ctx, "<div class='alert alert-danger'>"+err.Error()+"</div>")
}
var f form
page64, err := strconv.ParseInt(path[3], 10, 10)
if err != nil {
panic(err.Error())
}
page := int(page64)
f.loadMySQL(db, path[2], page)
fmt.Fprintf(ctx, f.compile())
if page > 1 {
fmt.Fprintf(ctx, "<a class='btn btn-info' role='button' href='/table/"+path[2]+"/"+strconv.Itoa(page-1)+"'> Prev </a>")
}
fmt.Fprintf(ctx, "<a class='btn btn-info' role='button' href='/table/"+path[2]+"/"+strconv.Itoa(page+1)+"'> Next </a>")
} else if path[1] == "zvit" {
zvit_id, err := strconv.ParseInt(path[2], 10, 64)
if err != nil || zvit_id < 0 || int(zvit_id) >= len(zvits) {
fmt.Fprintf(ctx, "<h3>Unknown zvit!</h3>")
} else {
zvit := &zvits[zvit_id]
fmt.Fprintf(ctx, "<h3>"+zvit.name+"</h3>")
sql := zvit.sql
q, err := db.Prepare(sql)
if err != nil {
panic(err)
}
rows, err := q.Query()
columns, err := rows.Columns()
resp := []interface{}{}
for range columns {
resp = append(resp, new([]byte))
}
fmt.Fprintf(ctx, "<table class='table'><thead><tr><th>"+strings.Join(columns, "</th><th>")+"</th></tr><tbody>")
for rows.Next() {
rows.Scan(resp...)
var vals []string
for _, i := range resp {
vals = append(vals, string(*i.(*[]byte)))
}
fmt.Fprintf(ctx, "<tr><td>"+strings.Join(vals, "</td><td>")+"</td></tr>")
}
fmt.Fprintf(ctx, "</tbody></table>")
}
} else if path[1] == "query" {
query_id, err := strconv.ParseInt(path[2], 10, 64)
if err != nil || query_id < 0 || int(query_id) >= len(queries) {
fmt.Fprintf(ctx, "<h3>Unknown query!</h3>")
} else {
query := &queries[query_id]
showTable := false
ctx.PostArgs().VisitAll(func(key, value []byte) {
_, ok := query.data[string(key)]
if ok {
showTable = true
switch query.data[string(key)].(type) {
case input:
inp := query.data[string(key)].(input)
inp.ivalue = string(value)
query.data[string(key)] = inp
case selectbox:
sb := query.data[string(key)].(selectbox)
sb.iselected = string(value)
query.data[string(key)] = sb
}
}
})
fmt.Fprintf(ctx, "<h3>"+query.name+"</h3>")
fmt.Fprintf(ctx, query.BuildDescription("/query/"+path[2], ""))
if showTable {
sql := query.BuildSql()
fmt.Println(sql)
q, err := db.Prepare(sql)
if err != nil {
panic(err)
}
rows, err := q.Query()
columns, err := rows.Columns()
resp := []interface{}{}
for range columns {
resp = append(resp, new([]byte))
}
fmt.Fprintf(ctx, "<table class='table'><thead><tr><th>"+strings.Join(columns, "</th><th>")+"</th></tr><tbody>")
for rows.Next() {
rows.Scan(resp...)
var vals []string
for _, i := range resp {
vals = append(vals, string(*i.(*[]byte)))
}
fmt.Fprintf(ctx, "<tr><td>"+strings.Join(vals, "</td><td>")+"</td></tr>")
}
fmt.Fprintf(ctx, "</tbody></table>")
}
}
} else {
var table string
tables, err := getTables.Query()
if err != nil {
panic(err.Error())
}
fmt.Fprintf(ctx, "<h3>Forms</h3><div class='list-group'>")
for tables.Next() {
tables.Scan(&table)
fmt.Fprintf(ctx, "<a href='/table/"+table+"/1' class='list-group-item'>"+table+"</a>")
}
fmt.Fprintf(ctx, "</div>")
fmt.Fprintf(ctx, "<h3>Queries</h3><div class='list-group'>")
for i, q := range queries {
fmt.Fprintf(ctx, "<a href='/query/"+strconv.FormatInt(int64(i), 10)+"' class='list-group-item'>"+q.name+"</a>")
}
fmt.Fprintf(ctx, "</div>")
fmt.Fprintf(ctx, "<h3>Zvits</h3><div class='list-group'>")
for i, z := range zvits {
fmt.Fprintf(ctx, "<a href='/zvit/"+strconv.FormatInt(int64(i), 10)+"' class='list-group-item'>"+z.name+"</a>")
}
fmt.Fprintf(ctx, "</div>")
}
fmt.Fprintf(ctx, `</body></html>`)
ctx.SetContentType("text/html; charset=UTF-8")
}
if *compress {
h = fasthttp.CompressHandler(h)
}
if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}