Skip to content

Commit

Permalink
amqp subscriptions working
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Jul 17, 2019
1 parent e59753a commit 0f502a3
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 251 deletions.
10 changes: 5 additions & 5 deletions a_main-packr.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func updateCmd(cmd *cobra.Command, args []string) {
// AMQP SUBSCRIBER BUNDLE
if config.IsSet(bundle.SubscriberKey) {

// subscriber/initialize.go
// amqp/subscriptions.go
amqpSubscriberInit := generate.NewAMQPSubscriber(TemplateFilesystem, service.Interface, tplContext)
fs.MakeDirs([]string{path.Dir(amqpSubscriberInit.TargetPath())}) // ignore errors, just ensure the path exists
if err := amqpSubscriberInit.Update(); err != nil {
Expand All @@ -176,6 +176,7 @@ func updateCmd(cmd *cobra.Command, args []string) {
for _, subscriber := range tplContext.Service.Subscriber {
fileName := bundle.SubscriberFileName(subscriber.Subscription.Topic)
impl := generate.NewAMQPSubscriberHandler(subscriber, TemplateFilesystem, service.Interface, tplContext)
fs.MakeDirs([]string{path.Dir(impl.TargetPath())}) // ignore errors, just ensure the path exists
if err := impl.Update(); err != nil {
logrus.Errorf("failed to update internal/service/subscriber/%s: %s", fileName, err)
} else {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/fatih/structtag v1.0.0 // indirect
github.com/go-godin/rabbitmq v0.0.0-20190716163246-e909e622b8c7
github.com/go-kit/kit v0.8.0
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/gobuffalo/genny v0.0.0-20190403191548-3ca520ef0d9e // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/fatih/structtag v1.0.0 h1:pTHj65+u3RKWYPSGaU290FpI/dXxTaHdVwVwbcPKmEc
github.com/fatih/structtag v1.0.0/go.mod h1:IKitwq45uXL/yqi5mYghiD3w9H6eTOvI9vnk8tXMphA=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-godin/rabbitmq v0.0.0-20190716163246-e909e622b8c7 h1:dYvuqVELcsEcrUD1S+ztnmV5uCis6uh92oFxVcDUQPM=
github.com/go-godin/rabbitmq v0.0.0-20190716163246-e909e622b8c7/go.mod h1:RC4VtaodR+xZa0/+CDeM1cXCSmkxnOYKvtDntFA4lIw=
github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA=
Expand Down
13 changes: 7 additions & 6 deletions internal/bundle/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package bundle

import (
"fmt"
"github.com/iancoleman/strcase"
"strings"

"github.com/iancoleman/strcase"

"github.com/go-godin/rabbitmq"
"github.com/lukasjarosch/godin/internal/godin"
"github.com/lukasjarosch/godin/internal/prompt"
"github.com/lukasjarosch/godin/pkg/amqp"
config "github.com/spf13/viper"
)

const SubscriberKey = "transport.amqp.subscriber"

type subscriber struct {
Subscription amqp.Subscription
Subscription rabbitmq.Subscription
HandlerName string `json:"handler_name"`
}

func InitializeSubscriber() (*subscriber, error) {
sub := amqp.Subscription{}
sub := rabbitmq.Subscription{}
err := promptValues(&sub)
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,7 +68,7 @@ func SubscriberFileName(topic string) string {
return fmt.Sprintf("%s.go", name)
}

func promptValues(sub *amqp.Subscription) (err error) {
func promptValues(sub *rabbitmq.Subscription) (err error) {
// Topic
p := prompt.NewPrompt(
"AMQP subscription Topic",
Expand Down Expand Up @@ -104,5 +105,5 @@ func promptValues(sub *amqp.Subscription) (err error) {
}
sub.Queue.Name = queue

return nil
return nil
}
2 changes: 2 additions & 0 deletions internal/bundle/transport/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

const AMQPTransportKey = "transport.amqp"
const AMQPTransportEnabledKey = "transport.amqp.enabled"
const AMQPDefaultAddressKey = "transport.amqp.default_address"

type amqpTransport struct {
Expand All @@ -32,6 +33,7 @@ func InitializeAMQP() (transport *amqpTransport, err error) {
}

config.Set(AMQPDefaultAddressKey, transport.DefaultAddress)
config.Set(AMQPTransportEnabledKey, true)
godin.SaveConfiguration()

return transport, nil
Expand Down
6 changes: 6 additions & 0 deletions internal/generate/amqp_subscriber_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type AmqpSubscriberHandler struct {
BaseGenerator
subscriber template.Subscriber
}

func NewAMQPSubscriberHandler(sub template.Subscriber, box packr.Box, serviceInterface *types.Interface, ctx template.Context, options ...Option) *AmqpSubscriberHandler {
Expand All @@ -31,10 +32,15 @@ func NewAMQPSubscriberHandler(sub template.Subscriber, box packr.Box, serviceInt
iface: serviceInterface,
opts: defaults,
},
sub,
}
}

func (s *AmqpSubscriberHandler) Update() error {

// overwrite the contet subscribers with the - to be generated - subscriber.
s.opts.Context.Service.Subscriber = []template.Subscriber{s.subscriber}

if s.TargetExists() {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/amqp_subscriber_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewAMQPSubscriber(box packr.Box, serviceInterface *types.Interface, ctx tem
Overwrite: true,
IsGoSource: true,
Template: "amqp_subscriber_init",
TargetFile: "internal/service/subscriber/initialize.go",
TargetFile: "internal/amqp/subscriptions.go",
}

for _, opt := range options {
Expand All @@ -32,7 +32,7 @@ func NewAMQPSubscriber(box packr.Box, serviceInterface *types.Interface, ctx tem
}
}

// Update will call GenerateFull. The cmd/main.go cannot be updated.
// Update will call GenerateFull
func (s *AmqpSubscriber) Update() error {
return s.GenerateFull()
}
17 changes: 14 additions & 3 deletions internal/template/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package template

import (
"fmt"
"github.com/lukasjarosch/godin/internal/bundle/transport"
"strings"

"github.com/go-godin/rabbitmq"
"github.com/lukasjarosch/godin/internal/bundle"
"github.com/lukasjarosch/godin/pkg/amqp"
"github.com/mitchellh/mapstructure"

"github.com/lukasjarosch/godin/internal"
Expand All @@ -30,7 +31,7 @@ func NewContextFromConfig() Context {
sub := config.GetStringMap(bundle.SubscriberKey)
if len(sub) > 0 {
for x := range sub {
s := &amqp.Subscription{}
s := &rabbitmq.Subscription{}
mapstructure.Decode(sub[x], s)
subscribers = append(subscribers, Subscriber{
Subscription: *s,
Expand All @@ -46,6 +47,10 @@ func NewContextFromConfig() Context {
Module: config.GetString("service.module"),
LoggingMiddleware: config.GetBool("service.middleware.logging"),
Subscriber: subscribers,
Transport:Transport{
GRPC: config.GetBool("transport.grpc.enabled"),
AMQP: config.GetBool(transport.AMQPTransportEnabledKey),
},
},
Protobuf: Protobuf{
Package: config.GetString("protobuf.package"),
Expand Down Expand Up @@ -120,20 +125,26 @@ type Service struct {
Module string
LoggingMiddleware bool
Subscriber []Subscriber
Transport Transport
}

type Protobuf struct {
Package string
Service string
}

type Transport struct {
GRPC bool
AMQP bool
}

type Docker struct {
Registry string
}

type Subscriber struct {
Handler string
Subscription amqp.Subscription
Subscription rabbitmq.Subscription
}

type Variable struct {
Expand Down
1 change: 0 additions & 1 deletion pkg/amqp/publish.go

This file was deleted.

46 changes: 0 additions & 46 deletions pkg/amqp/rabbitmq.go

This file was deleted.

136 changes: 0 additions & 136 deletions pkg/amqp/subscribe.go

This file was deleted.

Loading

0 comments on commit 0f502a3

Please sign in to comment.