From fe2fdb57e66648f031b068bc7db118a05c516811 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Fri, 19 Jul 2024 21:25:59 +0200 Subject: [PATCH] Huge refactoring, resolved coupling issues --- deploy/image-build.sh | 2 +- deploy/image-push.sh | 4 +- src/app.run | 2 +- src/cmd/migrate/main.go | 6 +- src/cmd/server/main.go | 33 +-- src/cmd/worker/main.go | 30 +-- src/go.mod | 37 +-- src/go.sum | 115 +++++---- src/internal/app/app.go | 81 +++++++ src/internal/app/plugins.go | 139 +++++++++++ src/internal/server/basket_handler.go | 24 +- src/internal/server/config.go | 111 +++++++++ src/internal/server/health_handler.go | 2 +- src/internal/server/middleware.go | 4 +- src/internal/server/router.go | 18 +- src/internal/server/server.go | 271 ++++++++-------------- src/internal/service/basket.go | 20 +- src/internal/ui/basket.go | 6 +- src/internal/worker/command.go | 4 +- src/internal/{config => worker}/config.go | 67 ++++-- src/internal/worker/ext.go | 216 +++++++++-------- src/internal/worker/worker.go | 221 ++++++++++-------- src/pkg/server/config.go | 34 --- src/pkg/server/server.go | 101 -------- 24 files changed, 878 insertions(+), 670 deletions(-) create mode 100644 src/internal/app/app.go create mode 100644 src/internal/app/plugins.go create mode 100644 src/internal/server/config.go rename src/internal/{config => worker}/config.go (53%) delete mode 100644 src/pkg/server/config.go delete mode 100644 src/pkg/server/server.go diff --git a/deploy/image-build.sh b/deploy/image-build.sh index c5e7b34..109eb51 100755 --- a/deploy/image-build.sh +++ b/deploy/image-build.sh @@ -1,7 +1,7 @@ #!/bin/sh # RUN IN REPO ROOT DIR !! -export IMAGE_PREFIX="git.pbiernat.dev/egommerce/basket" +export IMAGE_PREFIX="git.pbiernat.io/egommerce/basket" export BUILDER_IMAGE="egommerce-builder:basket" export BUILD_TIME=$(date +"%Y%m%d%H%M%S") export SERVER_IMAGE="$IMAGE_PREFIX-svc" diff --git a/deploy/image-push.sh b/deploy/image-push.sh index 613cc5f..9be1fef 100755 --- a/deploy/image-push.sh +++ b/deploy/image-push.sh @@ -1,13 +1,13 @@ #!/bin/sh # RUN IN REPO ROOT DIR !! -export IMAGE_BASE="git.pbiernat.dev/egommerce/basket" +export IMAGE_BASE="git.pbiernat.io/egommerce/basket" export SERVER_IMAGE="$IMAGE_BASE-svc" export WORKER_IMAGE="$IMAGE_BASE-worker" TARGET=${1:-latest} -echo $DOCKER_PASSWORD | docker login git.pbiernat.dev -u $DOCKER_USERNAME --password-stdin +echo $DOCKER_PASSWORD | docker login git.pbiernat.io -u $DOCKER_USERNAME --password-stdin docker push "$SERVER_IMAGE:$TARGET" docker push "$WORKER_IMAGE:$TARGET" diff --git a/src/app.run b/src/app.run index a9e0667..66b5474 100644 --- a/src/app.run +++ b/src/app.run @@ -1 +1 @@ -954751 \ No newline at end of file +1697814 \ No newline at end of file diff --git a/src/cmd/migrate/main.go b/src/cmd/migrate/main.go index 4a77945..94aba4d 100644 --- a/src/cmd/migrate/main.go +++ b/src/cmd/migrate/main.go @@ -9,10 +9,10 @@ import ( "github.com/go-pg/migrations/v8" "github.com/go-pg/pg/v10" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - baseCnf "git.pbiernat.dev/egommerce/go-api-pkg/config" + cnf "git.pbiernat.io/egommerce/basket-service/internal/server" + baseCnf "git.pbiernat.io/egommerce/go-api-pkg/config" ) const ( diff --git a/src/cmd/server/main.go b/src/cmd/server/main.go index cf284de..8394edd 100644 --- a/src/cmd/server/main.go +++ b/src/cmd/server/main.go @@ -1,32 +1,34 @@ package main import ( + "fmt" "log" "os" - baseCnf "git.pbiernat.dev/egommerce/go-api-pkg/config" + cnf "git.pbiernat.io/egommerce/go-api-pkg/config" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - svr "git.pbiernat.dev/egommerce/basket-service/internal/server" + "git.pbiernat.io/egommerce/basket-service/internal/app" + "git.pbiernat.io/egommerce/basket-service/internal/server" ) func main() { - if baseCnf.ErrLoadingEnvs != nil { - log.Panicln("Error loading .env file", baseCnf.ErrLoadingEnvs) + if cnf.ErrLoadingEnvs != nil { + log.Panicln("Error loading .env file", cnf.ErrLoadingEnvs) } - c := cnf.NewConfig("basket") - srv := svr.New( - c, - svr.WithCache(c), - svr.WithDatabase(c), - svr.WithEventbus(c), - svr.WithLogger(c), - svr.WithRegistry(c), - ) + c := server.NewConfig("basket") + cArr := c.GetArray() + + doer := server.New(c) + a := app.NewApp(doer) + a.RegisterPlugin(app.LoggerPlugin(cArr)) + a.RegisterPlugin(app.CachePlugin(cArr)) + a.RegisterPlugin(app.DatabasePlugin(cArr)) + a.RegisterPlugin(app.EventbusPlugin(cArr)) + a.RegisterPlugin(app.RegistryPlugin(cArr)) while := make(chan struct{}) - err := srv.Base.Start(while) + err := a.Start(while) <-while if err != nil { @@ -34,5 +36,6 @@ func main() { os.Exit(1) } + fmt.Println("Gone") os.Exit(0) } diff --git a/src/cmd/worker/main.go b/src/cmd/worker/main.go index a4c7cd3..6d75c4a 100644 --- a/src/cmd/worker/main.go +++ b/src/cmd/worker/main.go @@ -1,32 +1,33 @@ package main import ( + "fmt" "log" "os" - "git.pbiernat.dev/egommerce/go-api-pkg/config" + cnf "git.pbiernat.io/egommerce/go-api-pkg/config" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - "git.pbiernat.dev/egommerce/basket-service/internal/worker" + "git.pbiernat.io/egommerce/basket-service/internal/app" + "git.pbiernat.io/egommerce/basket-service/internal/worker" ) func main() { - if config.ErrLoadingEnvs != nil { + if cnf.ErrLoadingEnvs != nil { log.Fatalln("Error loading .env file.") } - c := cnf.NewConfig("basket-worker") - wrk := worker.New( - c, - worker.WithCache(c), - worker.WithDatabase(c), - worker.WithEventbus(c), - worker.WithLogger(c), - worker.WithRegistry(c), - ) + c := worker.NewConfig("catalog-worker") + cArr := c.GetArray() + + doer := worker.New(c) + a := app.NewApp(doer) + a.RegisterPlugin(app.LoggerPlugin(cArr)) + a.RegisterPlugin(app.CachePlugin(cArr)) + a.RegisterPlugin(app.DatabasePlugin(cArr)) + a.RegisterPlugin(app.EventbusPlugin(cArr)) while := make(chan struct{}) - err := wrk.Start(while) + err := a.Start(while) <-while if err != nil { @@ -34,5 +35,6 @@ func main() { os.Exit(1) } + fmt.Println("Gone") os.Exit(0) } diff --git a/src/go.mod b/src/go.mod index 7a4f7e8..6ad9164 100644 --- a/src/go.mod +++ b/src/go.mod @@ -1,22 +1,22 @@ -module git.pbiernat.dev/egommerce/basket-service +module git.pbiernat.io/egommerce/basket-service go 1.18 require ( - git.pbiernat.dev/egommerce/api-entities v0.0.26 - git.pbiernat.dev/egommerce/go-api-pkg v0.1.66 + git.pbiernat.io/egommerce/api-entities v0.2.3 + git.pbiernat.io/egommerce/go-api-pkg v0.2.88 github.com/georgysavva/scany/v2 v2.0.0 github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/pg/v10 v10.10.7 github.com/go-redis/redis/v8 v8.11.5 - github.com/gofiber/fiber/v2 v2.40.1 + github.com/gofiber/fiber/v2 v2.52.5 github.com/jackc/pgx/v5 v5.1.1 - github.com/streadway/amqp v1.0.0 + github.com/rabbitmq/amqp091-go v1.10.0 ) require ( github.com/DataDog/datadog-go v3.2.0+incompatible // indirect - github.com/andybalholm/brotli v1.0.4 // indirect + github.com/andybalholm/brotli v1.0.5 // indirect github.com/armon/go-metrics v0.4.1 // indirect github.com/armon/go-radix v1.0.0 // indirect github.com/aws/aws-sdk-go v1.42.34 // indirect @@ -35,6 +35,7 @@ require ( github.com/go-pg/zerochecker v0.2.0 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/btree v1.0.1 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/hashicorp/consul v1.16.0 // indirect github.com/hashicorp/consul-net-rpc v0.0.0-20221205195236-156cfab66a69 // indirect github.com/hashicorp/consul/api v1.22.0 // indirect @@ -62,15 +63,15 @@ require ( github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect - github.com/jackc/pgtype v1.13.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgtype v1.14.3 // indirect github.com/jackc/puddle/v2 v2.1.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/joho/godotenv v1.5.1 // indirect - github.com/klauspost/compress v1.15.9 // indirect + github.com/klauspost/compress v1.17.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect - github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/miekg/dns v1.1.41 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect @@ -79,7 +80,7 @@ require ( github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect - github.com/philhofer/fwd v1.1.1 // indirect + github.com/philhofer/fwd v1.1.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.14.0 // indirect @@ -90,23 +91,23 @@ require ( github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.3 // indirect - github.com/tinylib/msgp v1.1.6 // indirect + github.com/tinylib/msgp v1.1.8 // indirect github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.41.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect github.com/vmihailenco/bufpool v0.1.11 // indirect github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect github.com/vmihailenco/tagparser v0.1.2 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect go.uber.org/atomic v1.10.0 // indirect - golang.org/x/crypto v0.1.0 // indirect + golang.org/x/crypto v0.20.0 // indirect golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect - golang.org/x/net v0.10.0 // indirect + golang.org/x/net v0.21.0 // indirect golang.org/x/sync v0.2.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect google.golang.org/protobuf v1.30.0 // indirect diff --git a/src/go.sum b/src/go.sum index ed25bc6..99ea1fb 100644 --- a/src/go.sum +++ b/src/go.sum @@ -35,10 +35,10 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -git.pbiernat.dev/egommerce/api-entities v0.0.26 h1:Avz02GINwuYWOjw1fmZIJ3QgGEIz3a5vRQZNaxxUQIk= -git.pbiernat.dev/egommerce/api-entities v0.0.26/go.mod h1:+BXvUcr6Cr6QNpJsW8BUfe1vVILdWDADNE0e3u0lNvU= -git.pbiernat.dev/egommerce/go-api-pkg v0.1.66 h1:CRJYVSIZ8iolu3LQM9hAcImniM5u9+EYFRUR2bY0Emk= -git.pbiernat.dev/egommerce/go-api-pkg v0.1.66/go.mod h1:nzsa99OyjTHGT5KK294iPzDEjtDFdkcHYejcpDDTL6M= +git.pbiernat.io/egommerce/api-entities v0.2.3 h1:mR6EYfZkAzh4teydb7KXDBWoxwVW3qasnmmH5J3mnas= +git.pbiernat.io/egommerce/api-entities v0.2.3/go.mod h1:INXAG5x4+i+vNwg1NpfPHiDW8nY1kn1K7pgLOtX+/I0= +git.pbiernat.io/egommerce/go-api-pkg v0.2.88 h1:xya/39BnFeha3Oc76ad/ppoQd6AstTGQd87Qszamr1A= +git.pbiernat.io/egommerce/go-api-pkg v0.2.88/go.mod h1:XIy2mmvRNIzQmYIUAcDZafhRPxTQFS2HDmsK7ZQ6980= github.com/Azure/azure-sdk-for-go v44.0.0+incompatible h1:e82Yv2HNpS0kuyeCrV29OPKvEiqfs2/uJHic3/3iKdg= github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM= @@ -64,8 +64,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/aliyun/alibaba-cloud-sdk-go v1.62.156 h1:K4N91T1+RlSlx+t2dujeDviy4ehSGVjEltluDgmeHS4= -github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= -github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e h1:QEF07wC0T1rKkctt1RINW/+RMTVmiwxETico2l3gxJA= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= @@ -184,8 +184,8 @@ github.com/go-pg/zerochecker v0.2.0/go.mod h1:NJZ4wKL0NmTtz0GKCoJ8kym6Xn/EQzXRl2 github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gofiber/fiber/v2 v2.40.1 h1:pc7n9VVpGIqNsvg9IPLQhyFEMJL8gCs1kneH5D1pIl4= -github.com/gofiber/fiber/v2 v2.40.1/go.mod h1:Gko04sLksnHbzLSRBFWPFdzM9Ws9pRxvvIaohJK1dsk= +github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= +github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -250,7 +250,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/enterprise-certificate-proxy v0.2.3 h1:yk9/cqRKtT9wXZSsRH9aurXEpJX+U6FLtpYTdC3R06k= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -366,8 +367,9 @@ github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= -github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530 h1:dUJ578zuPEsXjtzOfEF0q9zDAfljJ9oFnTHcQaNkccw= github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= +github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w= +github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= @@ -382,26 +384,31 @@ github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.1.1 h1:7PQ/4gLoqnl87ZxL7xjO0DR5gYuviDCZxQJsUlFW1eI= github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= +github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag= +github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= -github.com/jackc/pgtype v1.13.0 h1:XkIc7A+1BmZD19bB2NxrtjJweHxQ9agqvM+9URc68Cg= -github.com/jackc/pgtype v1.13.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= +github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= +github.com/jackc/pgtype v1.14.3 h1:h6W9cPuHsRWQFTWUZMAKMgG5jSwQI0Zurzdvlx3Plus= +github.com/jackc/pgtype v1.14.3/go.mod h1:aKeozOde08iifGosdJpz9MBZonJOUJxqNpPBcMJTlVA= github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= -github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c h1:Dznn52SgVIVst9UyOT9brctYUgxs+CvVfPaC3jKrA50= github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= +github.com/jackc/pgx/v4 v4.18.2 h1:xVpYkNR5pk5bMCZGfClbO962UIqVABcAGt7ha1s/FeU= +github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw= github.com/jackc/pgx/v5 v5.1.1 h1:pZD79K1SYv8wc2HmCQA6VdmRQi7/OtCfv9bM3WAXUYA= github.com/jackc/pgx/v5 v5.1.1/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle/v2 v2.1.2 h1:0f7vaaXINONKTsxYDn4otOAiJanX/BMeAtY//BXqzlg= github.com/jackc/puddle/v2 v2.1.2/go.mod h1:2lpufsF5mRHO6SuZkm0fNYxM6SWHfvyFj62KwNzgels= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -425,8 +432,8 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= -github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= +github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -462,10 +469,10 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= -github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -522,8 +529,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= -github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= +github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= +github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pierrec/lz4 v2.5.2+incompatible h1:WCjObylUIOlKy/+7Abdn34TLIkXiA4UWUMhxq9m9ZXI= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -568,6 +575,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= +github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw= +github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o= github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 h1:Wdi9nwnhFNAlseAOekn6B5G/+GMtks9UKbvRU/CMM/o= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -594,8 +603,6 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= github.com/softlayer/softlayer-go v0.0.0-20180806151055-260589d94c7d h1:bVQRCxQvfjNUeRqaY/uT0tFuvuFY0ulgnczuR684Xic= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= -github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= @@ -611,12 +618,13 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tencentcloud/tencentcloud-sdk-go v1.0.162 h1:8fDzz4GuVg4skjY2B0nMN7h6uN61EDVkuLyI2+qGHhI= -github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw= -github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= +github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= +github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o= github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= @@ -625,8 +633,8 @@ github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.41.0 h1:zeR0Z1my1wDHTRiamBCXVglQdbUwgb9uWG3k1HQz6jY= -github.com/valyala/fasthttp v1.41.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94= @@ -644,7 +652,7 @@ github.com/vmware/govmomi v0.18.0 h1:f7QxSmP7meCtoAmiKZogvVbLInT+CZx6Px6K5rYsJZo github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ= @@ -668,8 +676,8 @@ go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= -go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= @@ -693,9 +701,9 @@ golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= -golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= +golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -720,7 +728,6 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= @@ -729,6 +736,9 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -766,17 +776,18 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -793,9 +804,10 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -860,14 +872,24 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= +golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -876,8 +898,11 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -930,7 +955,9 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/src/internal/app/app.go b/src/internal/app/app.go new file mode 100644 index 0000000..26225c9 --- /dev/null +++ b/src/internal/app/app.go @@ -0,0 +1,81 @@ +package app + +import ( + "log" + "os" + "os/signal" + "strconv" + "syscall" +) + +type ( + Doer interface { + Start() error + RegisterHandler(string, func() any) + OnShutdown() + } + Application interface { + Start(while chan struct{}) + RegisterPlugin(PluginFn) error + Shutdown() + } + + App struct { + doer Doer + } +) + +func NewApp(d Doer) *App { + return &App{ + doer: d, + } +} + +func (a *App) Start(while chan struct{}) error { + go func() { + sigint := make(chan os.Signal, 1) + signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + <-sigint + + a.Shutdown() + + close(while) + }() + + run := a.createRunFile("./app.run") // FIXME path... + defer a.removeRunFile(run) + + err := a.doer.Start() + if err != nil { + log.Fatalf("Failed to start app. Reason: %v\n", err) + close(while) + } + <-while + + return err +} + +func (a *App) RegisterPlugin(p Plugin) error { + a.doer.RegisterHandler(p.name, p.fn) + + return nil +} + +func (a *App) Shutdown() { + a.doer.OnShutdown() +} + +func (a *App) createRunFile(path string) *os.File { + run, err := os.Create(path) + if err != nil { + log.Fatalf("Failed to create run file. Reason: %v\n", err) + os.Exit(1) + } + run.WriteString(strconv.Itoa(os.Getpid())) + + return run +} + +func (a *App) removeRunFile(f *os.File) error { + return f.Close() +} diff --git a/src/internal/app/plugins.go b/src/internal/app/plugins.go new file mode 100644 index 0000000..af156fa --- /dev/null +++ b/src/internal/app/plugins.go @@ -0,0 +1,139 @@ +package app + +import ( + "log" + "os" + "strconv" + + redis "github.com/go-redis/redis/v8" + amqp "github.com/rabbitmq/amqp091-go" + + "git.pbiernat.io/egommerce/go-api-pkg/consul" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + + db "git.pbiernat.io/egommerce/basket-service/pkg/database" +) + +type ( + Plugin struct { + name string + fn PluginFn + } + PluginFn func() any +) + +func CachePlugin(cArr map[string]string) Plugin { + return Plugin{ + name: "cache", + fn: func() any { + return redis.NewClient(&redis.Options{ + Addr: cArr["cacheAddr"], + Password: cArr["cachePassword"], + DB: 0, + }) + }, + } +} + +func DatabasePlugin(cArr map[string]string) Plugin { + return Plugin{ + name: "database", + fn: func() any { + dbConn, err := db.Connect(cArr["dbURL"]) + if err != nil { + log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", cArr["dbURL"], err) + os.Exit(1) // TODO: retry in background... + } + + return dbConn + }, + } +} + +func EventbusPlugin(cArr map[string]string) Plugin { + return Plugin{ + name: "eventbus", + fn: func() any { + conn, err := amqp.Dial(cArr["eventBusURL"]) + if err != nil { + log.Fatalf("Failed to connect to the EventBus: %s. Err: %v\n", cArr["eventBusURL"], err) + os.Exit(1) // TODO: retry in background... + } + + chn, err := conn.Channel() + if err != nil { + log.Fatalf("Failed to open new EventBus channel. Err: %v\n", err) + os.Exit(1) // TODO: retry in background... + } + + return chn + }, + } +} + +func LoggerPlugin(cArr map[string]string) Plugin { + return Plugin{ + name: "logger", + fn: func() any { + logHost, logPort, err := fluentd.ParseAddr(cArr["loggerAddr"]) + if err != nil { + log.Fatalf("Failed to parse FluentD address: %s. Err: %v", cArr["loggerAddr"], err) + os.Exit(1) // TODO: retry in background... + } + + logger, err := fluentd.NewLogger(cArr["appFullname"], logHost, logPort) + if err != nil { + log.Fatalf("Failed to connect to the FluentD on %s:%d. Err: %v", logHost, logPort, err) + os.Exit(1) // TODO: retry in background... + } + + return logger + }, + } +} + +func RegistryPlugin(cArr map[string]string) Plugin { + return Plugin{ + name: "registry", + fn: func() any { + port, _ := strconv.Atoi(cArr["netAddr"][1:]) // FIXME: can be IP:PORT or :PORT + // log.Printf("Consul retrieved port: %v", port) + registry, err := consul.NewService(cArr["registryAddr"], cArr["id"], cArr["name"], cArr["registryDomainOverIP"], cArr["ip"], cArr["domain"], cArr["pathPrefix"], port) + if err != nil { + log.Fatalf("Failed to connect to the Consul on: %s. Err: %v", cArr["registryAddr"], err) + os.Exit(1) // TODO: retry in background... + } + + err = registry.Register() + if err != nil { + log.Fatalf("Failed to register in the Consul service. Err: %v", err) + os.Exit(1) // TODO: retry in background... + } + + registry.RegisterHealthChecks() + // a.registerKVUpdater() // FIXME run as goroutine + + return registry + + // svc, _ := registry.Connect() + // tlsCnf := svc.ServerTLSConfig() + // s.Base.App.Server().TLSConfig = tlsCnf + // fmt.Println("Podmiana configa TLS") + // defer svc.Close() + + // go func() { // Consul KV updater + // ticker := time.NewTicker(time.Second * 15) + // for range ticker.C { + // fetchKVConfig(s) // FIXME: duplicated in worker + // } + // }() + + // go func() { // Server metadata cache updater + // ticker := time.NewTicker(time.Second * 5) + // for range ticker.C { + // s.cacheMetadata() + // } + // }() + }, + } +} diff --git a/src/internal/server/basket_handler.go b/src/internal/server/basket_handler.go index 04d984f..d4867b5 100644 --- a/src/internal/server/basket_handler.go +++ b/src/internal/server/basket_handler.go @@ -4,23 +4,23 @@ package server import ( "time" - "git.pbiernat.dev/egommerce/api-entities/http" - "git.pbiernat.dev/egommerce/basket-service/internal/service" - "git.pbiernat.dev/egommerce/basket-service/internal/ui" + "git.pbiernat.io/egommerce/api-entities/http" + "git.pbiernat.io/egommerce/basket-service/internal/service" + "git.pbiernat.io/egommerce/basket-service/internal/ui" "github.com/gofiber/fiber/v2" ) func (s *Server) GetBasketHandler(c *fiber.Ctx) error { req := new(http.GetBasketRequest) if err := c.BodyParser(req); err != nil { - return s.Base.Error(c, 400, err.Error()) + return s.Error(c, 400, err.Error()) } basketID := req.BasketID - basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) + basketSrv := service.NewBasketService(s.GetDatabase(), s.GetCache(), s.GetEventBus(), s.GetLogger()) basket, err := basketSrv.FetchFromDB(c.Context(), basketID) if err != nil { - return s.Base.Error(c, 400, "Failed to retrieve basket") + return s.Error(c, 400, "Failed to retrieve basket") } res := &http.GetBasketResponse{ @@ -37,10 +37,10 @@ func (s *Server) GetBasketHandler(c *fiber.Ctx) error { func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error { basketID := c.Params("basketId", "") - basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) + basketSrv := service.NewBasketService(s.GetDatabase(), s.GetCache(), s.GetEventBus(), s.GetLogger()) items, err := basketSrv.FetchItems(c.Context(), basketID) if err != nil { - return s.Base.Error(c, 400, "Failed to retrieve basket") + return s.Error(c, 400, "Failed to retrieve basket") } var res []*http.GetBasketItemsResponse @@ -63,17 +63,17 @@ func (s *Server) GetBasketItemsHandler(c *fiber.Ctx) error { } func (s *Server) CheckoutHandler(c *fiber.Ctx) error { - reqID, _ := s.Base.GetRequestID(c) + reqID, _ := s.GetRequestID(c) req := new(http.BasketCheckoutRequest) if err := c.BodyParser(req); err != nil { - return s.Base.Error(c, 400, err.Error()) + return s.Error(c, 400, err.Error()) } basketID := req.BasketID - basketSrv := service.NewBasketService(s.Database, s.Cache, s.Eventbus, s.Logger) + basketSrv := service.NewBasketService(s.GetDatabase(), s.GetCache(), s.GetEventBus(), s.GetLogger()) res, err := ui.CheckoutBasket(basketSrv, basketID, reqID) if err != nil { - return s.Base.Error(c, 400, "Failed to create order") + return s.Error(c, 400, "Failed to create order") } return c.JSON(res) diff --git a/src/internal/server/config.go b/src/internal/server/config.go new file mode 100644 index 0000000..c5b43ba --- /dev/null +++ b/src/internal/server/config.go @@ -0,0 +1,111 @@ +package server + +import ( + "fmt" + "net" + "os" + "time" + + cnf "git.pbiernat.io/egommerce/go-api-pkg/config" +) + +const ( + defName = "basket-svc" + defDomain = "basket-svc" + defCacheAddr = "egommerce.local:6379" + defCachePassword = "12345678" + defDbURL = "postgres://postgres:12345678@db-postgres:5432/egommerce" + defEventBusURL = "amqp://guest:guest@api-eventbus:5672" + defKVNmspc = "dev.egommerce/service/basket" + defLoggerAddr = "api-logger:24224" + defNetAddr = ":80" + defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017" + defPathPrefix = "/basket" + defRegistryAddr = "api-registry:8500" + defEbEventsExchange = "api-events" + defEbEventsQueue = "basket-svc" +) + +type Config struct { + ID string + Name string + Domain string + NetAddr string + RegistryDomainOverIP string + PathPrefix string + + IdleTimeout time.Duration // miliseconds + ReadTimeout time.Duration // miliseconds + WriteTimeout time.Duration // miliseconds + + LoggerAddr string `json:"logger_addr"` + DbURL string `json:"db_url"` + CacheAddr string `json:"cache_addr"` + CachePassword string `json:"cache_password"` + MongoDbUrl string `json:"mongodb_url"` + EventBusURL string `json:"eventbus_url"` + EventBusExchange string `json:"eventbus_exchange"` + EventBusQueue string `json:"eventbus_queue"` + KVNamespace string + RegistryAddr string + + // Fields with JSON mappings are available through Consul KV storage +} + +func NewConfig(name string) *Config { + c := new(Config) + + c.ID, _ = os.Hostname() + c.Name = name + c.Domain = cnf.GetEnv("APP_DOMAIN", defDomain) + c.NetAddr = cnf.GetEnv("SERVER_ADDR", defNetAddr) + c.RegistryDomainOverIP = cnf.GetEnv("REGISTRY_USE_DOMAIN_OVER_IP", "false") + c.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix) + + c.CacheAddr = cnf.GetEnv("CACHE_ADDR", defCacheAddr) + c.CachePassword = cnf.GetEnv("CACHE_PASSWORD", defCachePassword) + c.DbURL = cnf.GetEnv("DATABASE_URL", defDbURL) + c.EventBusExchange = defEbEventsExchange + c.EventBusURL = cnf.GetEnv("EVENTBUS_URL", defEventBusURL) + c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc) + c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr) + c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr) + + return c +} + +func (c *Config) GetAppFullName() string { + return fmt.Sprintf("%s_%s", c.Name, c.ID) +} + +func (c *Config) GetIP() string { + host, _ := os.Hostname() + ips, _ := net.LookupIP(host) + // for _, ip := range ips { + // return ip.String() + // } + + return ips[0].String() +} + +func (c *Config) GetArray() map[string]string { // FIXME fix types etc + arr := make(map[string]string) + arr["id"] = c.ID + arr["name"] = c.Name + arr["appFullname"] = c.GetAppFullName() + arr["domain"] = c.Domain + arr["ip"] = c.GetIP() + arr["netAddr"] = c.NetAddr + arr["registryDomainOverIP"] = c.RegistryDomainOverIP + arr["pathPrefix"] = c.PathPrefix + arr["cacheAddr"] = c.CacheAddr + arr["cachePassword"] = c.CachePassword + arr["dbURL"] = c.DbURL + arr["eventBusExchange"] = c.EventBusExchange + arr["eventBusURL"] = c.EventBusURL + arr["kvNamespace"] = c.KVNamespace + arr["loggerAddr"] = c.LoggerAddr + arr["registryAddr"] = c.RegistryAddr + + return arr +} diff --git a/src/internal/server/health_handler.go b/src/internal/server/health_handler.go index 68e1ffc..062bf6f 100644 --- a/src/internal/server/health_handler.go +++ b/src/internal/server/health_handler.go @@ -2,7 +2,7 @@ package server // REFACTOR: UNIVERSAL SERVER CODE import ( - def "git.pbiernat.dev/egommerce/api-entities/http" + def "git.pbiernat.io/egommerce/api-entities/http" "github.com/gofiber/fiber/v2" ) diff --git a/src/internal/server/middleware.go b/src/internal/server/middleware.go index a059d7c..32328d9 100644 --- a/src/internal/server/middleware.go +++ b/src/internal/server/middleware.go @@ -5,14 +5,14 @@ import ( "github.com/gofiber/fiber/v2" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" ) // "github.com/gofiber/fiber/v2" // "github.com/gofiber/fiber/v2/middleware/cors" func SetupMiddleware(s *Server) { - s.Base.Use(LoggingMiddleware(s.Logger)) + s.Use(LoggingMiddleware(s.GetLogger())) } func LoggingMiddleware(log *fluentd.Logger) func(c *fiber.Ctx) error { diff --git a/src/internal/server/router.go b/src/internal/server/router.go index 986e39a..99cdbec 100644 --- a/src/internal/server/router.go +++ b/src/internal/server/router.go @@ -7,21 +7,21 @@ import ( var ( defaultCORS = cors.New(cors.Config{ - AllowOrigins: "*", - AllowCredentials: true, - AllowMethods: "GET, POST, PATCH, PUT, DELETE, OPTIONS", - AllowHeaders: "Accept, Authorization, Content-Type, Vary, X-Request-Id", + AllowOrigins: "*", + // AllowCredentials: true, + AllowMethods: "GET, POST, PATCH, PUT, DELETE, OPTIONS", + AllowHeaders: "Accept, Authorization, Content-Type, Vary, X-Request-Id", }) ) func SetupRouter(s *Server) { - s.Base.Options("*", defaultCORS) - s.Base.Use(defaultCORS) + s.Options("*", defaultCORS) + s.Use(defaultCORS) - s.Base.Get("/health", s.HealthHandler) - s.Base.Get("/config", s.ConfigHandler) + s.Get("/health", s.HealthHandler) + s.Get("/config", s.ConfigHandler) - api := s.Base.Group("/api") + api := s.Group("/api") v1 := api.Group("/v1") v1.Get("/basket", s.GetBasketHandler) v1.Get("/basket/:basketId/items", s.GetBasketItemsHandler) diff --git a/src/internal/server/server.go b/src/internal/server/server.go index 9007723..6799c72 100644 --- a/src/internal/server/server.go +++ b/src/internal/server/server.go @@ -1,92 +1,128 @@ package server import ( - "bytes" - "encoding/json" - "log" - "os" - "strconv" + "net" "time" - redis "github.com/go-redis/redis/v8" + "github.com/go-redis/redis/v8" + "github.com/gofiber/fiber/v2" "github.com/jackc/pgx/v5/pgxpool" - "github.com/streadway/amqp" + amqp "github.com/rabbitmq/amqp091-go" - "git.pbiernat.dev/egommerce/go-api-pkg/consul" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" - - db "git.pbiernat.dev/egommerce/basket-service/pkg/database" - p "git.pbiernat.dev/egommerce/basket-service/pkg/server" - - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" + "git.pbiernat.io/egommerce/api-entities/http" + "git.pbiernat.io/egommerce/go-api-pkg/consul" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" ) type ( Server struct { - Base *p.Server - Config *cnf.Config - Cache *redis.Client - Database *pgxpool.Pool - Eventbus *amqp.Channel - Logger *fluentd.Logger - Registry *consul.Service + *fiber.App + + ID string + addr string // e.g. "127.0.0.1:80" + handlers map[string]any + } + HeaderRequestID struct { + RequestID string `reqHeader:"x-request-id"` } - OptionFn func(*Server) error // FIXME: similar in worker ) -func New(c *cnf.Config, opts ...OptionFn) *Server { - svr := &Server{ - Base: p.New(c.Base), - Config: c, +func New(c *Config) *Server { + return &Server{ + ID: c.ID, + App: fiber.New(fiber.Config{ + AppName: c.ID, + ServerHeader: c.Name + ":" + c.ID, + ReadTimeout: c.ReadTimeout * time.Millisecond, + WriteTimeout: c.WriteTimeout * time.Millisecond, + IdleTimeout: c.IdleTimeout * time.Millisecond, + }), + addr: c.NetAddr, + handlers: make(map[string]any), } - - for _, opt := range opts { - if err := opt(svr); err != nil { - log.Fatalf("Failed to attach extension to the server. Err: %v\n", err) - } - } - - svr.Base.ShutdownFn = svr.Shutdown() - - SetupMiddleware(svr) - SetupRouter(svr) - - return svr } -func (s *Server) Shutdown() p.ShutdownFn { - return func() error { - s.Logger.Log("Server %s is going down...", s.Base.AppID) +func (s *Server) Start() error { + SetupMiddleware(s) + SetupRouter(s) - s.Registry.Unregister() - // s.clearMetadataCache() - s.Eventbus.Close() - s.Database.Close() - s.Logger.Log("Gone.") - s.Logger.Close() + // fmt.Printf("Starting server at: %s...\n", s.addr) + ln, _ := net.Listen("tcp", s.addr) + // ln = tls.NewListener(ln, s.App.Server().TLSConfig) - return s.Base.Shutdown() + return s.Listener(ln) +} + +func (s *Server) RegisterHandler(name string, fn func() any) { + // fmt.Printf("Registering plugin( with handler): %s... OK\n", name) + s.handlers[name] = fn() +} + +func (s *Server) OnShutdown() { + // s.GetLogger().Log("Server %s is going down...", s.ID) + + s.GetRegistry().Unregister() + // a.clearMetadataCache() + s.GetEventBus().Close() + s.GetDatabase().Close() + s.GetLogger().Log("Gone.") + s.GetLogger().Close() + + s.Shutdown() +} + +func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) { + var hdr = new(HeaderRequestID) + if err := c.ReqHeaderParser(hdr); err != nil { + return "", err } + + return hdr.RequestID, nil +} + +func (s *Server) Error(c *fiber.Ctx, code int, msg string) error { + return c.Status(code).JSON(http.ErrorResponse{Error: msg}) +} + +// Plugin helper funcitons +func (s *Server) GetCache() *redis.Client { + return (s.handlers["cache"]).(*redis.Client) +} + +func (s *Server) GetDatabase() *pgxpool.Pool { // FIXME hardcoded index issue + return (s.handlers["database"]).(*pgxpool.Pool) +} + +func (s *Server) GetEventBus() *amqp.Channel { + return (s.handlers["eventbus"]).(*amqp.Channel) +} + +func (s *Server) GetLogger() *fluentd.Logger { + return (s.handlers["logger"]).(*fluentd.Logger) +} + +func (s *Server) GetRegistry() *consul.Service { + return (s.handlers["registry"]).(*consul.Service) } // @CHECK: merge s.Config and s.Base.Config to display all config as one array/map -func (s *Server) registerKVUpdater() { // @FIXME: merge duplication in server.go and worker.go - go func() { - ticker := time.NewTicker(time.Second * 10) - for range ticker.C { - config, _, err := s.Registry.KV().Get(s.Config.KVNamespace, nil) - if err != nil || config == nil { - return - } +// func (s *Server) registerKVUpdater() { // @FIXME: merge duplication in server.go and worker.go +// go func() { +// ticker := time.NewTicker(time.Second * 10) +// for range ticker.C { +// config, _, err := s.Registry.KV().Get(s.Config.KVNamespace, nil) +// if err != nil || config == nil { +// return +// } - kvCnf := bytes.NewBuffer(config.Value) - decoder := json.NewDecoder(kvCnf) - if err := decoder.Decode(&s.Config); err != nil { - return - } - } - }() -} +// kvCnf := bytes.NewBuffer(config.Value) +// decoder := json.NewDecoder(kvCnf) +// if err := decoder.Decode(&s.Config); err != nil { +// return +// } +// } +// }() +// } // func (s *Server) clearMetadataCache() { // ctx := context.Background() @@ -98,108 +134,3 @@ func (s *Server) registerKVUpdater() { // @FIXME: merge duplication in server.go // func (s *Server) getMetadataIPsKey() string { // return "internal__" + s.Base.Config.AppName + "__ips" // } - -func WithCache(c *cnf.Config) OptionFn { - return func(s *Server) error { - s.Cache = redis.NewClient(&redis.Options{ - Addr: s.Config.CacheAddr, - Password: s.Config.CachePassword, - DB: 0, - }) - - return nil - } -} - -func WithDatabase(c *cnf.Config) OptionFn { - dbConn, err := db.Connect(c.DbURL) - if err != nil { - log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", c.DbURL, err) - os.Exit(1) - } - - return func(s *Server) error { - s.Database = dbConn - - return nil - } -} - -func WithEventbus(c *cnf.Config) OptionFn { - return func(s *Server) error { - conn, err := amqp.Dial(s.Config.EventBusURL) - if err != nil { - log.Fatalf("Failed to connect to the Eventbus: %s. Err: %v\n", s.Config.EventBusURL, err) - } - - chn, err := conn.Channel() - if err != nil { - log.Fatalf("Failed to open new Eventbus channel. Err: %v\n", err) - } - - s.Eventbus = chn - - return nil - } -} - -func WithLogger(c *cnf.Config) OptionFn { - return func(s *Server) error { - logHost, logPort, err := fluentd.ParseAddr(c.LoggerAddr) - if err != nil { - log.Fatalf("Failed to parse Fluentd address: %s. Err: %v", c.LoggerAddr, err) - } - - logger, err := fluentd.NewLogger(c.Base.GetAppFullName(), logHost, logPort) - if err != nil { - log.Fatalf("Failed to connect to the Fluentd on %s:%d. Err: %v", logHost, logPort, err) - } - - s.Logger = logger - - return nil - } -} - -func WithRegistry(c *cnf.Config) OptionFn { - return func(s *Server) error { - port, _ := strconv.Atoi(c.Base.NetAddr[1:]) // FIXME: can be IP:PORT which will cause error - log.Printf("Consul retrieved port: %v", port) - registry, err := consul.NewService(c.RegistryAddr, c.Base.AppID, c.Base.AppName, c.Base.GetIP(), c.Base.AppDomain, c.Base.PathPrefix, port) - if err != nil { - log.Fatalf("Failed to connect to the Consul on: %s. Err: %v", c.RegistryAddr, err) - } - - err = registry.Register() - if err != nil { - log.Fatalf("Failed to register in the Consul service. Err: %v", err) - } - - registry.RegisterHealthChecks() - s.registerKVUpdater() - - s.Registry = registry - - // svc, _ := registry.Connect() - // tlsCnf := svc.ServerTLSConfig() - // s.Base.App.Server().TLSConfig = tlsCnf - // fmt.Println("Podmiana configa TLS") - // defer svc.Close() - - // go func() { // Consul KV updater - // ticker := time.NewTicker(time.Second * 15) - // for range ticker.C { - // fetchKVConfig(s) // FIXME: duplicated in worker - // } - // }() - - // go func() { // Server metadata cache updater - // ticker := time.NewTicker(time.Second * 5) - // for range ticker.C { - // s.cacheMetadata() - // } - // }() - - return nil - } -} diff --git a/src/internal/service/basket.go b/src/internal/service/basket.go index a18720b..e294672 100644 --- a/src/internal/service/basket.go +++ b/src/internal/service/basket.go @@ -4,17 +4,17 @@ import ( "context" "time" + "git.pbiernat.io/egommerce/api-entities/model" + "git.pbiernat.io/egommerce/basket-service/internal/event" + + "git.pbiernat.io/egommerce/go-api-pkg/api" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + "git.pbiernat.io/egommerce/go-api-pkg/rabbitmq" + "github.com/georgysavva/scany/v2/pgxscan" "github.com/go-redis/redis/v8" "github.com/jackc/pgx/v5/pgxpool" - "github.com/streadway/amqp" - - "git.pbiernat.dev/egommerce/api-entities/model" - "git.pbiernat.dev/egommerce/go-api-pkg/api" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" - "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" - - "git.pbiernat.dev/egommerce/basket-service/internal/event" + amqp "github.com/rabbitmq/amqp091-go" ) const ( @@ -84,7 +84,7 @@ func (s *BasketService) FetchItem(ctx context.Context, basketID string, productI } func (s *BasketService) AddItem(ctx context.Context, itemID int, basketID string, qty int) error { - var price float64 = 0 + var price int = 0 pricingAPI := api.NewPricingAPI(ServiceUserAgent, s.redis) productPrice, err := pricingAPI.GetProductPrice(itemID) @@ -114,7 +114,7 @@ func (s *BasketService) RemoveItem(ctx context.Context, itemID int, basketID str } func (s *BasketService) UpdateItem(ctx context.Context, item *model.BasketItemModel, qty int) error { - var price float64 = 0 + var price int = 0 pricingAPI := api.NewPricingAPI(ServiceUserAgent, s.redis) // FIXME productPrice, err := pricingAPI.GetProductPrice(item.ProductID) diff --git a/src/internal/ui/basket.go b/src/internal/ui/basket.go index 28df2b9..81a1635 100644 --- a/src/internal/ui/basket.go +++ b/src/internal/ui/basket.go @@ -3,10 +3,10 @@ package ui import ( "context" - entity "git.pbiernat.dev/egommerce/api-entities/http" - "git.pbiernat.dev/egommerce/api-entities/model" + entity "git.pbiernat.io/egommerce/api-entities/http" + "git.pbiernat.io/egommerce/api-entities/model" - "git.pbiernat.dev/egommerce/basket-service/internal/service" + "git.pbiernat.io/egommerce/basket-service/internal/service" ) func AddProductToBasket(srv *service.BasketService, productID, qty int, basketID, reqID string) (*model.BasketModel, error) { diff --git a/src/internal/worker/command.go b/src/internal/worker/command.go index a361893..97abc65 100644 --- a/src/internal/worker/command.go +++ b/src/internal/worker/command.go @@ -1,8 +1,8 @@ package worker import ( - "git.pbiernat.dev/egommerce/basket-service/internal/service" - "git.pbiernat.dev/egommerce/basket-service/internal/ui" + "git.pbiernat.io/egommerce/basket-service/internal/service" + "git.pbiernat.io/egommerce/basket-service/internal/ui" ) var ( diff --git a/src/internal/config/config.go b/src/internal/worker/config.go similarity index 53% rename from src/internal/config/config.go rename to src/internal/worker/config.go index bc1dcad..96552b6 100644 --- a/src/internal/config/config.go +++ b/src/internal/worker/config.go @@ -1,33 +1,30 @@ -package common +package worker import ( + "fmt" + "net" "os" - srv "git.pbiernat.dev/egommerce/basket-service/pkg/server" - cnf "git.pbiernat.dev/egommerce/go-api-pkg/config" + cnf "git.pbiernat.io/egommerce/go-api-pkg/config" ) const ( - // defAppDomain = "basket-svc" - // defEventBusURL = "amqp://guest:guest@esb.service.ego.io:5672" - defAppName = "basket-svc" - defAppDomain = "basket-svc" - defCacheAddr = "api-cache:6379" + defName = "catalog-worker" + defDomain = "catalog-worker" + defCacheAddr = "egommerce.local:6379" defCachePassword = "12345678" defDbURL = "postgres://postgres:12345678@db-postgres:5432/egommerce" defEventBusURL = "amqp://guest:guest@api-eventbus:5672" - defKVNmspc = "dev.egommerce/service/basket" + defKVNmspc = "dev.egommerce/service/catalog-worker" defLoggerAddr = "api-logger:24224" - defNetAddr = ":80" defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017" - defPathPrefix = "/basket" - defRegistryAddr = "api-registry:8500" defEbEventsExchange = "api-events" - defEbEventsQueue = "basket-svc" + defEbEventsQueue = "catalog-svc" ) type Config struct { - Base *srv.Config + ID string + Name string LoggerAddr string `json:"logger_addr"` DbURL string `json:"db_url"` @@ -38,20 +35,13 @@ type Config struct { EventBusExchange string `json:"eventbus_exchange"` EventBusQueue string `json:"eventbus_queue"` KVNamespace string - RegistryAddr string - - // Fields with JSON mappings are available through Consul KV storage } func NewConfig(name string) *Config { c := new(Config) - c.Base = new(srv.Config) - c.Base.AppID, _ = os.Hostname() - c.Base.AppName = name - c.Base.AppDomain = cnf.GetEnv("APP_DOMAIN", defAppDomain) - c.Base.NetAddr = cnf.GetEnv("SERVER_ADDR", defNetAddr) - c.Base.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix) + c.ID, _ = os.Hostname() + c.Name = name c.CacheAddr = cnf.GetEnv("CACHE_ADDR", defCacheAddr) c.CachePassword = cnf.GetEnv("CACHE_PASSWORD", defCachePassword) @@ -60,7 +50,36 @@ func NewConfig(name string) *Config { c.EventBusURL = cnf.GetEnv("EVENTBUS_URL", defEventBusURL) c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc) c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr) - c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr) return c } + +func (c *Config) GetAppFullName() string { + return fmt.Sprintf("%s_%s", c.Name, c.ID) +} + +func (c *Config) GetIP() string { + host, _ := os.Hostname() + ips, _ := net.LookupIP(host) + // for _, ip := range ips { + // return ip.String() + // } + + return ips[0].String() +} + +func (c *Config) GetArray() map[string]string { // FIXME fix types etc + arr := make(map[string]string) + arr["id"] = c.ID + arr["name"] = c.Name + arr["appFullname"] = c.GetAppFullName() + arr["cacheAddr"] = c.CacheAddr + arr["cachePassword"] = c.CachePassword + arr["dbURL"] = c.DbURL + arr["eventBusExchange"] = c.EventBusExchange + arr["eventBusURL"] = c.EventBusURL + arr["kvNamespace"] = c.KVNamespace + arr["loggerAddr"] = c.LoggerAddr + + return arr +} diff --git a/src/internal/worker/ext.go b/src/internal/worker/ext.go index 681febe..9ba2f79 100644 --- a/src/internal/worker/ext.go +++ b/src/internal/worker/ext.go @@ -1,135 +1,133 @@ package worker -import ( - "bytes" - "encoding/json" - "os" - "time" +// import ( +// "bytes" +// "encoding/json" +// "os" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - "git.pbiernat.dev/egommerce/basket-service/pkg/database" - "git.pbiernat.dev/egommerce/go-api-pkg/consul" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" - "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" - "github.com/go-redis/redis/v8" -) +// cnf "git.pbiernat.io/egommerce/basket-service/internal/config" +// "git.pbiernat.io/egommerce/basket-service/pkg/database" +// "git.pbiernat.io/egommerce/go-api-pkg/fluentd" +// "git.pbiernat.io/egommerce/go-api-pkg/rabbitmq" +// "github.com/go-redis/redis/v8" +// ) -func WithCache(c *cnf.Config) OptionFn { - return func(w *Worker) error { - conn := redis.NewClient(&redis.Options{ - Addr: c.CacheAddr, - Password: c.CachePassword, - DB: 0, - }) +// func WithCache(c *cnf.Config) OptionFn { +// return func(w *Worker) error { +// conn := redis.NewClient(&redis.Options{ +// Addr: c.CacheAddr, +// Password: c.CachePassword, +// DB: 0, +// }) - w.Cache = conn +// w.Cache = conn - return nil - } -} +// return nil +// } +// } -func WithDatabase(c *cnf.Config) OptionFn { - return func(w *Worker) error { - conn, err := database.Connect(c.DbURL) - if err != nil { - w.Logger.Log("Failed to connect to Database server: %v\n", err) - os.Exit(1) - } +// func WithDatabase(c *cnf.Config) OptionFn { +// return func(w *Worker) error { +// conn, err := database.Connect(c.DbURL) +// if err != nil { +// w.Logger.Log("Failed to connect to Database server: %v\n", err) +// os.Exit(1) +// } - w.Database = conn +// w.Database = conn - return nil - } -} +// return nil +// } +// } -func WithEventbus(c *cnf.Config) OptionFn { - return func(w *Worker) error { - _, chn, err := rabbitmq.Open(c.EventBusURL) - if err != nil { - w.Logger.Log("Failed to connect to EventBus server: %v\n", err) - os.Exit(1) - } +// func WithEventbus(c *cnf.Config) OptionFn { +// return func(w *Worker) error { +// _, chn, err := rabbitmq.Open(c.EventBusURL) +// if err != nil { +// w.Logger.Log("Failed to connect to EventBus server: %v\n", err) +// os.Exit(1) +// } - err = rabbitmq.NewExchange(chn, c.EventBusExchange) - if err != nil { - w.Logger.Log("Failed to declare EventBus exchange: %v\n", err) - os.Exit(1) - } +// err = rabbitmq.NewExchange(chn, c.EventBusExchange) +// if err != nil { +// w.Logger.Log("Failed to declare EventBus exchange: %v\n", err) +// os.Exit(1) +// } - _, err = chn.QueueDeclare( - c.EventBusQueue, // name - false, // durable - false, // delete when unused - false, // exclusive - false, // no-wait - nil, // arguments - ) - if err != nil { - w.Logger.Log("Failed to declare EventBus queue: %v\n", err) - os.Exit(1) - } +// _, err = chn.QueueDeclare( +// c.EventBusQueue, // name +// false, // durable +// false, // delete when unused +// false, // exclusive +// false, // no-wait +// nil, // arguments +// ) +// if err != nil { +// w.Logger.Log("Failed to declare EventBus queue: %v\n", err) +// os.Exit(1) +// } - // w.bindQueues() - rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.productAddedToBasket") - rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.productRemovedFromBasket") - rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.updateQuantity") +// // w.bindQueues() +// rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.productAddedToBasket") +// rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.productRemovedFromBasket") +// rabbitmq.BindQueueToExchange(chn, c.EventBusQueue, c.EventBusExchange, "catalog.basket.updateQuantity") - w.Eventbus = chn +// w.Eventbus = chn - return nil - } -} +// return nil +// } +// } -func WithLogger(c *cnf.Config) OptionFn { - return func(w *Worker) error { - logHost, logPort, err := fluentd.ParseAddr(c.LoggerAddr) - if err != nil { - w.Logger.Log("Failed to parse Fluentd address: %s. Err: %v", c.LoggerAddr, err) - os.Exit(1) - } +// func WithLogger(c *cnf.Config) OptionFn { +// return func(w *Worker) error { +// logHost, logPort, err := fluentd.ParseAddr(c.LoggerAddr) +// if err != nil { +// w.Logger.Log("Failed to parse Fluentd address: %s. Err: %v", c.LoggerAddr, err) +// os.Exit(1) +// } - logger, err := fluentd.NewLogger(c.Base.GetAppFullName(), logHost, logPort) - if err != nil { - w.Logger.Log("Failed to connect to the Fluentd on %s:%d. Err: %v", logHost, logPort, err) - os.Exit(1) - } +// logger, err := fluentd.NewLogger(c.Base.GetAppFullName(), logHost, logPort) +// if err != nil { +// w.Logger.Log("Failed to connect to the Fluentd on %s:%d. Err: %v", logHost, logPort, err) +// os.Exit(1) +// } - w.Logger = logger +// w.Logger = logger - return nil - } -} +// return nil +// } +// } -func WithRegistry(c *cnf.Config) OptionFn { - return func(w *Worker) error { - registry, err := consul.NewService(c.RegistryAddr, c.Base.AppID, c.Base.AppName, c.Base.AppID, "", "", 0) - if err != nil { - w.Logger.Log("Error connecting to %s: %v", c.RegistryAddr, err) - } +// // func WithRegistry(c *cnf.Config) OptionFn { +// // return func(w *Worker) error { +// // registry, err := consul.NewService(c.RegistryAddr, c.Base.AppID, c.Base.AppName, c.Base.AppID, "", "", 0) +// // if err != nil { +// // w.Logger.Log("Error connecting to %s: %v", c.RegistryAddr, err) +// // } - w.Registry = registry +// // w.Registry = registry - go func(w *Worker) { // Fetch Consul KV config and store it in app config - ticker := time.NewTicker(time.Second * 15) - for range ticker.C { - fetchKVConfig(w) // FIXME: duplicated in server - } - }(w) +// // go func(w *Worker) { // Fetch Consul KV config and store it in app config +// // ticker := time.NewTicker(time.Second * 15) +// // for range ticker.C { +// // fetchKVConfig(w) // FIXME: duplicated in server +// // } +// // }(w) - return nil - } +// // return nil +// // } -} +// // } -func fetchKVConfig(w *Worker) { // @FIXME: merge duplication in server.go and worker.go - config, _, err := w.Registry.KV().Get(w.Config.KVNamespace, nil) - if err != nil || config == nil { - return - } +// func fetchKVConfig(w *Worker) { // @FIXME: merge duplication in server.go and worker.go +// config, _, err := w.Registry.KV().Get(w.Config.KVNamespace, nil) +// if err != nil || config == nil { +// return +// } - kvCnf := bytes.NewBuffer(config.Value) - decoder := json.NewDecoder(kvCnf) - if err := decoder.Decode(&w.Config); err != nil { - return - } -} +// kvCnf := bytes.NewBuffer(config.Value) +// decoder := json.NewDecoder(kvCnf) +// if err := decoder.Decode(&w.Config); err != nil { +// return +// } +// } diff --git a/src/internal/worker/worker.go b/src/internal/worker/worker.go index c0bbb5a..6667f4b 100644 --- a/src/internal/worker/worker.go +++ b/src/internal/worker/worker.go @@ -4,125 +4,156 @@ import ( "fmt" "log" "os" - "os/signal" - "strconv" "strings" - "syscall" "github.com/go-redis/redis/v8" "github.com/jackc/pgx/v5/pgxpool" - "github.com/streadway/amqp" + amqp "github.com/rabbitmq/amqp091-go" - "git.pbiernat.dev/egommerce/go-api-pkg/consul" - "git.pbiernat.dev/egommerce/go-api-pkg/fluentd" - "git.pbiernat.dev/egommerce/go-api-pkg/rabbitmq" + "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + "git.pbiernat.io/egommerce/go-api-pkg/rabbitmq" - cnf "git.pbiernat.dev/egommerce/basket-service/internal/config" - "git.pbiernat.dev/egommerce/basket-service/internal/event" - "git.pbiernat.dev/egommerce/basket-service/internal/service" + "git.pbiernat.io/egommerce/basket-service/internal/event" + "git.pbiernat.io/egommerce/basket-service/internal/service" ) type ( Worker struct { - Config *cnf.Config - Cache *redis.Client - Database *pgxpool.Pool - Eventbus *amqp.Channel - Logger *fluentd.Logger - Registry *consul.Service + ID string + cnf *Config + handlers map[string]any + services map[string]any + doWrkUntil chan struct{} } - OptionFn func(*Worker) error // FIXME: similar in server/server.go ) -func New(c *cnf.Config, opts ...OptionFn) *Worker { - w := &Worker{ - Config: c, +func New(c *Config) *Worker { + return &Worker{ + ID: c.ID, + cnf: c, + handlers: make(map[string]any), + services: make(map[string]any), + doWrkUntil: make(chan struct{}), } - - for _, opt := range opts { - if err := opt(w); err != nil { - log.Fatalf("Failed to attach extension to the Worker. Err: %v\n", err) - } - } - - return w } -func (w *Worker) Start(while chan struct{}) error { - go func() { - sigint := make(chan os.Signal, 1) - signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - <-sigint - - w.Shutdown() - close(while) - }() - - run := w.createRunFile("/app.run") // TODO move to common library (shared between server and worker) - defer w.removeRunFile(run) - - err := w.doWork() +func (w *Worker) Start() error { + // Init + err := rabbitmq.NewExchange(w.GetEventBus(), w.cnf.EventBusExchange) if err != nil { - log.Fatalf("Failed to start worker: %s. Reason: %v\n", w.Config.Base.AppID, err) - close(while) - } + w.GetLogger().Log("Failed to declare EventBus exchange: %v\n", err) + fmt.Printf("Failed to declare EventBus exchange: %v\n", err) - w.Logger.Log("Waiting for messages...") - - return nil -} - -func (w *Worker) Shutdown() error { - w.Logger.Log("Worker %s is going down...", w.Config.Base.AppID) - - w.Registry.Unregister() - w.Eventbus.Close() - w.Database.Close() - w.Logger.Log("Gone.") - w.Logger.Close() - - return nil -} - -func (w *Worker) createRunFile(path string) *os.File { - run, err := os.Create(path) - if err != nil { - log.Fatalf("Failed to create run file. Reason: %v\n", err) os.Exit(1) } - run.WriteString(strconv.Itoa(os.Getpid())) - return run -} - -func (w *Worker) removeRunFile(f *os.File) error { - return f.Close() -} - -func (w *Worker) doWork() error { - msgs, err := w.Eventbus.Consume( - w.Config.EventBusQueue, // queue - "", // consumer - false, // auto-ack - false, // exclusive - false, // no-local - false, // no-wait - nil, // args + _, err = w.GetEventBus().QueueDeclare( + w.cnf.EventBusQueue, // name + false, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + nil, // arguments ) if err != nil { - w.Logger.Log("Failed to register a consumer: %s", err) + w.GetLogger().Log("Failed to declare EventBus queue: %v\n", err) + fmt.Printf("Failed to declare EventBus queue: %v\n", err) + + os.Exit(1) + } + + // // w.bindQueues() + rabbitmq.BindQueueToExchange(w.GetEventBus(), w.cnf.EventBusQueue, w.cnf.EventBusExchange, "catalog.basket.productAddedToBasket") + rabbitmq.BindQueueToExchange(w.GetEventBus(), w.cnf.EventBusQueue, w.cnf.EventBusExchange, "catalog.basket.productRemovedFromBasket") + rabbitmq.BindQueueToExchange(w.GetEventBus(), w.cnf.EventBusQueue, w.cnf.EventBusExchange, "catalog.basket.updateQuantity") + + err = w.doWork(w.doWrkUntil) + if err != nil { + log.Fatalf("Failed to start worker: %s. Reason: %v\n", w.ID, err) + close(w.doWrkUntil) + } + <-w.doWrkUntil + + return err + + // go func() { + // sigint := make(chan os.Signal, 1) + // signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + // <-sigint + + // w.Shutdown() + // close(while) + // }() + + // run := w.createRunFile("./app.run") // TODO move to common library (shared between server and worker) + // defer w.removeRunFile(run) + + // w.Logger.Log("Waiting for messages...") + + // return nil +} + +func (w *Worker) RegisterHandler(name string, fn func() any) { + // fmt.Printf("Registering plugin( with handler): %s... OK\n", name) + w.handlers[name] = fn() +} + +func (w *Worker) OnShutdown() { + w.GetLogger().Log("Worker %s is going down...", w.ID) + // fmt.Printf("Worker %s is going down...\n", w.ID) + w.GetEventBus().Close() + w.GetDatabase().Close() + w.GetLogger().Log("Gone.") + w.GetLogger().Close() + + close(w.doWrkUntil) +} + +// Plugin helper funcitons +func (w *Worker) GetCache() *redis.Client { + return (w.handlers["cache"]).(*redis.Client) +} + +func (w *Worker) GetDatabase() *pgxpool.Pool { // FIXME hardcoded index issue + return (w.handlers["database"]).(*pgxpool.Pool) +} + +func (w *Worker) GetEventBus() *amqp.Channel { + return (w.handlers["eventbus"]).(*amqp.Channel) +} + +func (w *Worker) GetLogger() *fluentd.Logger { + return (w.handlers["logger"]).(*fluentd.Logger) +} + +func (w *Worker) doWork(while chan struct{}) error { + w.services["basket"] = + service.NewBasketService(w.GetDatabase(), w.GetCache(), w.GetEventBus(), w.GetLogger()) + + bSrv := (w.services["basket"]).(*service.BasketService) + + msgs, err := w.GetEventBus().Consume( + w.cnf.EventBusQueue, // queue + "", // consumer + false, // auto-ack + false, // exclusive + false, // no-local + false, // no-wait + nil, // args + ) + if err != nil { + w.GetLogger().Log("Failed to register a consumer: %s", err) os.Exit(1) } go func() { - basketSrvc := service.NewBasketService(w.Database, w.Cache, w.Eventbus, w.Logger) - for d := range msgs { - go func(d amqp.Delivery) { - w.processMsg(basketSrvc, d) - }(d) + // go func(d amqp.Delivery) { + w.processMsg(bSrv, d) + // }(d) } }() + <-while return nil } @@ -130,7 +161,7 @@ func (w *Worker) doWork() error { func (w *Worker) processMsg(srvc *service.BasketService, m amqp.Delivery) { msg, err := rabbitmq.Deserialize(m.Body) if err != nil { - w.Logger.Log("Deserialization error: %v\n", err) + w.GetLogger().Log("Deserialization error: %v\n", err) m.Reject(false) return @@ -140,14 +171,14 @@ func (w *Worker) processMsg(srvc *service.BasketService, m amqp.Delivery) { data := (msg["data"]).(map[string]interface{}) // reqID := (data["request_id"]).(string) // FIXME Check input params! - w.Logger.Log("Processing message \"%s\" with data: %v\n", name, data) + w.GetLogger().Log("Processing message \"%s\" with data: %v\n", name, data) var ok = false switch true { // Refactor -> use case for polymorphism case strings.Contains(name, event.EVENT_PRODUCT_ADDED_TO_BASKET): - w.Logger.Log("Event: %s", event.EVENT_PRODUCT_ADDED_TO_BASKET) + w.GetLogger().Log("Event: %s", event.EVENT_PRODUCT_ADDED_TO_BASKET) case strings.Contains(name, event.EVENT_PRODUCT_REMOVED_FROM_BASKET): - w.Logger.Log("Event: %s", event.EVENT_PRODUCT_REMOVED_FROM_BASKET) + w.GetLogger().Log("Event: %s", event.EVENT_PRODUCT_REMOVED_FROM_BASKET) // case strings.Contains(name, event.EVENT_BASKET_CHECKOUT): // w.Logger.Log("Event: %s", event.EVENT_BASKET_CHECKOUT) } @@ -169,11 +200,11 @@ func (w *Worker) processMsg(srvc *service.BasketService, m amqp.Delivery) { rnr := NewCommandRunner((data["command"]).(string), srvc) ok, _ = rnr.run(data) if ok { - w.Logger.Log("Successful executed message \"%s\"\n", name) + w.GetLogger().Log("Successful executed message \"%s\"\n", name) m.Ack(false) return } - w.Logger.Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) + w.GetLogger().Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) m.Reject(false) // FIXME: or Nack(repeat until success - maybe message shout know...? } diff --git a/src/pkg/server/config.go b/src/pkg/server/config.go deleted file mode 100644 index 577644f..0000000 --- a/src/pkg/server/config.go +++ /dev/null @@ -1,34 +0,0 @@ -package server - -import ( - "fmt" - "net" - "os" - "time" -) - -type Config struct { - AppID string - AppName string - AppDomain string - NetAddr string - PathPrefix string - - IdleTimeout time.Duration // miliseconds - ReadTimeout time.Duration // miliseconds - WriteTimeout time.Duration // miliseconds -} - -func (c *Config) GetAppFullName() string { - return fmt.Sprintf("%s_%s", c.AppName, c.AppID) -} - -func (c *Config) GetIP() string { - host, _ := os.Hostname() - ips, _ := net.LookupIP(host) - for _, ip := range ips { - return ip.String() - } - - return host -} diff --git a/src/pkg/server/server.go b/src/pkg/server/server.go deleted file mode 100644 index a86e899..0000000 --- a/src/pkg/server/server.go +++ /dev/null @@ -1,101 +0,0 @@ -package server - -import ( - "log" - "net" - "os" - "os/signal" - "strconv" - "syscall" - "time" - - "github.com/gofiber/fiber/v2" - - "git.pbiernat.dev/egommerce/api-entities/http" -) - -type ( - Server struct { - *fiber.App - *Config - - addr string // e.g. "127.0.0.1:80" - - ShutdownFn - } - HeaderRequestID struct { - RequestID string `reqHeader:"x-request-id"` - } - OptionFn func(*Server) error - ShutdownFn func() error -) - -func New(conf *Config) *Server { - return &Server{ - App: fiber.New(fiber.Config{ - AppName: conf.AppID, - ServerHeader: conf.AppName + ":" + conf.AppID, - ReadTimeout: conf.ReadTimeout * time.Millisecond, - WriteTimeout: conf.WriteTimeout * time.Millisecond, - IdleTimeout: conf.IdleTimeout * time.Millisecond, - }), - Config: conf, - addr: conf.NetAddr, - } -} - -func (s *Server) Start(while chan struct{}) error { - go func() { - sigint := make(chan os.Signal, 1) - signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - <-sigint - - if err := s.ShutdownFn(); err != nil { - log.Fatalf("Failed to shutdown server. Reason: %v\n", err) - } - - close(while) - }() - - run := s.createRunFile("./app.run") // FIXME path... TODO move to common library (shared between server and worker) - defer s.removeRunFile(run) - - ln, _ := net.Listen("tcp", s.addr) - // ln = tls.NewListener(ln, s.App.Server().TLSConfig) - err := s.Listener(ln) - if err != nil { - log.Fatalf("Failed to start server: %s. Reason: %v\n", s.Config.AppID, err) - close(while) - } - <-while - - return err -} - -func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) { - var hdr = new(HeaderRequestID) - if err := c.ReqHeaderParser(hdr); err != nil { - return "", err - } - - return hdr.RequestID, nil -} - -func (s *Server) Error(c *fiber.Ctx, code int, msg string) error { - return c.Status(code).JSON(http.ErrorResponse{Error: msg}) -} - -func (s *Server) createRunFile(path string) *os.File { - run, err := os.Create(path) - if err != nil { - log.Fatalf("Failed to create run file. Reason: %v\n", err) - os.Exit(1) - } - run.WriteString(strconv.Itoa(os.Getpid())) - - return run -} - -func (s *Server) removeRunFile(f *os.File) error { - return f.Close() -}