forked from VKCOM/kittenhouse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (113 loc) · 3.19 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
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
package main
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/valyala/fasthttp"
"github.com/vkcom/kittenhouse/core/clickhouse"
"github.com/vkcom/kittenhouse/core/cmd"
"github.com/vkcom/kittenhouse/core/inmem"
"github.com/vkcom/kittenhouse/core/persist"
)
const (
defaultTimeout = 30 * time.Second
)
var (
// query (GET)
queryKeyBytes = []byte("query") // SQL query in form 'SELECT ... FROM ...'
queryKeyTimeout = []byte("timeout") // timeout, in seconds
// insert (POST)
queryKeyTable = []byte("table") // table name with columns, e.g. table(a,b,c)
queryKeyDebug = []byte("debug") // set debug=1 to do INSERT synchronously
queryKeyPersistent = []byte("persistent") // set persistent=1 if you need to write data to disk (in-memory otherwise)
queryKeyRowBinary = []byte("rowbinary") // set rowbinary=1 if you send data in RowBinary format instead of VALUES
)
func handleGET(ctx *fasthttp.RequestCtx) {
var query string
timeout := defaultTimeout
args := ctx.QueryArgs()
if queryBytes := args.PeekBytes(queryKeyBytes); queryBytes != nil {
query = string(queryBytes)
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.WriteString("GET-parameter `query` is missing")
return
}
if t, err := args.GetUfloat("timeout"); err == nil {
timeout = time.Duration(t * float64(time.Second))
}
httpCode, res, err := clickhouse.QueryDeadline(time.Now().Add(timeout), query)
if err != nil {
ctx.SetStatusCode(500)
ctx.WriteString(err.Error())
} else {
ctx.SetStatusCode(httpCode)
ctx.Write(res)
}
}
func handlePOST(ctx *fasthttp.RequestCtx) {
var table string
args := ctx.QueryArgs()
if tableBytes := args.PeekBytes(queryKeyTable); tableBytes != nil {
table = string(tableBytes)
} else {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.WriteString("GET-parameter `table` is missing")
return
}
data := ctx.PostBody()
rowbinary := args.GetUintOrZero("rowbinary") == 1
if args.GetUintOrZero("debug") == 1 {
tableWithColumns := string(table)
tableClean := tableWithColumns
if idx := strings.IndexByte(tableClean, '('); idx >= 0 {
tableClean = tableWithColumns[0:idx]
}
err := clickhouse.Flush(
clickhouse.GetDestinationSetting(strings.TrimSpace(tableClean)),
tableWithColumns,
bytes.NewReader(data),
rowbinary,
)
if err != nil {
ctx.SetStatusCode(500)
ctx.WriteString(err.Error())
return
}
} else if args.GetUintOrZero("persistent") == 1 {
if err := persist.Write(string(table), data, rowbinary); err != nil {
ctx.SetStatusCode(500)
ctx.WriteString(err.Error())
return
}
} else {
if err := inmem.Write(string(table), data, rowbinary); err != nil {
ctx.SetStatusCode(500)
ctx.WriteString(err.Error())
return
}
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
if ctx.IsPost() {
handlePOST(ctx)
return
}
if ctx.IsGet() {
handleGET(ctx)
return
}
ctx.Response.Header.SetStatusCode(fasthttp.StatusMethodNotAllowed)
return
}
func main() {
cmd.StartServerCallback = func(host string, port uint) error {
srv := &fasthttp.Server{
MaxRequestBodySize: 16 << 20,
Handler: requestHandler,
}
return srv.ListenAndServe(fmt.Sprintf("%s:%d", host, port))
}
cmd.Main()
}