diff options
| author | Baitinq <[email protected]> | 2024-05-02 15:43:24 +0200 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2024-05-02 16:06:52 +0200 |
| commit | 3e876c6d03c766193c62c2c0148efdaf43b69d3e (patch) | |
| tree | 46552f75dd920db8516272f8489a38a8b3b70941 /src/rest-api | |
| parent | Start setting up rabbitmq connection (diff) | |
| download | fs-tracer-backend-3e876c6d03c766193c62c2c0148efdaf43b69d3e.tar.gz fs-tracer-backend-3e876c6d03c766193c62c2c0148efdaf43b69d3e.tar.bz2 fs-tracer-backend-3e876c6d03c766193c62c2c0148efdaf43b69d3e.zip | |
Change module name and cleanup http handler
Diffstat (limited to 'src/rest-api')
| -rw-r--r-- | src/rest-api/cmd/BUILD.bazel | 7 | ||||
| -rw-r--r-- | src/rest-api/cmd/main.go | 27 | ||||
| -rw-r--r-- | src/rest-api/handler/BUILD.bazel | 9 | ||||
| -rw-r--r-- | src/rest-api/handler/handler.go | 44 |
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)) +} |