diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-02 15:43:24 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-02 16:06:52 +0200 |
commit | 3e876c6d03c766193c62c2c0148efdaf43b69d3e (patch) | |
tree | 46552f75dd920db8516272f8489a38a8b3b70941 | |
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
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 17 | ||||
-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 |
6 files changed, 79 insertions, 27 deletions
diff --git a/go.mod b/go.mod index 21c4602..1085bfe 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Baitinq/rest-api +module github.com/Baitinq/fs-tracer-backend go 1.22.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..00c627d --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo= +github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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)) +} |