-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
143 lines (128 loc) · 4.15 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 (C) 2021 CGI France
//
// This file is part of LINO.
//
// LINO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LINO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LINO. If not, see <http://www.gnu.org/licenses/>.
package push
import (
"fmt"
"html"
"net/http"
"strconv"
"github.com/cgi-fr/lino/pkg/push"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
func DeleteHandlerFactory(ingressDescriptor string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Delete, ingressDescriptor)
}
}
func InsertHandlerFactory(ingressDescriptor string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Insert, ingressDescriptor)
}
}
func TruncatHandlerFactory(ingressDescriptor string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Handler(w, r, push.Truncate, ingressDescriptor)
}
}
func Handler(w http.ResponseWriter, r *http.Request, mode push.Mode, ingressDescriptor string) {
pathParams := mux.Vars(r)
query := r.URL.Query()
var (
dcDestination string
ok bool
commitSize = uint(100)
disableConstraints bool
)
if dcDestination, ok = pathParams["dataDestination"]; !ok {
log.Error().Msg("param dataDestination is required")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\": \"param dataDestination is required\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
datadestination, err := getDataDestination(dcDestination)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + html.EscapeString(err.Description) + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
plan, e2 := getPlan(idStorageFactory(query.Get("table"), ingressDescriptor))
if e2 != nil {
log.Error().Err(e2).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + e2.Description + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
if query.Get("commitsize") != "" {
commitsize64, ecommitsize := strconv.ParseUint(query.Get("commitsize"), 10, 64)
if ecommitsize != nil {
log.Error().Err(ecommitsize).Msg("can't parse commitsize")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\" : \"param commitsize must be an positive integer\"}\n"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
commitSize = uint(commitsize64)
}
if query.Get("disable-constraints") != "" {
var edisableConstraints error
disableConstraints, edisableConstraints = strconv.ParseBool(query.Get("disable-constraints"))
if edisableConstraints != nil {
log.Error().Err(edisableConstraints).Msg("can't parse disable-constraints")
w.WriteHeader(http.StatusBadRequest)
_, ew := w.Write([]byte("{\"error\" : \"param disable-constraints must be a boolean\"}\n"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
}
log.Debug().Msg(fmt.Sprintf("call Push with mode %s", mode))
e3 := push.Push(rowIteratorFactory(r.Body), datadestination, plan, mode, commitSize, disableConstraints, push.NoErrorCaptureRowWriter{})
if e3 != nil {
log.Error().Err(e3).Msg("")
w.WriteHeader(http.StatusNotFound)
_, ew := w.Write([]byte("{\"error\": \"" + e3.Description + "\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
return
}
_, ew := w.Write([]byte("{\"error\": \"\"}"))
if ew != nil {
log.Error().Err(ew).Msg("Write failed")
return
}
}