about summary refs log tree commit diff
path: root/src/rest-api
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest-api')
-rw-r--r--src/rest-api/cmd/BUILD.bazel7
-rw-r--r--src/rest-api/cmd/main.go27
-rw-r--r--src/rest-api/handler/BUILD.bazel9
-rw-r--r--src/rest-api/handler/handler.go44
4 files changed, 61 insertions, 26 deletions
diff --git a/src/rest-api/cmd/BUILD.bazel b/src/rest-api/cmd/BUILD.bazel
index d91e054..842f6f9 100644
--- a/src/rest-api/cmd/BUILD.bazel
+++ b/src/rest-api/cmd/BUILD.bazel
@@ -5,9 +5,12 @@ load("@rules_pkg//:pkg.bzl", "pkg_tar")
 go_library(
     name = "cmd_lib",
     srcs = ["main.go"],
-    importpath = "github.com/Baitinq/rest-api/src/rest-api/cmd",
+    importpath = "github.com/Baitinq/fs-tracer-backend/src/rest-api/cmd",
     visibility = ["//visibility:private"],
-    deps = ["@com_github_rabbitmq_amqp091_go//:go_default_library"],
+    deps = [
+        "//src/rest-api/handler",
+        "@com_github_rabbitmq_amqp091_go//:amqp091-go",
+    ],
 )
 
 go_binary(
diff --git a/src/rest-api/cmd/main.go b/src/rest-api/cmd/main.go
index 37182c1..6740bea 100644
--- a/src/rest-api/cmd/main.go
+++ b/src/rest-api/cmd/main.go
@@ -1,14 +1,12 @@
 package main
 
 import (
-	"context"
 	"fmt"
-	"io"
 	"log"
 	"net/http"
 	"os"
-	"time"
 
+	"github.com/Baitinq/fs-tracer-backend/src/rest-api/handler"
 	amqp "github.com/rabbitmq/amqp091-go"
 )
 
@@ -41,32 +39,13 @@ func main() {
 	if err != nil {
 		panic(err)
 	}
-	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
-	defer cancel()
-
-	body := "Hello World!"
-	ch.PublishWithContext(ctx, "", q.Name, false, false, amqp.Publishing{
-		ContentType: "text/plain",
-		Body:        []byte(body),
-	})
-
-	log.Println(" [x] Sent", body)
+	handler := handler.NewHandler(ch, q.Name)
 
 	mux := http.NewServeMux()
 	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 		fmt.Fprint(w, "Hello folks!")
 	})
-	mux.HandleFunc("/payload", handleRequest)
+	mux.Handle("/payload", handler)
 
 	http.ListenAndServe(":8080", mux)
 }
-
-func handleRequest(w http.ResponseWriter, r *http.Request) {
-	bytes, err := io.ReadAll(io.Reader(r.Body))
-	if err != nil {
-		panic(err)
-	}
-
-	fmt.Fprint(w, "Hello, World!", string(bytes))
-	log.Println("Request received", r.RemoteAddr, string(bytes))
-}
diff --git a/src/rest-api/handler/BUILD.bazel b/src/rest-api/handler/BUILD.bazel
new file mode 100644
index 0000000..a5638f4
--- /dev/null
+++ b/src/rest-api/handler/BUILD.bazel
@@ -0,0 +1,9 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
+
+go_library(
+    name = "handler",
+    srcs = ["handler.go"],
+    importpath = "github.com/Baitinq/fs-tracer-backend/src/rest-api/handler",
+    visibility = ["//visibility:public"],
+    deps = ["@com_github_rabbitmq_amqp091_go//:amqp091-go"],
+)
diff --git a/src/rest-api/handler/handler.go b/src/rest-api/handler/handler.go
new file mode 100644
index 0000000..b5c3ae2
--- /dev/null
+++ b/src/rest-api/handler/handler.go
@@ -0,0 +1,44 @@
+package handler
+
+import (
+	"context"
+	"fmt"
+	"io"
+	"log"
+	"net/http"
+	"time"
+
+	amqp "github.com/rabbitmq/amqp091-go"
+)
+
+type Handler struct {
+	ch        *amqp.Channel
+	queueName string
+}
+
+func NewHandler(ch *amqp.Channel, queueName string) *Handler {
+	return &Handler{
+		ch:        ch,
+		queueName: queueName,
+	}
+}
+
+func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	bytes, err := io.ReadAll(io.Reader(r.Body))
+	if err != nil {
+		panic(err)
+	}
+
+	body := fmt.Sprint("Hello World!", r.RemoteAddr, string(bytes))
+
+	ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
+	defer cancel()
+
+	go h.ch.PublishWithContext(ctx, "", h.queueName, false, false, amqp.Publishing{
+		ContentType: "text/plain",
+		Body:        []byte(body),
+	})
+
+	fmt.Fprint(w, "Hello, World!", string(bytes))
+	log.Println("Request received", r.RemoteAddr, string(bytes))
+}