api: instroduce /add_schema
diff --git a/controller/main.go b/controller/main.go
index 2bba060..9c3cd4b 100644
--- a/controller/main.go
+++ b/controller/main.go
@@ -58,7 +58,7 @@
}
}
-func (a *ApiHandler) graphqlHandler(w http.ResponseWriter, r *http.Request) {
+func (a *ApiHandler) graphql(w http.ResponseWriter, r *http.Request) {
glog.Infof("New GraphQL query received: %s", r.Method)
q, err := extractQuery(r)
if err != nil {
@@ -75,6 +75,23 @@
w.Header().Set("Content-Type", "application/json")
}
+func (a *ApiHandler) addSchema(w http.ResponseWriter, r *http.Request) {
+ if r.Method != "POST" {
+ http.Error(w, "Only POST requests are accepted in /add_schema", http.StatusBadRequest)
+ return
+ }
+ body, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Could not read request", http.StatusInternalServerError)
+ return
+ }
+ err = a.gql.AddSchema(string(body))
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusPreconditionFailed)
+ return
+ }
+}
+
func main() {
flag.Parse()
@@ -116,6 +133,7 @@
panic(err)
}
api := ApiHandler{gqlClient}
- http.HandleFunc("/graphql", api.graphqlHandler)
+ http.HandleFunc("/graphql", api.graphql)
+ http.HandleFunc("/add_schema", api.addSchema)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}