-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathhttp.go
143 lines (122 loc) · 2.95 KB
/
http.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
// Copyright 2016 Eleme. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"compress/gzip"
"io/ioutil"
"log"
"net/http"
"net/http/pprof"
"strings"
"github.com/shell909090/influx-proxy/backend"
)
type HttpService struct {
db string
ic *backend.InfluxCluster
}
func NewHttpService(ic *backend.InfluxCluster, db string) (hs *HttpService) {
hs = &HttpService{
db: db,
ic: ic,
}
if hs.db != "" {
log.Print("http database: ", hs.db)
}
return
}
func (hs *HttpService) Register(mux *http.ServeMux) {
mux.HandleFunc("/reload", hs.HandlerReload)
mux.HandleFunc("/ping", hs.HandlerPing)
mux.HandleFunc("/query", hs.HandlerQuery)
mux.HandleFunc("/write", hs.HandlerWrite)
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
}
func (hs *HttpService) HandlerReload(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
w.Header().Add("X-Influxdb-Version", backend.VERSION)
err := hs.ic.LoadConfig()
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(204)
return
}
func (hs *HttpService) HandlerPing(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
version, err := hs.ic.Ping()
if err != nil {
panic("WTF")
return
}
w.Header().Add("X-Influxdb-Version", version)
w.WriteHeader(204)
return
}
func (hs *HttpService) HandlerQuery(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
w.Header().Add("X-Influxdb-Version", backend.VERSION)
db := req.FormValue("db")
if hs.db != "" {
if db != hs.db {
w.WriteHeader(404)
w.Write([]byte("database not exist."))
return
}
}
q := strings.TrimSpace(req.FormValue("q"))
err := hs.ic.Query(w, req)
if err != nil {
log.Printf("query error: %s,the query is %s,the client is %s\n", err, q, req.RemoteAddr)
return
}
if hs.ic.QueryTracing != 0 {
log.Printf("the query is %s,the client is %s\n", q, req.RemoteAddr)
}
return
}
func (hs *HttpService) HandlerWrite(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
w.Header().Add("X-Influxdb-Version", backend.VERSION)
if req.Method != "POST" {
w.WriteHeader(405)
w.Write([]byte("method not allow."))
return
}
db := req.URL.Query().Get("db")
if hs.db != "" {
if db != hs.db {
w.WriteHeader(404)
w.Write([]byte("database not exist."))
return
}
}
body := req.Body
if req.Header.Get("Content-Encoding") == "gzip" {
b, err := gzip.NewReader(req.Body)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("unable to decode gzip body"))
return
}
defer b.Close()
body = b
}
p, err := ioutil.ReadAll(body)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
err = hs.ic.Write(p)
if err == nil {
w.WriteHeader(204)
}
if hs.ic.WriteTracing != 0 {
log.Printf("Write body received by handler: %s,the client is %s\n", p, req.RemoteAddr)
}
return
}