This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
117 lines (98 loc) · 3.4 KB
/
manager.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/couchbase/gocb"
)
func manager(w http.ResponseWriter, r *http.Request) {
Logger, _ := initLogger()
type Cas gocb.Cas
switch r.Method {
case "GET":
var document inventory
doc := r.URL.Path[len("/manager/"):]
_, err := bucket.Get(doc, &document)
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusNotFound)).Inc()
Logger.Errorf("GET: Failed getting: %s", err)
fmt.Fprintf(w, "GET: Failed getting: %s \n", err)
return
} else {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
}
jsonDocument, err := json.Marshal(&document)
if err != nil {
Logger.Errorf("GET: Can`t marshal: %s", err)
fmt.Fprintf(w, "GET: Can`t marshal: %s \n", err)
}
fmt.Fprintf(w, "%s\n", string(jsonDocument))
case "POST":
var result inventory
doc := r.URL.Path[len("/manager/"):]
body, err := ioutil.ReadAll(r.Body)
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusBadRequest)).Inc()
Logger.Errorf("Incorrect body request: %s", err)
fmt.Fprintf(w, "Incorrect body request: %s \n", err)
}
err = json.Unmarshal(body, &result)
if err != nil {
Logger.Errorf("POST: Can't unmarshal: %s", err)
fmt.Fprintf(w, "POST: Can't unmarshal: %s \n", err)
} else {
_, err = bucket.Upsert(doc, result, 0)
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusNotFound)).Inc()
Logger.Debugf("POST: Can't upsert: %s", err)
fmt.Fprintf(w, "POST: Can't upsert: %s \n", err)
} else {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusCreated)).Inc()
}
}
case "DELETE":
doc := r.URL.Path[len("/manager/"):]
_, err := bucket.Remove(doc, 0)
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusNotFound)).Inc()
Logger.Errorf("DELETE: Deleting failed: %s", err)
fmt.Fprintf(w, "DELETE: Deleting failed: %s \n", err)
} else {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
}
case "UPDATE":
doc := r.URL.Path[len("/manager/"):]
var document inventory
cas, err := bucket.GetAndLock(doc, 10, &document) //TODO: set time lock
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusNotFound)).Inc()
Logger.Errorf("UPDATE: Failed getting and locking: %s", err)
fmt.Fprintf(w, "UPDATE: Failed getting and locking: %s \n", err)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
Logger.Errorf("UPDATE: Incorrect body request: %s", err)
fmt.Fprintf(w, "UPDATE: Incorrect body request: %s \n", err)
}
err = json.Unmarshal(body, &document)
if err != nil {
Logger.Errorf("UPDATE: Can't unmarshal: %s", err)
fmt.Fprintf(w, "UPDATE: Can't unmarshal: %s \n", err)
}
cas, err = bucket.Replace(doc, &document, cas, 0)
if err != nil {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusNotFound)).Inc()
Logger.Errorf("UPDATE: Failed replace document: %s", err)
fmt.Fprintf(w, "UPDATE: Failed replace document: %s \n", err)
} else {
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusOK)).Inc()
}
bucket.Unlock(doc, cas)
default:
totalRequestHttp.WithLabelValues(strconv.Itoa(http.StatusMethodNotAllowed)).Inc()
Logger.Errorf("DEFAULT MANAGER: Incorrect method!")
fmt.Println("Error: %s", "\"", r.Method, "\"", " - unknown method. Using GET, POST, DELETE, UPDATE method.\n")
}
}