-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/google/uuid" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
cli "github.com/urfave/cli/v2" | ||
) | ||
|
||
func fatalOnError(err error, msg string) { | ||
if err != nil { | ||
log.Fatalf("💥 %s: %s", msg, err) | ||
} | ||
} | ||
|
||
func isFlagSet(name string) bool { | ||
found := false | ||
flag.Visit(func(f *flag.Flag) { | ||
if f.Name == name { | ||
found = true | ||
} | ||
}) | ||
return found | ||
} | ||
var Version = "development" | ||
|
||
func main() { | ||
url := flag.String("url", "", "RabbitMQ url, must start with amqps:// or amqp://.") | ||
exchange := flag.String("exchange", "", "Exchange name to listen messages.") | ||
queue := flag.String("queue", "interceptor", "Interceptor queue name.") | ||
routingKey := flag.String("bind", "#", "Routing key to bind.") | ||
flag.Parse() | ||
app := &cli.App{ | ||
Name: "coyote", | ||
Usage: "Coyote is a RabbitMQ message sink.", | ||
Version: Version, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "url", | ||
Required: true, | ||
Usage: "RabbitMQ url, must start with amqps:// or amqp://.", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "exchange", | ||
Required: true, | ||
Usage: "Exchange name to listen messages.", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "queue", | ||
Value: "interceptor", | ||
Usage: "Interceptor queue name.", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "bind", | ||
Value: "#", | ||
Usage: "Routing key to bind.", | ||
}, | ||
}, | ||
Action: func(ctx *cli.Context) error { | ||
conn, err := amqp.Dial(ctx.String("url")) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to RabbitMQ: %w", err) | ||
} | ||
defer conn.Close() | ||
|
||
if !isFlagSet("url") || !isFlagSet("exchange") { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
ch, err := conn.Channel() | ||
if err != nil { | ||
return fmt.Errorf("failed to open a channel: %w", err) | ||
} | ||
defer ch.Close() | ||
|
||
conn, err := amqp.Dial(*url) | ||
fatalOnError(err, "Failed to connect to RabbitMQ") | ||
defer conn.Close() | ||
err = ch.ExchangeDeclarePassive(ctx.String("exchange"), "topic", false, true, false, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to exchange: %w", err) | ||
} | ||
|
||
ch, err := conn.Channel() | ||
fatalOnError(err, "Failed to open a channel") | ||
defer ch.Close() | ||
q, err := ch.QueueDeclare(fmt.Sprintf("%s.%s", ctx.String("queue"), uuid.NewString()), false, true, false, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to declare a queue: %w", err) | ||
} | ||
ch.QueueBind(q.Name, ctx.String("bind"), ctx.String("exchange"), false, nil) | ||
|
||
err = ch.ExchangeDeclarePassive(*exchange, "topic", false, true, false, false, nil) | ||
fatalOnError(err, "Failed to connect to exchange") | ||
msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to register a consumer: %w", err) | ||
} | ||
|
||
q, err := ch.QueueDeclare(fmt.Sprintf("%s.%s", *queue, uuid.NewString()), false, true, false, false, nil) | ||
fatalOnError(err, "Failed to declare a queue") | ||
ch.QueueBind(q.Name, *routingKey, *exchange, false, nil) | ||
var forever chan struct{} | ||
go func() { | ||
for d := range msgs { | ||
log.Printf("📧 Received a message on queue %s: %s", d.RoutingKey, d.Body) | ||
} | ||
}() | ||
|
||
msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil) | ||
fatalOnError(err, "Failed to register a consumer") | ||
|
||
var forever chan struct{} | ||
go func() { | ||
for d := range msgs { | ||
log.Printf("📧 Received a message on queue %s: %s", d.RoutingKey, d.Body) | ||
} | ||
}() | ||
|
||
log.Printf("⏳ Waiting for messages. To exit press CTRL+C") | ||
<-forever | ||
log.Printf("⏳ Waiting for messages. To exit press CTRL+C") | ||
<-forever | ||
return nil | ||
}, | ||
} | ||
if err := app.Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |