refactor, TLS support, v0.5

This commit is contained in:
Piotr Biernat 2024-07-20 19:21:38 +02:00
parent 5317cbe6e7
commit 5b1bf92845
9 changed files with 61 additions and 41 deletions

View File

@ -2,15 +2,13 @@
"ID": "catalog", "ID": "catalog",
"Name": "catalog", "Name": "catalog",
"Address": "__IP__", "Address": "__IP__",
"Tags": ["catalogs-svc", "catalog", "https", "service"], "Tags": ["catalog-svc", "catalog", "https", "service"],
"Port": 80, "Port": 443,
"Connect": { "Connect": {
"Native": true "Native": true
}, },
"Check": { "Check": {
"TCP": "__IP__:80", "TCP": "__IP__:443",
"Interval": "10s", "DeregisterCriticalServiceAfter": "10s"
"Timeout": "10s",
"DeregisterCriticalServiceAfter": "5m"
} }
} }

View File

@ -1,4 +1,4 @@
SERVER_ADDR=:80 SERVER_ADDR=:443
APP_NAME=catalog-svc APP_NAME=catalog-svc
APP_DOMAIN=catalog.service.ego.io APP_DOMAIN=catalog.service.ego.io

View File

@ -28,7 +28,7 @@ RUN chmod 755 /bin/entrypoint.sh /bin/migrate.sh
RUN apk add curl RUN apk add curl
EXPOSE 80 EXPOSE 443
ENTRYPOINT ["entrypoint.sh"] ENTRYPOINT ["entrypoint.sh"]
CMD ["sh", "-c", "/app"] CMD ["sh", "-c", "/app"]

View File

@ -55,7 +55,7 @@ func main() {
log.Fatalf("Error parsing logger addr: %s. Err: %v", c.LoggerAddr, err) 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 { if err != nil {
log.Fatalf("Error connecting to %s:%d. Err: %v", logHost, logPort, err) log.Fatalf("Error connecting to %s:%d. Err: %v", logHost, logPort, err)
} }

View File

@ -4,7 +4,7 @@ go 1.18
require ( require (
git.pbiernat.io/egommerce/api-entities v0.2.3 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/georgysavva/scany/v2 v2.0.0
github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/migrations/v8 v8.1.0
github.com/go-pg/pg/v10 v10.11.1 github.com/go-pg/pg/v10 v10.11.1

View File

@ -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= 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 h1:mR6EYfZkAzh4teydb7KXDBWoxwVW3qasnmmH5J3mnas=
git.pbiernat.io/egommerce/api-entities v0.2.3/go.mod h1:INXAG5x4+i+vNwg1NpfPHiDW8nY1kn1K7pgLOtX+/I0= 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.3.18 h1:0+C9BMsllrNvRbh4kb7dJ5lrzP1Lc7J4pb+KV76YrXk=
git.pbiernat.io/egommerce/go-api-pkg v0.2.88/go.mod h1:XIy2mmvRNIzQmYIUAcDZafhRPxTQFS2HDmsK7ZQ6980= 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/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 v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM= github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM=

View File

@ -18,7 +18,7 @@ const (
defEventBusURL = "amqp://guest:guest@api-eventbus:5672" defEventBusURL = "amqp://guest:guest@api-eventbus:5672"
defKVNmspc = "dev.egommerce/service/catalog" defKVNmspc = "dev.egommerce/service/catalog"
defLoggerAddr = "api-logger:24224" defLoggerAddr = "api-logger:24224"
defNetAddr = ":80" defNetAddr = ":443"
defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017" defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017"
defPathPrefix = "/catalog" defPathPrefix = "/catalog"
defRegistryAddr = "api-registry:8500" defRegistryAddr = "api-registry:8500"

View File

@ -1,6 +1,8 @@
package server package server
import ( import (
"crypto/tls"
"log"
"net" "net"
"time" "time"
@ -19,7 +21,7 @@ type (
*fiber.App *fiber.App
ID string ID string
addr string // e.g. "127.0.0.1:80" addr string // e.g. "127.0.0.1:443"
handlers map[string]any handlers map[string]any
} }
HeaderRequestID struct { HeaderRequestID struct {
@ -47,8 +49,14 @@ func (s *Server) Start() error {
SetupRouter(s) SetupRouter(s)
// fmt.Printf("Starting server at: %s...\n", s.addr) // 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, _ := net.Listen("tcp", s.addr)
// ln = tls.NewListener(ln, s.App.Server().TLSConfig) ln = tls.NewListener(ln, tlsCnf)
return s.Listener(ln) return s.Listener(ln)
} }

View File

@ -38,31 +38,9 @@ func New(c *Config) *Worker {
} }
func (w *Worker) Start() error { func (w *Worker) Start() error {
// Init setupQueues(w)
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) err := w.doWork(w.doWrkUntil)
}
_, 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)
if err != nil { if err != nil {
log.Fatalf("Failed to start worker: %s. Reason: %v\n", w.ID, err) log.Fatalf("Failed to start worker: %s. Reason: %v\n", w.ID, err)
close(w.doWrkUntil) close(w.doWrkUntil)
@ -96,6 +74,8 @@ func (w *Worker) RegisterHandler(name string, fn func() any) {
func (w *Worker) OnShutdown() { func (w *Worker) OnShutdown() {
w.GetLogger().Log("Worker %s is going down...", w.ID) w.GetLogger().Log("Worker %s is going down...", w.ID)
// fmt.Printf("Worker %s is going down...\n", w.ID) // fmt.Printf("Worker %s is going down...\n", w.ID)
unbindQueues(w)
w.GetEventBus().Close() w.GetEventBus().Close()
w.GetDatabase().Close() w.GetDatabase().Close()
w.GetLogger().Log("Gone.") w.GetLogger().Log("Gone.")
@ -159,6 +139,7 @@ func (w *Worker) processMsg(cSrv *service.CatalogService, d amqp.Delivery) {
msg, err := rabbitmq.Deserialize(d.Body) msg, err := rabbitmq.Deserialize(d.Body)
if err != nil { if err != nil {
w.GetLogger().Log("Deserialization error: %v\n", err) w.GetLogger().Log("Deserialization error: %v\n", err)
fmt.Printf("Deserialization error: %v\n", err)
d.Reject(false) d.Reject(false)
return return
@ -169,6 +150,7 @@ func (w *Worker) processMsg(cSrv *service.CatalogService, d amqp.Delivery) {
// reqID := (data["request_id"]).(string) // FIXME Check input params! // reqID := (data["request_id"]).(string) // FIXME Check input params!
w.GetLogger().Log("Processing message \"%s\" with data: %v\n", name, data) 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 var ok = false
switch true { // Refactor -> use case for polymorphism 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) ok, _ = rnr.run(data)
if ok { if ok {
w.GetLogger().Log("Successful executed message \"%s\"\n", name) w.GetLogger().Log("Successful executed message \"%s\"\n", name)
fmt.Printf("Successful executed message \"%s\"\n", name)
d.Ack(false) d.Ack(false)
return return
} }
w.GetLogger().Log("Error processing \"%s\": %s (%v)", name, err.Error(), err) w.GetLogger().Log("Error processing \"%s\": %v\n", name, err)
d.Reject(false) // FIXME: or Nack(repeat until success - maybe message shoud know...? 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) {
} }