Ver código fonte

Split back/front

Frederic G. MARAND 1 ano atrás
pai
commit
db8cf4b32f

+ 1 - 1
.gitignore

@@ -1,2 +1,2 @@
 /.idea
-/volume
+volume

+ 17 - 17
back/cmd/consumer/main.go

@@ -8,7 +8,7 @@ import (
 
 	"github.com/fgm/izidic"
 
-	"code.osinet.fr/fgm/sqs_demo/di"
+	"code.osinet.fr/fgm/sqs_demo/services"
 )
 
 func main() {
@@ -18,14 +18,14 @@ func main() {
 func main2(w io.Writer, name string, args []string) (exitCode byte) {
 	ctx := context.Background()
 	dic := Resolve(w, name, args)
-	lister := dic.MustService(di.Lister).(func(ctx context.Context) string)
+	lister := dic.MustService(services.SvcLister).(func(ctx context.Context) string)
 	qURL := lister(ctx)
 
 	if false {
-		receiver := dic.MustService(di.Receiver).(func(ctx context.Context, qURL string))
+		receiver := dic.MustService(services.SvcReceiver).(func(ctx context.Context, qURL string))
 		receiver(ctx, qURL)
 	}
-	consumer := dic.MustService(di.Consumer).(func(ctx context.Context, qURL string) error)
+	consumer := dic.MustService(services.SvcConsumer).(func(ctx context.Context, qURL string) error)
 	err := consumer(ctx, qURL)
 	if err != nil {
 		log.Printf("error in consumer: %v, aborting", err)
@@ -38,19 +38,19 @@ func main2(w io.Writer, name string, args []string) (exitCode byte) {
 
 func Resolve(w io.Writer, name string, args []string) *izidic.Container {
 	dic := izidic.New()
-	dic.Store("name", name)
-	dic.Store("args", args)
-	dic.Store("writer", w)
-	dic.Store("handler", di.Handler(HandleDummy))
-
-	dic.Register(di.Client, di.SQSClientService)
-	dic.Register(di.Consumer, di.ConsumerService)
-	dic.Register(di.Flags, di.FlagsService)
-	dic.Register(di.Lister, di.ListerService)
-	dic.Register(di.Logger, di.LoggerService)
-	dic.Register(di.Receiver, di.ReceiverService)
-
-	dic.MustService(di.Flags) // Store generated params before freeze.
+	dic.Store(services.PName, name)
+	dic.Store(services.PArgs, args)
+	dic.Store(services.PWriter, w)
+	dic.Store("handler", services.Handler(HandleDummy))
+
+	dic.Register(services.SvcClient, services.SQSClientService)
+	dic.Register(services.SvcConsumer, services.ConsumerService)
+	dic.Register(services.SvcFlags, services.FlagsService)
+	dic.Register(services.SvcLister, services.ListerService)
+	dic.Register(services.SvcLogger, services.LoggerService)
+	dic.Register(services.SvcReceiver, services.ReceiverService)
+
+	dic.MustService(services.SvcFlags) // Store generated params before freeze.
 	dic.Freeze()
 	return dic
 }

+ 12 - 12
back/cmd/producer/main.go

@@ -8,7 +8,7 @@ import (
 
 	"github.com/fgm/izidic"
 
-	"code.osinet.fr/fgm/sqs_demo/di"
+	"code.osinet.fr/fgm/sqs_demo/services"
 )
 
 func main() {
@@ -18,10 +18,10 @@ func main() {
 func main2(w io.Writer, name string, args []string) (exitCode byte) {
 	ctx := context.Background()
 	dic := Resolve(w, name, args)
-	lister := dic.MustService(di.Lister).(func(ctx context.Context) string)
+	lister := dic.MustService(services.SvcLister).(func(ctx context.Context) string)
 	qURL := lister(ctx)
 
-	producer := dic.MustService(di.Producer).(func(ctx context.Context, qName string))
+	producer := dic.MustService(services.SvcProducer).(func(ctx context.Context, qName string))
 	producer(ctx, qURL)
 
 	log.Printf("exiting cleanly")
@@ -30,17 +30,17 @@ func main2(w io.Writer, name string, args []string) (exitCode byte) {
 
 func Resolve(w io.Writer, name string, args []string) *izidic.Container {
 	dic := izidic.New()
-	dic.Store("name", name)
-	dic.Store("args", args)
-	dic.Store("writer", w)
+	dic.Store(services.PName, name)
+	dic.Store(services.PArgs, args)
+	dic.Store(services.PWriter, w)
 
-	dic.Register(di.Client, di.SQSClientService)
-	dic.Register(di.Flags, di.FlagsService)
-	dic.Register(di.Lister, di.ListerService)
-	dic.Register(di.Logger, di.LoggerService)
-	dic.Register(di.Producer, di.ProducerService)
+	dic.Register(services.SvcClient, services.SQSClientService)
+	dic.Register(services.SvcFlags, services.FlagsService)
+	dic.Register(services.SvcLister, services.ListerService)
+	dic.Register(services.SvcLogger, services.LoggerService)
+	dic.Register(services.SvcProducer, services.ProducerService)
 
-	dic.MustService(di.Flags) // Store generated params before freeze.
+	dic.MustService(services.SvcFlags) // Store generated params before freeze.
 	dic.Freeze()
 	return dic
 }

+ 46 - 0
back/cmd/redriver/main.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"context"
+	"io"
+	"log"
+	"os"
+
+	"github.com/fgm/izidic"
+
+	services2 "code.osinet.fr/fgm/sqs_demo/back/services"
+)
+
+func main() {
+	os.Exit(int(main2(os.Stdout, os.Args[0], os.Args[1:])))
+}
+
+func main2(w io.Writer, name string, args []string) (exitCode byte) {
+	ctx := context.Background()
+	dic := Resolve(w, name, args)
+	lister := dic.MustService(services2.SvcLister).(func(ctx context.Context) string)
+	qURL := lister(ctx)
+
+	producer := dic.MustService(services2.SvcProducer).(func(ctx context.Context, qName string))
+	producer(ctx, qURL)
+
+	log.Printf("exiting cleanly")
+	return 0
+}
+
+func Resolve(w io.Writer, name string, args []string) *izidic.Container {
+	dic := izidic.New()
+	dic.Store(services2.PName, name)
+	dic.Store(services2.PArgs, args)
+	dic.Store(services2.PWriter, w)
+
+	dic.Register(services2.SvcClient, services2.SQSClientService)
+	dic.Register(services2.SvcFlags, services2.FlagsService)
+	dic.Register(services2.SvcLister, services2.ListerService)
+	dic.Register(services2.SvcLogger, services2.LoggerService)
+	dic.Register(services2.SvcProducer, services2.ProducerService)
+
+	dic.MustService(services2.SvcFlags) // Store generated params before freeze.
+	dic.Freeze()
+	return dic
+}

+ 0 - 45
back/di/di.go

@@ -1,45 +0,0 @@
-package di
-
-import (
-	"flag"
-	"fmt"
-	"io"
-	"log"
-
-	"github.com/fgm/izidic"
-)
-
-const (
-	Client   = "sqs"
-	Consumer = "consumeMessage"
-	Flags    = "flags"
-	Lister   = "lister"
-	Logger   = "logger"
-	Producer = "producer"
-	Receiver = "receiver"
-)
-
-func FlagsService(dic *izidic.Container) (any, error) {
-	fs := flag.NewFlagSet(dic.MustParam("name").(string), flag.ContinueOnError)
-	profile := fs.String("profile", "test-profile", "The AWS profile")
-	region := fs.String("region", "eu-west-3", "The AWS region to connect to")
-	qName := fs.String("queue-name", "dummy-queue", "The queue name")
-	sqsURL := fs.String("url", "http://localhost:4566", "The SQS endpoint URL")
-	if err := fs.Parse(dic.MustParam("args").([]string)); err != nil {
-		return nil, fmt.Errorf("cannot obtain CLI args")
-	}
-
-	dic.Store("profile", *profile)
-	dic.Store("region", *region)
-	dic.Store("url", *sqsURL)
-	dic.Store("queue-name", *qName)
-	return fs, nil
-}
-
-// LoggerService is an izidic.Service also containing a one-time initialization action.
-func LoggerService(dic *izidic.Container) (any, error) {
-	w := dic.MustParam("writer").(io.Writer)
-	log.SetOutput(w) // Support dependency code not taking an injected logger.
-	logger := log.New(w, "", log.LstdFlags)
-	return logger, nil
-}

+ 3 - 3
back/di/client.go → back/services/client.go

@@ -1,4 +1,4 @@
-package di
+package services
 
 import (
 	"context"
@@ -12,8 +12,8 @@ import (
 
 func SQSClientService(dic *izidic.Container) (any, error) {
 	ctx := context.Background()
-	profile := dic.MustParam("profile").(string)
-	region := dic.MustParam("region").(string)
+	profile := dic.MustParam(PProfile).(string)
+	region := dic.MustParam(PRegion).(string)
 	epr := endpointResolver{region: region}
 	cfg, err := config.LoadDefaultConfig(ctx,
 		config.WithRegion(region),

+ 4 - 4
back/di/consumer.go → back/services/consumer.go

@@ -1,4 +1,4 @@
-package di
+package services
 
 import (
 	"context"
@@ -59,8 +59,8 @@ func (m message) String() string {
 
 func ConsumerService(dic *izidic.Container) (any, error) {
 	cli := dic.MustService("sqs").(*sqs.Client)
-	w := dic.MustParam("writer").(io.Writer)
-	hdl := dic.MustParam("handler").(Handler)
+	w := dic.MustParam(PWriter).(io.Writer)
+	hdl := dic.MustParam(PHandler).(Handler)
 	enc := json.NewEncoder(w)
 	return func(ctx context.Context, qURL string) error {
 		return consumeMessage(ctx, w, enc, cli, qURL, hdl)
@@ -69,7 +69,7 @@ func ConsumerService(dic *izidic.Container) (any, error) {
 
 func ReceiverService(dic *izidic.Container) (any, error) {
 	cli := dic.MustService("sqs").(*sqs.Client)
-	w := dic.MustParam("writer").(io.Writer)
+	w := dic.MustParam(PWriter).(io.Writer)
 	return func(ctx context.Context, qURL string) {
 		receiveMessage(ctx, w, cli, qURL)
 	}, nil

+ 2 - 2
back/di/lister.go → back/services/lister.go

@@ -1,4 +1,4 @@
-package di
+package services
 
 import (
 	"context"
@@ -13,7 +13,7 @@ import (
 
 func ListerService(dic *izidic.Container) (any, error) {
 	cli := dic.MustService("sqs").(*sqs.Client)
-	w := dic.MustParam("writer").(io.Writer)
+	w := dic.MustParam(PWriter).(io.Writer)
 	return func(ctx context.Context) string {
 		return lister(ctx, w, cli)
 	}, nil

+ 2 - 2
back/di/producer.go → back/services/producer.go

@@ -1,4 +1,4 @@
-package di
+package services
 
 import (
 	"context"
@@ -16,7 +16,7 @@ import (
 
 func ProducerService(dic *izidic.Container) (any, error) {
 	cli := dic.MustService("sqs").(*sqs.Client)
-	w := dic.MustParam("writer").(io.Writer)
+	w := dic.MustParam(PWriter).(io.Writer)
 	return func(ctx context.Context, qName string) {
 		senderHandler(ctx, w, cli, qName)
 	}, nil

+ 58 - 0
back/services/redriver.go

@@ -0,0 +1,58 @@
+package services
+
+import (
+	"context"
+	"io"
+
+	"github.com/aws/aws-sdk-go-v2/service/sqs"
+	"github.com/fgm/izidic"
+)
+
+type CreateMoveTaskOutput struct {
+	Status                           *string // Running
+	SourceARN                        *string
+	ApproximateNumberOfMessagesMoved int
+}
+
+type Redriver interface {
+	CreateMoveTask()
+	ListMoveTasks() []any
+}
+
+type redriver struct {
+}
+
+func (r redriver) CreateMoveTask() {
+	// TODO implement me
+	// u := url.URL{
+	// 	Scheme:      "https",
+	// 	User:        nil,
+	// 	Host:        "",
+	// 	Path:        "",
+	// 	RawPath:     "",
+	// 	OmitHost:    false,
+	// 	ForceQuery:  false,
+	// 	RawQuery:    "",
+	// 	Fragment:    "",
+	// 	RawFragment: "",
+	// }
+	/*
+		Action=CreateMoveTask
+		&SourceArn=arn%3Aaws%3Asqs%3Aeu-west-3%3A751146239996%3Atest-dlq
+		&TaskName=079228fe-098b-436d-a2f4-cb4d29ebb55a
+		&Version=2012-11-05
+	*/
+}
+
+func (r redriver) ListMoveTasks() []any {
+	// TODO implement me
+	panic("implement me")
+}
+
+func RedriverService(dic *izidic.Container) (any, error) {
+	cli := dic.MustService(SvcClient).(*sqs.Client)
+	w := dic.MustParam(PWriter).(io.Writer)
+	return func(ctx context.Context, qName string) {
+		senderHandler(ctx, w, cli, qName)
+	}, nil
+}

+ 55 - 0
back/services/services.go

@@ -0,0 +1,55 @@
+package services
+
+import (
+	"flag"
+	"fmt"
+	"io"
+	"log"
+
+	"github.com/fgm/izidic"
+)
+
+const (
+	PArgs    = "args"
+	PHandler = "handler"
+	PName    = "name"
+	PProfile = "profile"
+	PQName   = "queue-name"
+	PRegion  = "region"
+	PURL     = "url"
+	PWriter  = "writer"
+
+	SvcClient   = "sqs"
+	SvcConsumer = "consumeMessage"
+	SvcFlags    = "flags"
+	SvcLister   = "lister"
+	SvcLogger   = "logger"
+	SvcProducer = "producer"
+	SvcReceiver = "receiver"
+	SvcRedriver = "redriver"
+)
+
+func FlagsService(dic *izidic.Container) (any, error) {
+	fs := flag.NewFlagSet(dic.MustParam(PName).(string), flag.ContinueOnError)
+	profile := fs.String(PProfile, "test-profile", "The AWS profile")
+	region := fs.String(PRegion, "eu-west-3", "The AWS region to connect to")
+	qName := fs.String(PQName, "dummy-queue", "The queue name")
+	sqsURL := fs.String(PURL, "http://localhost:4566", "The SQS endpoint URL")
+	if err := fs.Parse(dic.MustParam(PArgs).([]string)); err != nil {
+		return nil, fmt.Errorf("cannot obtain CLI args")
+	}
+
+	dic.Store(PProfile, *profile)
+	dic.Store(PRegion, *region)
+	dic.Store(PURL, *sqsURL)
+	dic.Store(PQName, *qName)
+	return fs, nil
+}
+
+// LoggerService is an izidic.Service also containing a one-time initialization action.
+func LoggerService(dic *izidic.Container) (any, error) {
+	w := dic.MustParam(PWriter).(io.Writer)
+	log.SetOutput(w) // Support dependency code not taking an injected logger.
+	logger := log.New(w, "", log.LstdFlags)
+	return logger, nil
+}