-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
35 lines (31 loc) · 827 Bytes
/
exec.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
package server
import (
"encoding/json"
"net/http"
)
// Exec provides execution of commands
func (s *Server) Exec(w http.ResponseWriter, r *http.Request) {
query, ok := r.URL.Query()["query"]
if !ok || len(query[0]) < 1 {
http.Error(w, "query attribute is missed", http.StatusBadRequest)
return
}
command, ok := r.URL.Query()["command"]
if !ok || len(command[0]) < 1 {
http.Error(w, "command attribute is missed", http.StatusBadRequest)
return
}
response, err := s.a.Exec(query[0], command[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}