Skip to content

Commit 87c1154

Browse files
committed
Update DelChain
1 parent 1cc379f commit 87c1154

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/controllers/chain.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,51 @@ func AddChain(c *gin.Context) {
3232

3333
// DelChain DELETE /chain/{table}/{name}/
3434
func DelChain(c *gin.Context) {
35-
w := c.Writer
3635

3736
if !checkRole(c) {
37+
c.AbortWithStatusJSON(http.StatusUnauthorized, forms.BasicResponse{
38+
Ok: false,
39+
Message: "",
40+
})
3841
return
3942
}
4043

4144
ipt, err := iptables.New()
4245
if err != nil {
43-
http.Error(w, err.Error(), 500)
46+
c.AbortWithStatusJSON(http.StatusInternalServerError, forms.BasicResponse{
47+
Ok: false,
48+
Message: err.Error(),
49+
})
4450
return
4551
}
4652
// Clear chain before delete
47-
respErr = ipt.ClearChain(c.Param("table"), c.Param("name"))
48-
if respErr != nil {
49-
w.WriteHeader(http.StatusBadRequest)
50-
fmt.Fprintln(w, respErr)
53+
err = ipt.ClearChain(c.Param("table"), c.Param("name"))
54+
if err != nil {
55+
c.AbortWithStatusJSON(http.StatusBadRequest, forms.BasicResponse{
56+
Ok: false,
57+
Message: err.Error(),
58+
})
59+
return
5160
}
5261
// Delete chain
53-
respErr = ipt.DeleteChain(c.Param("table"), c.Param("name"))
62+
err = ipt.DeleteChain(c.Param("table"), c.Param("name"))
5463
if respErr != nil {
55-
w.WriteHeader(http.StatusBadRequest)
56-
fmt.Fprintln(w, respErr)
64+
c.JSON(http.StatusInternalServerError, forms.ChainListResponse{
65+
Ok: false,
66+
Message: err.Error(),
67+
})
5768
}
69+
70+
c.JSON(http.StatusOK, forms.ChainListResponse{
71+
Ok: true,
72+
Message: "",
73+
})
5874
}
5975

6076
// ListChain GET /chain/{table}/{name}/
6177
func ListChain(c *gin.Context) {
6278
if !checkRole(c) {
63-
c.AbortWithStatusJSON(http.StatusUnauthorized, forms.BasicResponse{
79+
c.AbortWithStatusJSON(http.StatusUnauthorized, forms.ChainListResponse{
6480
Ok: false,
6581
Message: "",
6682
})
@@ -69,15 +85,15 @@ func ListChain(c *gin.Context) {
6985

7086
ipt, err := iptables.New()
7187
if err != nil {
72-
c.AbortWithStatusJSON(http.StatusInternalServerError, forms.BasicResponse{
88+
c.AbortWithStatusJSON(http.StatusInternalServerError, forms.ChainListResponse{
7389
Ok: false,
7490
Message: err.Error(),
7591
})
7692
return
7793
}
7894
respStr, err := ipt.List(c.Param("table"), c.Param("name"))
7995
if err != nil {
80-
c.AbortWithStatusJSON(http.StatusInternalServerError, forms.BasicResponse{
96+
c.AbortWithStatusJSON(http.StatusInternalServerError, forms.ChainListResponse{
8197
Ok: false,
8298
Message: err.Error(),
8399
})

0 commit comments

Comments
 (0)