-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb.go
More file actions
56 lines (52 loc) · 1.85 KB
/
web.go
File metadata and controls
56 lines (52 loc) · 1.85 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
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
)
type infoMsg struct {
Type string `json:"type"`
Information string `json:"information"`
}
func ret2web(w http.ResponseWriter, r *http.Request, itype string, info string) {
einfo := infoMsg{Type: itype, Information: info}
bJSON, err := json.MarshalIndent(einfo, "", " ")
if err != nil {
log.Println("error:", err)
}
// ======================================== CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, STORE, LOAD, DUMP")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
// ================================= set as json
w.Header().Set("Content-type", "application/json")
// ============================= return the data
w.Write(bJSON)
}
func handleRequests(w http.ResponseWriter, r *http.Request) {
parms := strings.Split(r.URL.Path[1:], "/")
if r.Method == "GET" && len(parms) > 1 && len(parms[0]) > 0 && len(parms[1]) > 0 {
ret2web(w, r, "result", d.Get(parms[0], parms[1]))
} else if r.Method == "POST" && len(parms) > 2 && len(parms[0]) > 0 && len(parms[1]) > 0 && len(parms[2]) > 0 {
d.Set(parms[0], parms[1], parms[2])
ret2web(w, r, "result", "ok")
} else if r.Method == "DELETE" && len(parms) > 1 && len(parms[0]) > 0 && len(parms[1]) > 0 {
d.Del(parms[0], parms[1])
ret2web(w, r, "result", "ok")
} else if r.Method == "STORE" {
d.Store()
ret2web(w, r, "result", "ok")
} else if r.Method == "LOAD" {
d.Load()
ret2web(w, r, "result", "ok")
} else if r.Method == "DUMP" {
w.Header().Set("Content-type", "application/json")
w.Write([]byte(d.Dump()))
} else if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
} else {
ret2web(w, r, "error", "Not Implemented yet")
}
}