{"id":18259964,"url":"https://github.com/theflyingcodr/rabbitmq","last_synced_at":"2025-07-25T11:09:20.032Z","repository":{"id":57642756,"uuid":"134391034","full_name":"theflyingcodr/rabbitmq","owner":"theflyingcodr","description":"Golang Amqp library providing consumer and publisher interfaces","archived":false,"fork":false,"pushed_at":"2018-06-08T22:04:25.000Z","size":2773,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-08T23:44:27.438Z","etag":null,"topics":["amqp","consumer","go","golang-amqp-library","rabbitmq"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theflyingcodr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-22T09:21:04.000Z","updated_at":"2019-09-17T03:01:53.000Z","dependencies_parsed_at":"2022-08-27T22:02:15.590Z","dependency_job_id":null,"html_url":"https://github.com/theflyingcodr/rabbitmq","commit_stats":null,"previous_names":["azert-software/rabbitmq"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/theflyingcodr/rabbitmq","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theflyingcodr%2Frabbitmq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theflyingcodr%2Frabbitmq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theflyingcodr%2Frabbitmq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theflyingcodr%2Frabbitmq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theflyingcodr","download_url":"https://codeload.github.com/theflyingcodr/rabbitmq/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theflyingcodr%2Frabbitmq/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266997193,"owners_count":24018934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amqp","consumer","go","golang-amqp-library","rabbitmq"],"created_at":"2024-11-05T10:41:15.654Z","updated_at":"2025-07-25T11:09:19.975Z","avatar_url":"https://github.com/theflyingcodr.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Work In Progress\nThis is in pre-alpha, needs a lot of testing!\n\n# RabbitMq\nA small wrapper library for [streadway/amqp](https://github.com/streadway/amqp) that provides an opinionated way of setting up RabbitMq consumers.\n\nDon't worry about loosing connection to RabbitMq, the library will manage the connection and gracefully handle network issues and restart as soon as it has reached the Rabbit server.\n\nDeadlettering is a first class citizen and all queues by default have their own dead letter queues setup, with no configuration required from you.\n\nSane defaults are set for Exchanges \u0026 Queues so the only required configuration from you is an amqp url and exchange names. Of course, you can override defaults by supplying your own configuration values for Exchanges \u0026 Queues.\n\nSetting up Queues with Routing Keys is as easy as setting up an Http server with full support for Handlers \u0026 Middleware using familar patterns.\n\nQueue setup borrows ideas fairly heavily from (Gizmo)[https://github.com/NYTimes/gizmo]\n\n## A Consumer Example\n\nBelow is an example Consumer Implementation, yes, there is a simple Interface you can implement so you can be confident of compatibility.\n\n``` go\n// MyConsumer is an example consumer\ntype MyConsumer struct{\n   // consumer.ConsumerConfig can be embedded here to override the defaults\n}\n\n// NewMyConsumer sets up and returns the consumer\n// You could pass a customer consumer.ConsumerConfig here and return\n// in the init method\nfunc NewMyConsumer() *MyConsumer {\n   return \u0026MyConsumer{}\n}\n\n// Init is generally to be used to return a consumer.ConsumerConfig\n// which has been passed to the consumer.\nfunc(c *MyConsumer) Init() (*consumer.ConsumerConfig, error) {\n   return nil, nil\n}\n\n// Prefix gets prepended to all queues registered\n// to this consumer. An empty string can\n// be returned which will mean no prefix will appear\nfunc(c *MyConsumer) Prefix() string{\n   return \"azert\"\n}\n\n// Middleware is to be used to add consumer level middleware, this will\n// affect all queues registered \u0026 could be used to reject messages\n// based on headers on body type for example\nfunc(c *MyConsumer) Middleware(next consumer.HandlerFunc) consumer.HandlerFunc{\n   return next\n}\n\n// Queues is used to setup Queues with routing keys and handlers\nfunc(c *MyConsumer)Queues(ctx context.Context) (map[string]*consumer.Routes){\n   return map[string]*consumer.Routes{\n      \"error\":{ // queue name\n\t\t  Keys: []string{\"test.error\", \"azert.error.#\"}, // routing keys\n\t\t  DeliveryFunc:c.ErrorHandler, // handler func\n\t  },\n\t  \"success\":{ // queue name\n\t\t  Keys: []string{\"test.success\"}, // routing keys\n\t\t  DeliveryFunc:c.SuccessHandler, // handler func\n\t\t},\n\t}\n}\n\n// TestHandler is a really basic handler that returns an error\nfunc (c *MyConsumer) ErrorHandler(ctx context.Context, m amqp.Delivery) error{\n\tlogrus.Info(\"hit error handler\")\n\n\t // returning an error will automatically trigger a nack\n\t // and send the message to the deadletter queue\n\t return fmt.Errorf(\"oh no i failed\")\n}\n\n// SuccessHandler is a really basic handlerthat returns a success\nfunc (c *MyConsumer) SuccessHandler(ctx context.Context, m amqp.Delivery) error{\n   logrus.Info(\"hit success handler\")\n\n   // returning nil will automatically trigger an ack\n   return nil\n}\n```\n## Running it\nTo run the above consumer example we add a main.go file with the below code\n\n``` go\nfunc main(){\n   logrus.SetLevel(logrus.DebugLevel)\n\n   // setup \u0026 configure our Host\n   host := consumer.NewConsumerHost(\u0026consumer.HostConfig{Address:\"amqp://guest:guest@localhost:5672/\"})\n\n   // this is a basic example, config would usually be setup via env vars or a file\n   exchange := \u0026consumer.ExchangeConfig{Name:\"test\"}\n\n   // to add global custom middleware, just chain it here\n   // if you don't need global middle just remove this method\n   host.Middleware(consumer.MessageDump, consumer.JsonHandler)\n\n   // this registers a list of consumers with an exchange which\n   // is defined in configuration // multiple brokers can be added if required\n   host.AddBroker(context.Background(), exchange, []consumer.Consumer{NewMyConsumer()})\n\n   // run the consumer, it will exit on a setup error\n   // or on a graceful shutdown ie ctrl+c\n   if err := host.Run(context.Background()); err != nil{\n      logrus.Error(err)\n   }\n}\n```\n##  Runable Example\nAn example implementation can be found under the *examples* folder. ``` go run main.go``` will kick it off and you can try publishing messages and observe the results.\n\nFor testing I recommend using docker with the rabbitmq:management-alpine images using the below command\n\n```bash\ndocker run -d --hostname test-rabbit --name rabbitmq-test -p 5672:5672 -p 15672:15672 rabbitmq:management-alpine\n```\n\n## Publishing\nA publisher implementation will appear soon, for testing either write your own or using the RabbitMq ui for now.\n\n## Middleware\nA key feature of Go \u0026 especially http servers is the ability to write \u0026 chain middleware.\n\nThis library fully supports middleware and allows the user to define and include their own. The interface is\n\n```go\ntype MessageHandler interface {\n   HandleMessage(context.Context,amqp.Delivery)\n}\n```\nAnd the HandlerFunc is\n```go\ntype HandlerFunc func(context.Context, amqp.Delivery)\n```\nThere are currently two bits of middleware that can be used by users, they are *JsonHandler* \u0026 *MessageDump*, you can look at them in the *consumer/middleware.go* file for examples of writing middleware.\n\nMiddleware can be added by users at two levels\n### Consumer Level Middleware\n\n``` go\nfunc(c *MyConsumer) Middleware(next consumer.HandlerFunc) consumer.HandlerFunc{\n   return consumer.JsonHandler(consumer.MessageDump(next))\n}\n```\nThis shows the pre written middleware being added to the Consumers Middleware method.\n\n```go\nfunc(c *MyConsumer) Middleware(next consumer.HandlerFunc) consumer.HandlerFunc{\n   return func(ctx context.Context, d amqp.Delivery) {\n      if d.ContentType != \"mycompany/customtype\"{\n         d.Nack(false, false)\n         return\n  }\n      next(ctx, d)\n   }\n}\n```\nAbove shows a bit of custom middleware written by a user that rejects messages based on contentType\n\n### Global Middleware\nYou can also add middleware that gets executed for **every** consumer registered to the host. This is done as shown in your main.go file\n\n```go\n// setup \u0026 configure our Host\nhost := consumer.NewConsumerHost(\u0026consumer.HostConfig{Address:\"amqp://guest:guest@localhost:5672/\"})\n\n// this is a basic example, config would usually be setup via env vars or a file\nexchange := \u0026consumer.ExchangeConfig{Name:\"test\"}\n\n// to add global custom middleware, just chain it here\n// if you don't need global middle just remove this method\nhost.Middleware(consumer.MessageDump, consumer.JsonHandler)\n```\nNote the final line, this is a method that can be used to list a chain of middleware. The method itself is optional, if you don't want to add global middleware, don't add the method, simple.\n\nHere i am using the MessageDump debug middleware \u0026 the JsonHandler, this will reject messages that aren't \"application/json\"\n\nAs with the consumer, you can also define and chain your own middleware. To keep things neat, it's best to define these funcs elsewhere and then add them to the chain as shown above.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheflyingcodr%2Frabbitmq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheflyingcodr%2Frabbitmq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheflyingcodr%2Frabbitmq/lists"}