-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
275 lines (213 loc) · 6.36 KB
/
web.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// web.go - database in go
/*
* This file web.go is part of L1VMgodata.
*
* (c) Copyright Stefan Pietzonke ([email protected]), 2022
*
* L1VMgodata is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L1VMgodata is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with L1VMgodata. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strconv"
)
func send_form_head(w http.ResponseWriter) {
fmt.Fprintf(w, "<!DOCTYPE html>")
fmt.Fprintf(w, "<html>")
fmt.Fprintf(w, "<head>")
fmt.Fprintf(w, "<meta charset=\"UTF-8\" />")
fmt.Fprintf(w, "<title>L1VMgodata - web</title>")
fmt.Fprintf(w, "</head>")
fmt.Fprintf(w, "<body>")
fmt.Fprintf(w, "<div>")
fmt.Fprintf(w, "<h2>L1VMgodata - web</h2>")
fmt.Fprintf(w, "<br>")
fmt.Fprintf(w, "<form method=\"POST\" action=\"/\">")
fmt.Fprintf(w, "<label>command</label><input name=\"command\" type=\"text\" value=\"\"/> ")
fmt.Fprintf(w, "<label>key</label><input name=\"key\" type=\"text\" value=\"\"/> ")
fmt.Fprintf(w, "<label>value</label><input name=\"value\" type=\"text\" value=\"\"/> ")
fmt.Fprintf(w, "<input type=\"submit\" value=\"submit\"/>")
fmt.Fprintf(w, "</form>")
fmt.Fprintf(w, "<br>")
fmt.Fprintf(w, "<br>")
}
func send_form_end(w http.ResponseWriter) {
fmt.Fprintf(w, "</div>")
fmt.Fprintf(w, "</body>")
fmt.Fprintf(w, "</html>")
}
func parse_web(w http.ResponseWriter, command string, key string, value string) {
var key_ret string
var value_ret string
var used_elements uint64
var linkslen uint64
var retstr string
var linkindex uint64
switch command {
case STORE_DATA:
send_form_head(w)
if store_data(key, value) != 0 {
fmt.Fprintf(w, "ERROR can't store data!\n")
} else {
fmt.Fprintf(w, "data stored!\n")
}
send_form_end(w)
case GET_DATA_KEY:
send_form_head(w)
value_ret = get_data_key(key)
fmt.Fprintf(w, "key: %s, value: %s\n", key, value_ret)
send_form_end(w)
case GET_DATA_VALUE:
send_form_head(w)
key_ret = get_data_value(value)
fmt.Fprintf(w, "key: %s, value: %s\n", key_ret, value)
send_form_end(w)
case REMOVE_DATA:
send_form_head(w)
value_ret = remove_data(key)
fmt.Fprintf(w, "%s\n", value_ret)
send_form_end(w)
case GET_DATA_REGEXP_KEY:
send_form_head(w)
value_ret = get_data_key_regexp(key)
fmt.Fprintf(w, "%s\n", value_ret)
send_form_end(w)
case GET_DATA_REGEXP_VALUE:
send_form_head(w)
key_ret = get_data_value_regexp(value)
fmt.Fprintf(w, "%s\n", key_ret)
send_form_end(w)
case SAVE_DATA:
send_form_head(w)
if save_data(value) != 0 {
fmt.Fprintf(w, "ERROR can't save database %s !\n", value)
} else {
fmt.Fprintf(w, "database %s saved!\n", value)
}
send_form_end(w)
case LOAD_DATA:
send_form_head(w)
if load_data(value) != 0 {
fmt.Fprintf(w, "ERROR can't load database %s !\n", value)
} else {
fmt.Fprintf(w, "database %s loaded!\n", value)
}
send_form_end(w)
case SAVE_DATA_JSON:
send_form_head(w)
if save_data_json(value) != 0 {
fmt.Fprintf(w, "ERROR can't save JSON database %s !\n", value)
} else {
fmt.Fprintf(w, "JSON database %s saved!\n", value)
}
send_form_end(w)
case LOAD_DATA_JSON:
send_form_head(w)
if load_data_json(value) != 0 {
fmt.Fprintf(w, "ERROR can't load JSON database %s !\n", value)
} else {
fmt.Fprintf(w, "JSON database %s loaded!\n", value)
}
send_form_end(w)
case ERASE_DATA:
send_form_head(w)
init_data()
fmt.Fprintf(w, "ALL DATA ERASED!\n")
send_form_end(w)
case GET_USED_ELEMENTS:
send_form_head(w)
used_elements = get_used_elements()
fmt.Fprintf(w, "usage: %d of %d\n", used_elements, maxdata)
send_form_end(w)
case SET_LINK:
send_form_head(w)
if set_link(key, value) != 0 {
fmt.Fprintf(w, "ERROR can't set link %s !\n", value)
} else {
fmt.Fprintf(w, "link set!\n")
}
send_form_end(w)
case REMOVE_LINK:
send_form_head(w)
if remove_link(key, value) != 0 {
fmt.Fprintf(w, "ERROR can't remove link %s !\n", value)
} else {
fmt.Fprintf(w, "link removed!\n")
}
send_form_end(w)
case GET_LINKS_NUMBER:
send_form_head(w)
linkslen, retstr = get_number_of_links(key)
if retstr == "" {
fmt.Fprintf(w, "ERROR can't get links number!\n")
} else {
fmt.Fprintf(w, "links: %d\n", linkslen)
}
send_form_end(w)
case GET_LINK_NAME:
send_form_head(w)
linkindex, _ = strconv.ParseUint(value, 10, 64)
retstr = get_link(key, linkindex)
if retstr == "" {
fmt.Fprintf(w, "ERROR can't get links name!\n")
} else {
fmt.Fprintf(w, "link name: %s\n", retstr)
}
send_form_end(w)
default:
send_form_head(w)
fmt.Fprintf(w, "ERROR! UNKNOWN COMMAND!")
send_form_end(w)
}
}
func hello(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
http.ServeFile(w, r, "form.html")
case "POST":
// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
//fmt.Fprintf(w, "Post from website! r.PostFrom = %v\n", r.PostForm)
key := r.FormValue("key")
value := r.FormValue("value")
command := r.FormValue("command")
//fmt.Fprintf(w, "key = %s\n", key)
//fmt.Fprintf(w, "value = %s\n", value)
//fmt.Fprintf(w, "command = %s\n", command)
parse_web(w, command, key, value)
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func handle_http_request() {
fmt.Println("Web Listening on " + server_host + ":" + server_http_port)
fmt.Println("Waiting for client...")
port := flag.String("p", server_http_port, "port to serve on")
// directory := flag.String("d", "./statics", "the directory of static file to host")
flag.Parse()
// http.Handle("/", http.FileServer(http.Dir(*directory)))
http.HandleFunc("/", hello)
// log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}