Skip to content

Commit

Permalink
feat: add app pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
ucpr committed Dec 29, 2023
1 parent 64d5da7 commit 8d798ab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
5 changes: 3 additions & 2 deletions cmd/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/ucpr/mongo-streamer/internal/app"
"github.com/ucpr/mongo-streamer/internal/config"
"github.com/ucpr/mongo-streamer/internal/mongo"
"github.com/ucpr/mongo-streamer/internal/persistent"
Expand All @@ -16,15 +17,15 @@ type Streamer struct {
st persistent.StorageBuffer
}

func NewStreamer(ctx context.Context, cli *mongo.Client, mcfg *config.MongoDB, eh *EventHandler) (*Streamer, error) {
func NewStreamer(ctx context.Context, cli *mongo.Client, mcfg *config.MongoDB, h *app.Handler) (*Streamer, error) {
stLog := persistent.NewLogWriter()
st, err := persistent.NewBuffer(10, 5*time.Second, stLog)
if err != nil {
return nil, err
}
cs, err := mongo.NewChangeStream(ctx, mongo.ChangeStreamParams{
Client: cli,
Handler: eh.EventHandler,
Handler: h.EventHandler,
Storage: st,
Database: mcfg.Database,
Collection: mcfg.Collection,
Expand Down
3 changes: 2 additions & 1 deletion cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/google/wire"

"github.com/ucpr/mongo-streamer/internal/app"
"github.com/ucpr/mongo-streamer/internal/config"
"github.com/ucpr/mongo-streamer/internal/http"
"github.com/ucpr/mongo-streamer/internal/mongo"
Expand All @@ -18,8 +19,8 @@ func injectStreamer(ctx context.Context) (*Streamer, error) {
config.Set,
mongo.Set,
pubsub.Set,
app.NewHandler,
NewStreamer,
NewEventHandler,
)
return nil, nil
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions cmd/handler.go → internal/app/streamer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"context"
Expand All @@ -8,18 +8,17 @@ import (
"github.com/ucpr/mongo-streamer/pkg/log"
)

type EventHandler struct {
type Handler struct {
pubsub pubsub.Publisher
}

func NewEventHandler(ps pubsub.Publisher) *EventHandler {
return &EventHandler{
func NewHandler(ps pubsub.Publisher) *Handler {
return &Handler{
pubsub: ps,
}
}

func (e *EventHandler) EventHandler(ctx context.Context, event []byte) error {
log.Info("event", slog.String("event", string(event)))
func (e *Handler) EventHandler(ctx context.Context, event []byte) error {
res := e.pubsub.AsyncPublish(ctx, pubsub.Message{
Data: event,
})
Expand Down

0 comments on commit 8d798ab

Please sign in to comment.