Skip to content

Commit 9046078

Browse files
committed
Added processing plugin pipelines functionality
1 parent 27a6e7f commit 9046078

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

main.go

+42-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"database/sql"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"github.com/gophergala2016/dbserver/plugins/jwt"
89
"github.com/julienschmidt/httprouter"
@@ -98,19 +99,22 @@ func handler(api *Api, route *Route, version int) func(http.ResponseWriter, *htt
9899
if api.IsDeprecated(apiVersion) {
99100
w.Header().Set("X-Api-Deprecated", "true")
100101
}
101-
for rows.Next() {
102+
if rows.Next() {
102103
err := rows.Scan(&jsonValue)
103104
if err != nil {
104105
if route.Collection {
105-
fmt.Fprint(w, "[]")
106+
jsonValue = "[]"
106107
} else {
107108
w.WriteHeader(http.StatusNotFound)
108109
}
109110
return
110111
}
112+
}
113+
if len(route.PluginPipelines) > 0 {
114+
goThroughPipelines(api, jsonValue, route.PluginPipelines, w)
115+
} else {
111116
fmt.Fprint(w, jsonValue)
112117
}
113-
114118
}
115119
}
116120

@@ -170,3 +174,38 @@ func main() {
170174
}
171175
log.Fatal(http.ListenAndServe(":"+port, router))
172176
}
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+
}

parser.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ func ParseRoutes(path string) (*Api, error) {
2121
if err != nil {
2222
return nil, err
2323
}
24-
api := &Api{Routes: make([]*Route, 0, 0)}
24+
api := &Api{
25+
Routes: make([]*Route, 0, 0),
26+
Plugins: make(map[string]Plugin),
27+
}
2528
lines := bytes.Split(content, []byte("\n"))
2629
for _, line := range lines {
2730
line = bytes.TrimSpace(line)

0 commit comments

Comments
 (0)