-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 1.72 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
package main
import (
"encoding/json"
"net/http"
gVS "github.com/frankegoesdown/grafana-vertica-plugin/internal/app/grafana-vertica-service-server"
"github.com/go-chi/chi"
_ "github.com/vertica/vertica-sql-go"
)
func main() {
ConnectionString := ""
r := chi.NewRouter()
r.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Write([]byte("foo"))
}))
r.Get("/zip", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "applicaiton/zip")
w.Header().Set("Content-Disposition", "attachment; filename='grafana-vertica-datasource.zip'")
http.ServeFile(w, r, "./bin/grafana-vertica-datasource.zip")
}))
r.Post("/search", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
mapD := []string{"apple", "lettuce"}
mapB, _ := json.Marshal(mapD)
w.Write(mapB)
}))
r.Post("/query", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
qr := gVS.QueryRequest{}
err := json.NewDecoder(r.Body).Decode(&qr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
isTableQuery := false
for _, q := range qr.Targets {
if q.Type == "table" {
isTableQuery = true
}
}
if isTableQuery {
resp, err := gVS.TableResponse(ConnectionString, qr)
mapB, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(mapB)
} else {
resp, err := gVS.SeriesResponse(ConnectionString, qr)
mapB, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(mapB)
}
}))
http.ListenAndServe(":7000", r)
}