@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"database/sql"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"github.com/gophergala2016/dbserver/plugins/jwt"
8
9
"github.com/julienschmidt/httprouter"
@@ -98,19 +99,22 @@ func handler(api *Api, route *Route, version int) func(http.ResponseWriter, *htt
98
99
if api .IsDeprecated (apiVersion ) {
99
100
w .Header ().Set ("X-Api-Deprecated" , "true" )
100
101
}
101
- for rows .Next () {
102
+ if rows .Next () {
102
103
err := rows .Scan (& jsonValue )
103
104
if err != nil {
104
105
if route .Collection {
105
- fmt . Fprint ( w , "[]" )
106
+ jsonValue = "[]"
106
107
} else {
107
108
w .WriteHeader (http .StatusNotFound )
108
109
}
109
110
return
110
111
}
112
+ }
113
+ if len (route .PluginPipelines ) > 0 {
114
+ goThroughPipelines (api , jsonValue , route .PluginPipelines , w )
115
+ } else {
111
116
fmt .Fprint (w , jsonValue )
112
117
}
113
-
114
118
}
115
119
}
116
120
@@ -170,3 +174,38 @@ func main() {
170
174
}
171
175
log .Fatal (http .ListenAndServe (":" + port , router ))
172
176
}
177
+
178
+ func goThroughPipelines (api * Api ,
179
+ jsonValue string ,
180
+ pluginPipelines []* PluginPipeline ,
181
+ w http.ResponseWriter ) error {
182
+
183
+ data := make (map [string ]interface {})
184
+ err := json .Unmarshal ([]byte (jsonValue ), & data )
185
+ if err != nil {
186
+ return err
187
+ }
188
+ for _ , pp := range pluginPipelines {
189
+ plugin := api .GetPlugin (pp .Name )
190
+ if plugin == nil {
191
+ return errors .New (fmt .Sprintf ("Plugin missing: %v" , pp .Name ))
192
+ }
193
+ response := plugin .Process (data , pp .Argument )
194
+ if response .Headers != nil {
195
+ for name , values := range response .Headers {
196
+ for _ , value := range values {
197
+ w .Header ().Set (name , value )
198
+ }
199
+ }
200
+ }
201
+ if response .ResponseCode != 0 {
202
+ w .WriteHeader (response .ResponseCode )
203
+ if response .Error != "" {
204
+ fmt .Fprint (w , response .Error )
205
+ }
206
+ return nil
207
+ }
208
+ data = response .Data
209
+ }
210
+ return nil
211
+ }
0 commit comments