move minio webhook into new photostorage app
diff --git a/controller/cmd/main.go b/controller/cmd/main.go
new file mode 100644
index 0000000..af2e019
--- /dev/null
+++ b/controller/cmd/main.go
@@ -0,0 +1,89 @@
+package main
+
+import (
+	"bytes"
+	"encoding/json"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+
+	"github.com/golang/glog"
+	"github.com/itaysk/regogo"
+)
+
+var port = flag.Int("port", 123, "Port to listen on.")
+var apiAddr = flag.String("api_addr", "", "PCloud GraphQL API server address.")
+
+var jsonContentType = "application/json"
+
+var addImgTmpl = `
+mutation {
+  addImage(input: [%s]) {
+    image {
+      id
+    }
+  }
+}`
+
+type image struct {
+	ObjectPath string
+}
+
+type query struct {
+	Query string
+}
+
+func minioHandler(w http.ResponseWriter, r *http.Request) {
+	body, err := ioutil.ReadAll(r.Body)
+	if err != nil {
+		glog.Error(err)
+		http.Error(w, "Could not read HTTP request body", http.StatusInternalServerError)
+		return
+	}
+	if len(body) == 0 {
+		// Just a health check from Minio
+		return
+	}
+	bodyStr := string(body)
+	glog.Infof("Received event from Minio: %s", bodyStr)
+	key, err := regogo.Get(bodyStr, "input.Key")
+	if err != nil {
+		glog.Error(err)
+		http.Error(w, "Could not find object key", http.StatusBadRequest)
+		return
+	}
+	img := image{key.String()}
+	imgJson, err := json.Marshal(img)
+	if err != nil {
+		panic(err)
+	}
+	q := query{fmt.Sprintf(addImgTmpl, imgJson)}
+	glog.Info(q)
+	queryJson, err := json.Marshal(q)
+	if err != nil {
+		panic(err)
+	}
+	resp, err := http.Post(
+		*apiAddr,
+		jsonContentType,
+		bytes.NewReader(queryJson))
+	if err != nil {
+		glog.Error(err)
+		http.Error(w, "Query failed", http.StatusInternalServerError)
+		return
+	}
+	respBody, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		panic(err)
+	}
+	glog.Info(string(respBody))
+}
+
+func main() {
+	flag.Parse()
+
+	http.HandleFunc("/minio_webhook", minioHandler)
+	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
+}
diff --git a/controller/controller b/controller/controller
new file mode 100755
index 0000000..f2995db
--- /dev/null
+++ b/controller/controller
Binary files differ
diff --git a/controller/go.mod b/controller/go.mod
index 955f4b8..c372a57 100644
--- a/controller/go.mod
+++ b/controller/go.mod
@@ -7,5 +7,7 @@
 	github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
 	github.com/itaysk/regogo v0.0.0-20200418072509-74b59e1875c2
 	github.com/vektah/gqlparser v1.3.1
+	k8s.io/api v0.18.2
+	k8s.io/apimachinery v0.18.2
 	k8s.io/client-go v0.18.2
 )
diff --git a/controller/main.go b/controller/main.go
index 70e4af0..11498ad 100644
--- a/controller/main.go
+++ b/controller/main.go
@@ -39,30 +39,8 @@
 }
 
 func (m *MinioWebhook) minioHandler(w http.ResponseWriter, r *http.Request) {
-	body, err := ioutil.ReadAll(r.Body)
-	if err != nil {
-		glog.Error(err)
-		http.Error(w, "Could not read HTTP request body", http.StatusInternalServerError)
-		return
-	}
-	if len(body) == 0 {
-		return
-	}
-	glog.Infof("Received event from Minio: %s", string(body))
-	key, err := regogo.Get(string(body), "input.Key")
-	if err != nil {
-		glog.Error(err)
-		http.Error(w, "Could not find object key", http.StatusBadRequest)
-		return
-	}
-	resp, err := m.gql.RunQuery(fmt.Sprintf(
-		"mutation { addImage(input: [{objectPath: \"%s\"}]) { image { id }} }",
-		key.String()))
-	if err != nil {
-		glog.Error(err)
-		http.Error(w, "Can not add given objects", http.StatusInternalServerError)
-		return
-	}
+	// TODO(giolekva): move this to events processor
+	resp := ""
 	id, err := regogo.Get(resp, "input.addImage.image[0].id")
 	if err != nil {
 		glog.Error(err)
@@ -196,7 +174,6 @@
 		panic(err)
 	}
 	mw := MinioWebhook{gqlClient, pods}
-	http.HandleFunc("/minio_webhook", mw.minioHandler)
 	http.HandleFunc("/graphql", mw.graphqlHandler)
 	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
 }