-
Notifications
You must be signed in to change notification settings - Fork 18
/
handler.go
150 lines (134 loc) · 3.84 KB
/
handler.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package route
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/prashantgupta24/firewalld-rest/firewallcmd"
"github.com/prashantgupta24/firewalld-rest/ip"
)
//Index page
// GET /
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome!\n")
}
// IPAdd for the Create action
// POST /ip
func IPAdd(w http.ResponseWriter, r *http.Request) {
ipInstance := &ip.Instance{}
if err := populateModelFromHandler(w, r, ipInstance); err != nil {
writeErrorResponse(w, http.StatusUnprocessableEntity, "Unprocessible Entity")
return
}
ipExists, err := ip.GetHandler().CheckIPExists(ipInstance.IP)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if ipExists {
writeErrorResponse(w, http.StatusBadRequest, "ip already exists")
return
}
env := os.Getenv("env")
if env != "local" {
command, err := firewallcmd.EnableRichRuleForIP(ipInstance.IP)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("cannot exec command %v, err : %v", command, err.Error()))
return
}
}
err = ip.GetHandler().AddIP(ipInstance)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
writeOKResponse(w, ipInstance)
}
// IPShow for the ip Show action
// GET /ip/{ip}
func IPShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ipAddr := vars["ip"]
ip, err := ip.GetHandler().GetIP(ipAddr)
if err != nil {
// No IP found
writeErrorResponse(w, http.StatusNotFound, err.Error())
return
}
writeOKResponse(w, ip)
}
// ShowAllIPs shows all IPs
// GET /ip
func ShowAllIPs(w http.ResponseWriter, r *http.Request) {
ips, err := ip.GetHandler().GetAllIPs()
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
writeOKResponse(w, ips)
}
// IPDelete for the ip Delete action
// DELETE /ip/{ip}
func IPDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ipAddr := vars["ip"]
log.Printf("IP to delete %s\n", ipAddr)
ipExists, err := ip.GetHandler().CheckIPExists(ipAddr)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if !ipExists {
writeErrorResponse(w, http.StatusNotFound, "ip does not exist")
return
}
env := os.Getenv("env")
if env != "local" {
command, err := firewallcmd.DisableRichRuleForIP(ipAddr)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("cannot exec command %v, err : %v", command, err.Error()))
return
}
}
ip, err := ip.GetHandler().DeleteIP(ipAddr)
if err != nil {
// IP could not be deleted
writeErrorResponse(w, http.StatusNotFound, err.Error())
return
}
writeOKResponse(w, ip)
}
// Writes the response as a standard JSON response with StatusOK
func writeOKResponse(w http.ResponseWriter, m interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(&JSONResponse{Data: m}); err != nil {
writeErrorResponse(w, http.StatusInternalServerError, "Internal Server Error")
}
}
// Writes the error response as a Standard API JSON response with a response code
func writeErrorResponse(w http.ResponseWriter, errorCode int, errorMsg string) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(errorCode)
json.
NewEncoder(w).
Encode(&JSONErrorResponse{Error: &APIError{Status: errorCode, Title: errorMsg}})
}
//Populates a ip from the params in the Handler
func populateModelFromHandler(w http.ResponseWriter, r *http.Request, ip interface{}) error {
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
return err
}
if err := r.Body.Close(); err != nil {
return err
}
if err := json.Unmarshal(body, ip); err != nil {
return err
}
return nil
}