For the last couple of weeks, it has been a constant challenge to work with RabbitMQ in Golang. And during these couple of weeks I noticed that all existing libraries are super-verboose, and I found a solution to this problem - write my own microframework to create microservices with RabbitMQ.
The name of this project is VOLTA.
For the last year, for API development, I've been using a library called Fiber. I really like the way you work with it and the fact that it's similar to Express.js. So I decided to implement a similar usage pattern in Volta, you create simple handlers and just register them to a queue.
I want to roughly demonstrate how it works and ease of use.
func main() {
app := volta.New(volta.Config{
RabbitMQ: "amqp://guest:guest@localhost:5672/",
})
app.AddExchanges(volta.Exchange{Name: "test", Type: "topic"})
app.AddQueue(volta.Queue{Name: "test", RoutingKey: "test", Exchange: "test"})
app.AddConsumer("test", Handler)
if err := app.Listen(); err != nil {
panic(err)
}
}
func Handler(ctx *volta.Ctx) error {
return ctx.Reply([]body("Hello, World!"))
}
I also lacked the functionality of middlewares, so I had to implement those as well.
app.Use(func(ctx *volta.Ctx) error {
fmt.Println("Before")
return ctx.Next()
})
app.AddConsumer("testing", func(ctx *volta.Ctx) error {
fmt.Println("Handler")
return ctx.Reply([]body("Hello, World!"))
})
// Output:
// Before
// Handler
Also, I like the ability in Fiber to change the serialization of JSON, so I brazenly stole their idea to Volta
app := volta.New(volta.Config{
...
Marshal: sonic.Marshal,
Unmarshal: sonic.Unmarshal,
...
})
My project is only a couple days old and needs ideas and tweaks, I'd love if you could help with that :)
Top comments (0)