|
1 | 1 | # Google Cloud Platform PubSub
|
2 | 2 |
|
3 |
| -## Add to your `candi` service |
| 3 | +## Install this plugin in your `candi` service |
| 4 | + |
| 5 | +### Add in service.go |
| 6 | + |
4 | 7 | ```go
|
5 | 8 | package service
|
6 | 9 |
|
@@ -30,3 +33,81 @@ func NewService(cfg *config.Config) factory.ServiceFactory {
|
30 | 33 | }
|
31 | 34 | ...
|
32 | 35 | ```
|
| 36 | +
|
| 37 | +### Register in module.go |
| 38 | +
|
| 39 | +```go |
| 40 | +package examplemodule |
| 41 | + |
| 42 | +import ( |
| 43 | + "example.service/internal/modules/examplemodule/delivery/workerhandler" |
| 44 | + |
| 45 | + "github.com/agungdwiprasetyo/candi-plugin/worker/gcppubsub" |
| 46 | + |
| 47 | + "pkg.agungdp.dev/candi/codebase/factory/dependency" |
| 48 | + "pkg.agungdp.dev/candi/codebase/factory/types" |
| 49 | + "pkg.agungdp.dev/candi/codebase/interfaces" |
| 50 | +) |
| 51 | + |
| 52 | +type Module struct { |
| 53 | + // ...another delivery handler |
| 54 | + workerHandlers map[types.Worker]interfaces.WorkerHandler |
| 55 | +} |
| 56 | + |
| 57 | +func NewModules(deps dependency.Dependency) *Module { |
| 58 | + return &Module{ |
| 59 | + workerHandlers: map[types.Worker]interfaces.WorkerHandler{ |
| 60 | + // ...another worker handler |
| 61 | + // ... |
| 62 | + gcppubsub.GoogleCloudPubSub: workerhandler.NewGCPPubSubHandler(usecaseUOW.User(), deps.GetValidator()), |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// ...another method |
| 68 | +``` |
| 69 | +
|
| 70 | +### Create delivery handler |
| 71 | +
|
| 72 | +```go |
| 73 | +package workerhandler |
| 74 | + |
| 75 | +import ( |
| 76 | + "context" |
| 77 | + "encoding/json" |
| 78 | + |
| 79 | + "example.service/internal/modules/examplemodule/delivery/workerhandler" |
| 80 | + |
| 81 | + "pkg.agungdp.dev/candi/codebase/factory/types" |
| 82 | + "pkg.agungdp.dev/candi/tracer" |
| 83 | +) |
| 84 | + |
| 85 | +// GCPPubSubHandler struct |
| 86 | +type GCPPubSubHandler struct { |
| 87 | + uc usecase.Usecase |
| 88 | + validator interfaces.Validator |
| 89 | +} |
| 90 | + |
| 91 | +// NewGCPPubSubHandler constructor |
| 92 | +func NewGCPPubSubHandler(uc usecase.Usecase, validator interfaces.Validator) *GCPPubSubHandler { |
| 93 | + return &GCPPubSubHandler{ |
| 94 | + uc: uc, |
| 95 | + validator: validator, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// MountHandlers mount handler group |
| 100 | +func (h *GCPPubSubHandler) MountHandlers(group *types.WorkerHandlerGroup) { |
| 101 | + group.Add("example-topic", h.handleTopic) // consume topic "example-topic" |
| 102 | +} |
| 103 | + |
| 104 | +func (h *GCPPubSubHandler) handleTopic(ctx context.Context, message []byte) error { |
| 105 | + trace := tracer.StartTrace(ctx, "DeliveryGCPPubSub:HandleTopic") |
| 106 | + defer trace.Finish() |
| 107 | + ctx = trace.Context() |
| 108 | + |
| 109 | + log.Printf("message consumed. message: %s\n", message) |
| 110 | + // call usecase |
| 111 | + return nil |
| 112 | +} |
| 113 | +``` |
0 commit comments