/
Lab3Server1.go
97 lines (77 loc) · 2 KB
/
Lab3Server1.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
package main
import (
"encoding/json"
"fmt"
"httprouter"
"net/http"
"strconv"
)
//MyController structure
type MyController struct{}
//NewMyController function
func NewMyController() *MyController {
return &MyController{}
}
//DataCache struct
type DataCache struct {
DataCachekey int `json:"key"`
DataCachevalue string `json:"value"`
}
var myMap = make(map[int]string)
func main() {
// Instantiate a new router
router := httprouter.New()
// Get a controller instance
mycontroller := NewMyController()
// Add handlers
router.GET("/keys/:id", mycontroller.GetKey)
router.GET("/keys", mycontroller.GetAllKeys)
router.PUT("/keys/:id/:value", mycontroller.PutKey)
// Expose the server at port 3000
http.ListenAndServe(":3000", router)
}
//GetKey to display single key value
func (uc MyController) GetKey(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
eid, _ := strconv.Atoi(id)
var key int
var val string
fmt.Println("in GET", myMap)
for key, val = range myMap {
if key == eid {
val = myMap[key]
break
}
}
uj := DataCache{
DataCachekey: key,
DataCachevalue: val,
}
result, _ := json.Marshal(uj)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", result)
}
//GetAllKeys to display all the key values
func (uc MyController) GetAllKeys(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
type data []DataCache
var dataArray = make(data, 0)
for key, val := range myMap {
uj := DataCache{key, val}
dataArray = append(dataArray, uj)
}
result, _ := json.Marshal(dataArray)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", result)
}
//PutKey to insert value in a map
func (uc MyController) PutKey(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
val := p.ByName("value")
eid, _ := strconv.Atoi(id)
myMap[eid] = val
fmt.Println(myMap)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
}