diff --git a/.app.config b/.app.config index b839748..1380275 100644 --- a/.app.config +++ b/.app.config @@ -2,15 +2,13 @@ "ID": "catalog", "Name": "catalog", "Address": "__IP__", - "Tags": ["catalogs-svc", "catalog", "https", "service"], - "Port": 80, + "Tags": ["catalog-svc", "catalog", "https", "service"], + "Port": 443, "Connect": { "Native": true }, "Check": { - "TCP": "__IP__:80", - "Interval": "10s", - "Timeout": "10s", - "DeregisterCriticalServiceAfter": "5m" + "TCP": "__IP__:443", + "DeregisterCriticalServiceAfter": "10s" } } \ No newline at end of file diff --git a/.env.dist b/.env.dist index a60f53e..24c4ecb 100644 --- a/.env.dist +++ b/.env.dist @@ -1,4 +1,4 @@ -SERVER_ADDR=:80 +SERVER_ADDR=:443 APP_NAME=catalog-svc APP_DOMAIN=catalog.service.ego.io diff --git a/Dockerfile.target b/Dockerfile.target index 4547452..1f0475d 100644 --- a/Dockerfile.target +++ b/Dockerfile.target @@ -28,7 +28,7 @@ RUN chmod 755 /bin/entrypoint.sh /bin/migrate.sh RUN apk add curl -EXPOSE 80 +EXPOSE 443 ENTRYPOINT ["entrypoint.sh"] CMD ["sh", "-c", "/app"] diff --git a/src/cmd/migrate/main.go b/src/cmd/migrate/main.go index 70e2ccc..8170613 100644 --- a/src/cmd/migrate/main.go +++ b/src/cmd/migrate/main.go @@ -55,7 +55,7 @@ func main() { log.Fatalf("Error parsing logger addr: %s. Err: %v", c.LoggerAddr, err) } - logger, err := fluentd.NewLogger(c.Base.GetAppFullName(), logHost, logPort) // @Refactor NewLogger return (logger, error) + logger, err := fluentd.NewLogger(c.GetAppFullName(), logHost, logPort) // @Refactor NewLogger return (logger, error) if err != nil { log.Fatalf("Error connecting to %s:%d. Err: %v", logHost, logPort, err) } diff --git a/src/go.mod b/src/go.mod index 5f62dd6..9da9103 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( git.pbiernat.io/egommerce/api-entities v0.2.3 - git.pbiernat.io/egommerce/go-api-pkg v0.2.88 + git.pbiernat.io/egommerce/go-api-pkg v0.3.18 github.com/georgysavva/scany/v2 v2.0.0 github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/pg/v10 v10.11.1 diff --git a/src/go.sum b/src/go.sum index 83bf0eb..829d9e7 100644 --- a/src/go.sum +++ b/src/go.sum @@ -37,8 +37,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= 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= +git.pbiernat.io/egommerce/go-api-pkg v0.3.18 h1:0+C9BMsllrNvRbh4kb7dJ5lrzP1Lc7J4pb+KV76YrXk= +git.pbiernat.io/egommerce/go-api-pkg v0.3.18/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= diff --git a/src/internal/server/config.go b/src/internal/server/config.go index b32d91d..f8e927b 100644 --- a/src/internal/server/config.go +++ b/src/internal/server/config.go @@ -18,7 +18,7 @@ const ( defEventBusURL = "amqp://guest:guest@api-eventbus:5672" defKVNmspc = "dev.egommerce/service/catalog" defLoggerAddr = "api-logger:24224" - defNetAddr = ":80" + defNetAddr = ":443" defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017" defPathPrefix = "/catalog" defRegistryAddr = "api-registry:8500" diff --git a/src/internal/server/server.go b/src/internal/server/server.go index 5d1d426..3e5d21a 100644 --- a/src/internal/server/server.go +++ b/src/internal/server/server.go @@ -1,6 +1,8 @@ package server import ( + "crypto/tls" + "log" "net" "time" @@ -19,7 +21,7 @@ type ( *fiber.App ID string - addr string // e.g. "127.0.0.1:80" + addr string // e.g. "127.0.0.1:443" handlers map[string]any } HeaderRequestID struct { @@ -47,8 +49,14 @@ func (s *Server) Start() error { SetupRouter(s) // fmt.Printf("Starting server at: %s...\n", s.addr) + cer, err := tls.LoadX509KeyPair("certs/client.crt", "certs/client.key") + if err != nil { + log.Fatal(err) + } + tlsCnf := &tls.Config{Certificates: []tls.Certificate{cer}} + ln, _ := net.Listen("tcp", s.addr) - // ln = tls.NewListener(ln, s.App.Server().TLSConfig) + ln = tls.NewListener(ln, tlsCnf) return s.Listener(ln) } diff --git a/src/internal/worker/worker.go b/src/internal/worker/worker.go index e84dcd7..c1dc946 100644 --- a/src/internal/worker/worker.go +++ b/src/internal/worker/worker.go @@ -38,31 +38,9 @@ func New(c *Config) *Worker { } func (w *Worker) Start() error { - // Init - err := rabbitmq.NewExchange(w.GetEventBus(), w.cnf.EventBusExchange) - if err != nil { - w.GetLogger().Log("Failed to declare EventBus exchange: %v\n", err) - fmt.Printf("Failed to declare EventBus exchange: %v\n", err) + setupQueues(w) - os.Exit(1) - } - - _, 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.GetLogger().Log("Failed to declare EventBus queue: %v\n", err) - fmt.Printf("Failed to declare EventBus queue: %v\n", err) - - os.Exit(1) - } - - err = w.doWork(w.doWrkUntil) + err := w.doWork(w.doWrkUntil) if err != nil { log.Fatalf("Failed to start worker: %s. Reason: %v\n", w.ID, err) close(w.doWrkUntil) @@ -96,6 +74,8 @@ func (w *Worker) RegisterHandler(name string, fn func() any) { func (w *Worker) OnShutdown() { w.GetLogger().Log("Worker %s is going down...", w.ID) // fmt.Printf("Worker %s is going down...\n", w.ID) + + unbindQueues(w) w.GetEventBus().Close() w.GetDatabase().Close() w.GetLogger().Log("Gone.") @@ -159,6 +139,7 @@ func (w *Worker) processMsg(cSrv *service.CatalogService, d amqp.Delivery) { msg, err := rabbitmq.Deserialize(d.Body) if err != nil { w.GetLogger().Log("Deserialization error: %v\n", err) + fmt.Printf("Deserialization error: %v\n", err) d.Reject(false) return @@ -169,6 +150,7 @@ func (w *Worker) processMsg(cSrv *service.CatalogService, d amqp.Delivery) { // reqID := (data["request_id"]).(string) // FIXME Check input params! w.GetLogger().Log("Processing message \"%s\" with data: %v\n", name, data) + fmt.Printf("Processing message \"%s\" with data: %v\n", name, data) var ok = false switch true { // Refactor -> use case for polymorphism @@ -180,10 +162,42 @@ func (w *Worker) processMsg(cSrv *service.CatalogService, d amqp.Delivery) { ok, _ = rnr.run(data) if ok { w.GetLogger().Log("Successful executed message \"%s\"\n", name) + fmt.Printf("Successful executed message \"%s\"\n", name) d.Ack(false) return } - w.GetLogger().Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) - d.Reject(false) // FIXME: or Nack(repeat until success - maybe message shoud know...? + w.GetLogger().Log("Error processing \"%s\": %v\n", name, err) + fmt.Printf("Error processing \"%s\": %v\n", name, err) + d.Reject(true) // FIXME: or Nack(repeat until success - maybe message shoud know...? +} + +func setupQueues(w *Worker) { + err := rabbitmq.NewExchange(w.GetEventBus(), w.cnf.EventBusExchange) + if err != nil { + w.GetLogger().Log("Failed to declare EventBus exchange: %v\n", err) + fmt.Printf("Failed to declare EventBus exchange: %v\n", err) + + os.Exit(1) + } + + args := amqp.Table{} + args["x-message-ttl"] = 5 + _, err = w.GetEventBus().QueueDeclare( + w.cnf.EventBusQueue, // name + true, // durable + false, // delete when unused + false, // exclusive + false, // no-wait + args, // arguments + ) + if err != nil { + w.GetLogger().Log("Failed to declare EventBus queue: %v\n", err) + fmt.Printf("Failed to declare EventBus queue: %v\n", err) + + os.Exit(1) + } +} + +func unbindQueues(w *Worker) { }