-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (81 loc) · 2.64 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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type key struct {
Value string `json:"value"`
}
var keys = map[string]string{}
func getPing(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "I'm alive!!"})
}
func main() {
router := gin.Default()
router.GET("/ping", getPing)
router.POST("/ping", postPing)
router.POST("/ping/:name", postPingByName)
router.GET("/ping/:name", getPingByName)
router.POST("/echo", postEcho)
router.GET("/echo", getEcho)
router.PUT("/key-value-store/:key", postValue)
router.GET("/key-value-store/:key", getValue)
router.DELETE("/key-value-store/:key", deleteValue)
router.Run(":8085")
}
func deleteValue(c *gin.Context) {
key := c.Param("key")
if _, exists := keys[key]; !exists {
c.JSON(http.StatusNotFound, gin.H{"doesExist": false, "error": "Key does not exist", "message": "Error in DELETE"})
} else {
delete(keys, key)
c.JSON(http.StatusOK, gin.H{"doesExist": true, "message": "Deleted successfully"})
}
}
func getValue(c *gin.Context) {
key := c.Param("key")
if _, exists := keys[key]; !exists {
c.JSON(http.StatusNotFound, gin.H{"doesExist": false, "error": "Key does not exist", "message": "Error in GET"})
} else {
value := keys[key]
c.JSON(http.StatusOK, gin.H{"doesExist": true, "message": "Retrieved successfully", "value": value})
}
}
func postValue(c *gin.Context) {
var newKey key
param := c.Param("key")
if err := c.BindJSON(&newKey); err != nil {
return
} else if len(param) > 50 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Key is too long", "message": "Error in PUT"})
} else if newKey.Value == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Value is missing", "message": "Error in PUT"})
} else if _, exists := keys[param]; exists {
keys[param] = newKey.Value
c.JSON(http.StatusOK, gin.H{"message": "Updated successfully", "replaced": true})
} else {
keys[param] = newKey.Value
c.JSON(http.StatusCreated, gin.H{"message": "Added successfully", "replaced": false})
}
}
func getEcho(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Get Message Received"})
}
func postEcho(c *gin.Context) {
msg := c.Query("msg")
if msg == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Bad Request"})
} else {
c.JSON(http.StatusOK, gin.H{"message": msg})
}
}
func getPingByName(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{"message": "Method Not Allowed"})
}
func postPingByName(c *gin.Context) {
name := c.Param("name")
c.JSON(http.StatusOK, gin.H{"message": "I'm alive, " + name + "!!"})
}
func postPing(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{"message": "Method Not Allowed"})
}